Tauruser

Enjoy Every Day
posts - 34, comments - 95, trackbacks - 0, articles - 5
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

05年写的直线裁剪算法

Posted on 2007-01-28 10:28 Tauruser 阅读(2737) 评论(2)  编辑 收藏 引用 所属分类: 计算机图形学
头文件<line.h>

#pragma once
#include 
"afx.h"
#include 
"atltypes.h"
/*
 * Powered by Tauruser
 * Tauruser~gmail.com
 * Written on 2005.12
 * 欢迎转载,请说明出处
 
*/
 
class CLine :
    
public CObject
{
    DECLARE_SERIAL (CLine)
public:
    CLine(
void);
    
~CLine(void);
private:
    CPoint m_ptFrom;
private:
    CPoint m_ptTo;
public:
    CLine(CPoint ptFrom, CPoint ptTo);
    
void Draw(CDC* pDC);
    
void Serialize(CArchive& ar);
    CLine
* SetLine(CPoint ptFrom, CPoint ptTo);
    
// 剪裁线段,参数为窗口左上角和右下角坐标,当线段在窗口内返回TRUE,否则

FALSE
    
bool Cut(CPoint ptUpLeft, CPoint ptDownRight);
}
;

源码<line.cpp>

#include "StdAfx.h"
#include 
".\line.h"
/*
 * Powered by Tauruser
 * Tauruser~gmail.com
 * Written on 2005.12
 * 欢迎转载,请说明出处
 
*/
 
IMPLEMENT_SERIAL (CLine,CObject,
1)
CLine::CLine(
void)
: m_ptFrom(
0)
, m_ptTo(
0)
{
}


CLine::
~CLine(void)
{
}


CLine::CLine(CPoint ptFrom, CPoint ptTo)
{
    m_ptFrom
=ptFrom;
    m_ptTo
=ptTo;
}


void CLine::Draw(CDC* pDC)
{
    pDC
->MoveTo(m_ptFrom);
    pDC
->LineTo(m_ptTo);
}


void CLine::Serialize(CArchive& ar)
{
    CObject::Serialize(ar);
    
if(ar.IsStoring())
        ar
<<m_ptFrom<<m_ptTo;
    
else
        ar
>>m_ptFrom>>m_ptTo;
}


CLine
* CLine::SetLine(CPoint ptFrom, CPoint ptTo)
{
    m_ptFrom
=ptFrom;
    m_ptTo
=ptTo;
    
return this;
}


// 剪裁线段,参数为窗口左上角和右下角坐标,当线段在窗口内返回TRUE,否则FALSE
bool CLine::Cut(CPoint ptUpLeft, CPoint ptDownRight)
{
    
//long iA,iB,iC,iD,iXl,iXr,iYt,iYb; 
    const long iA(m_ptFrom.x), iB(m_ptFrom.y), iC(m_ptTo.x), iD(m_ptTo.y);
    
long iXl=ptUpLeft.x, iXr=ptDownRight.x, iYt=ptUpLeft.y, iYb=ptDownRight.y;
    
int temp;
    
if (iXl>iXr) 
    
{
        temp
=iXl;
        iXl
=iXr;
        iXr
=temp;
    }

    
if(iYb>iYt)
    
{
        temp
=iYb;
        iYb
=iYt;
        iYt
=temp;
    }


    
if(iXl<=iA && iA<=iXr)
    
{
        
if(iYb<=iB && iB<=iYt)
        
{    ;//m_ptFrom保持原值
        }
else if(iB<iYb && iD<iYb) 
            
return false;//与窗口无交.
        else if (iB<iYb && iD>=iYb)
        
{
            
//1.2
            int x;
            x
=iA-((iB-iYb)*(iA-iC)/(iB-iD));
            
if(iXl<=&& x<=iXr)//检验x的有校性
            {
                m_ptFrom.x
=x;
                m_ptFrom.y
=iYb;

            }
else return false;//否则无交点

        }
else if(iB>iYt && iD>iYt)     return false;//与窗口无交
        else if(iB>iYt && iD<=iYt)
        
{    
            
//1.3
            int x;
            x
=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性 
            if(iXl<=&& x<=iXr)//
            {
                m_ptFrom.x
=x;
                m_ptFrom.y
=iYt;
            }
else return false;//否则无交点

        }

        

    }
else if(iA<iXl)
    
{
        
if(iC<iXl) return false;
        
else
        
{
            
int y;
            y
=iB-((iA-iXl)*(iB-iD)/(iA-iC));

            
if(iYb<=&& y<=iYt)
            
{
                m_ptFrom.x
=iXl;
                m_ptFrom.y
=y;
            }
else if ((y<iYb || y>iYt) && (iYb<=iB && iB<=iYt)) 
            
{
                
return false;
            }

            
else if(y<iYb || iB<iYb)
            
{
                
//1.2
                int x;
                x
=iA-((iB-iYb)*(iA-iC)/(iB-iD));
                
if(iXl<=&& x<=iXr)//检验x的有校性
                {
                    m_ptFrom.x
=x;
                    m_ptFrom.y
=iYb;
                }
else return false;//否则无交点
            }

            
else if(y>iYt || iB>iYt)
            
{
                
//1.3
                int x;
                x
=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性 
                if(iXl<=&& x<=iXr)//
                {
                    m_ptFrom.x
=x;
                    m_ptFrom.y
=iYt;
                }
else return false;//否则无交点
            }

        }


    }
else if(iA>iXr)
    
{
        
if(iC>iXr) return false;//
        else
        
{
            
int y;
            y
=iB-((iA-iXr)*(iB-iD)/(iA-iC));
            
            
if(iYb<=&& y<=iYt)
            
{
                m_ptFrom.x
=iXr;
                m_ptFrom.y
=y;
            }
else if ((y<iYb || y>iYt) && (iYb<=iB && iB<=iYt)) 
            
{
                
return false;
            }

            
else if(y<iYb || iB<iYb)
            
{
                
//1.2
                int x;
                x
=iA-((iB-iYb)*(iA-iC)/(iB-iD));
                
if(iXl<=&& x<=iXr)//检验x的有校性
                {
                    m_ptFrom.x
=x;
                    m_ptFrom.y
=iYb;
                }
else return false;//否则无交点
            }

            
else if(y>iYt || iB>iYt)
            
{
                
//1.3
                int x;
                x
=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性 
                if(iXl<=&& x<=iXr)//
                {
                    m_ptFrom.x
=x;
                    m_ptFrom.y
=iYt;
                }
else return false;//否则无交点
            }


        }


    }

//////////////////////////////////////////////////////////////////////////////////

    
if(iXl<=iC && iC<=iXr)
    
{
        
if(iYb<=iD && iD<=iYt) 
            
return true;//m_ptFrom保持原值
        else if(iB<iYb && iD<iYb) return false;//与窗口无交.
        else if(iD<iYb && iB>=iYb)
        
{
            
//1.2
            int x;
            x
=iA-((iB-iYb)*(iA-iC)/(iB-iD));
            
if(iXl<=&& x<=iXr)//检验x的有校性
            {
                m_ptTo.x
=x;
                m_ptTo.y
=iYb;
                
return true;
            }
else return false;//否则无交点
        }
else if(iB>iYt && iD>iYt) return false;
        
else if(iD>iYt && iB<=iYt)
        
{    
            
//1.3
            int x;
            x
=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性 
            if(iXl<=&& x<=iXr)//
            {
                m_ptTo.x
=x;
                m_ptTo.y
=iYt;
                
return true;
            }
else return false;//否则无交点

        }

        

    }
else if(iC<iXl)
    
{
        
if(iA<iXl) return false;
        
else
        
{
            
int y;
            y
=iB-((iA-iXl)*(iB-iD)/(iA-iC));

            
if(iYb<=&& y<=iYt)
            
{
                m_ptTo.x
=iXl;
                m_ptTo.y
=y;
                
return true;
            }
else if ((y<iYb || y>iYt) && (iYb<=iD && iD<=iYt)) 
            
{
                
return false;
            }

            
else if(y<iYb || iB<iYb)
            
{
                
//1.2
                int x;
                x
=iA-((iB-iYb)*(iA-iC)/(iB-iD));
                
if(iXl<=&& x<=iXr)//检验x的有校性
                {
                    m_ptTo.x
=x;
                    m_ptTo.y
=iYb;
                    
return true;
                }
else return false;//否则无交点
            }

            
else if(y>iYt || iB>iYt)
            
{
                
//1.3
                int x;
                x
=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性 
                if(iXl<=&& x<=iXr)//
                {
                    m_ptTo.x
=x;
                    m_ptTo.y
=iYt;
                    
return true;
                }
else return false;//否则无交点
            }

        }


    }
else if(iC>iXr)
    
{
        
if(iA>iXr) return false;//
        else
        
{
            
int y;
            y
=iB-((iA-iXr)*(iB-iD)/(iA-iC));
            
            
if(iYb<=&& y<=iYt)
            
{
                m_ptTo.x
=iXr;
                m_ptTo.y
=y;
                
return true;
            }
else if ((y<iYb || y>iYt) && (iYb<=iD && iD<=iYt)) 
            
{
                
return false;
            }

            
else if(y<iYb || iD<iYb)
            
{
                
//1.2
                int x;
                x
=iA-((iB-iYb)*(iA-iC)/(iB-iD));
                
if(iXl<=&& x<=iXr)//检验x的有校性
                {
                    m_ptTo.x
=x;
                    m_ptTo.y
=iYb;
                    
return true;
                }
else return false;//否则无交点
            }

            
else if(y>iYt || iB>iYt)
            
{
                
//1.3
                int x;
                x
=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性 
                if(iXl<=&& x<=iXr)//
                {
                    m_ptTo.x
=x;
                    m_ptTo.y
=iYt;
                    
return true;
                }
else return false;//否则无交点
            }


        }


    }

    
return true;
    
}



Feedback

# re: 05年写的直线裁剪算法  回复  更多评论   

2007-06-30 13:18 by hello007@126.com
好,谢谢

# re: 05年写的直线裁剪算法  回复  更多评论   

2010-03-01 00:28 by 啊啊啊啊啊啊
你这个算法也太复杂了吧

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理