|
Posted on 2007-01-28 10:28 Tauruser 阅读(2754) 评论(2) 编辑 收藏 引用 所属分类: 计算机图形学
头文件<line.h> #pragma once
#include "afx.h"
#include "atltypes.h"
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
* Powered by Tauruser
* Tauruser~gmail.com
* Written on 2005.12
* 欢迎转载,请说明出处
*/
class CLine :
public CObject
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
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,否则
![](/Images/OutliningIndicators/InBlock.gif)
FALSE
bool Cut(CPoint ptUpLeft, CPoint ptDownRight);
};
![](/Images/OutliningIndicators/None.gif) 源码<line.cpp> #include "StdAfx.h"
#include ".\line.h"
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
* Powered by Tauruser
* Tauruser~gmail.com
* Written on 2005.12
* 欢迎转载,请说明出处
*/
IMPLEMENT_SERIAL (CLine,CObject,1)
CLine::CLine(void)
: m_ptFrom(0)
, m_ptTo(0)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
}
![](/Images/OutliningIndicators/None.gif)
CLine::~CLine(void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
}
![](/Images/OutliningIndicators/None.gif)
CLine::CLine(CPoint ptFrom, CPoint ptTo)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
m_ptFrom=ptFrom;
m_ptTo=ptTo;
}
![](/Images/OutliningIndicators/None.gif)
void CLine::Draw(CDC* pDC)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
pDC->MoveTo(m_ptFrom);
pDC->LineTo(m_ptTo);
}
![](/Images/OutliningIndicators/None.gif)
void CLine::Serialize(CArchive& ar)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
CObject::Serialize(ar);
if(ar.IsStoring())
ar<<m_ptFrom<<m_ptTo;
else
ar>>m_ptFrom>>m_ptTo;
}
![](/Images/OutliningIndicators/None.gif)
CLine* CLine::SetLine(CPoint ptFrom, CPoint ptTo)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
m_ptFrom=ptFrom;
m_ptTo=ptTo;
return this;
}
![](/Images/OutliningIndicators/None.gif)
// 剪裁线段,参数为窗口左上角和右下角坐标,当线段在窗口内返回TRUE,否则FALSE
bool CLine::Cut(CPoint ptUpLeft, CPoint ptDownRight)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
//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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
temp=iXl;
iXl=iXr;
iXr=temp;
}
if(iYb>iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
temp=iYb;
iYb=iYt;
iYt=temp;
}
![](/Images/OutliningIndicators/InBlock.gif)
if(iXl<=iA && iA<=iXr)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(iYb<=iB && iB<=iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { ;//m_ptFrom保持原值
}else if(iB<iYb && iD<iYb)
return false;//与窗口无交.
else if (iB<iYb && iD>=iYb)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.2
int x;
x=iA-((iB-iYb)*(iA-iC)/(iB-iD));
if(iXl<=x && x<=iXr)//检验x的有校性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=x;
m_ptFrom.y=iYb;
![](/Images/OutliningIndicators/InBlock.gif)
}else return false;//否则无交点
![](/Images/OutliningIndicators/InBlock.gif)
}else if(iB>iYt && iD>iYt) return false;//与窗口无交
else if(iB>iYt && iD<=iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.3
int x;
x=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性
if(iXl<=x && x<=iXr)//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=x;
m_ptFrom.y=iYt;
}else return false;//否则无交点
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}else if(iA<iXl)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(iC<iXl) return false;
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int y;
y=iB-((iA-iXl)*(iB-iD)/(iA-iC));
![](/Images/OutliningIndicators/InBlock.gif)
if(iYb<=y && y<=iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=iXl;
m_ptFrom.y=y;
}else if ((y<iYb || y>iYt) && (iYb<=iB && iB<=iYt))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return false;
}
else if(y<iYb || iB<iYb)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.2
int x;
x=iA-((iB-iYb)*(iA-iC)/(iB-iD));
if(iXl<=x && x<=iXr)//检验x的有校性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=x;
m_ptFrom.y=iYb;
}else return false;//否则无交点
}
else if(y>iYt || iB>iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.3
int x;
x=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性
if(iXl<=x && x<=iXr)//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=x;
m_ptFrom.y=iYt;
}else return false;//否则无交点
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}else if(iA>iXr)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(iC>iXr) return false;//
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int y;
y=iB-((iA-iXr)*(iB-iD)/(iA-iC));
if(iYb<=y && y<=iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=iXr;
m_ptFrom.y=y;
}else if ((y<iYb || y>iYt) && (iYb<=iB && iB<=iYt))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return false;
}
else if(y<iYb || iB<iYb)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.2
int x;
x=iA-((iB-iYb)*(iA-iC)/(iB-iD));
if(iXl<=x && x<=iXr)//检验x的有校性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=x;
m_ptFrom.y=iYb;
}else return false;//否则无交点
}
else if(y>iYt || iB>iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.3
int x;
x=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性
if(iXl<=x && x<=iXr)//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptFrom.x=x;
m_ptFrom.y=iYt;
}else return false;//否则无交点
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**///////////////////////////////////////////////////////////////////////////////////
![](/Images/OutliningIndicators/InBlock.gif)
if(iXl<=iC && iC<=iXr)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(iYb<=iD && iD<=iYt)
return true;//m_ptFrom保持原值
else if(iB<iYb && iD<iYb) return false;//与窗口无交.
else if(iD<iYb && iB>=iYb)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.2
int x;
x=iA-((iB-iYb)*(iA-iC)/(iB-iD));
if(iXl<=x && x<=iXr)//检验x的有校性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.3
int x;
x=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性
if(iXl<=x && x<=iXr)//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=x;
m_ptTo.y=iYt;
return true;
}else return false;//否则无交点
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}else if(iC<iXl)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(iA<iXl) return false;
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int y;
y=iB-((iA-iXl)*(iB-iD)/(iA-iC));
![](/Images/OutliningIndicators/InBlock.gif)
if(iYb<=y && y<=iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=iXl;
m_ptTo.y=y;
return true;
}else if ((y<iYb || y>iYt) && (iYb<=iD && iD<=iYt))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return false;
}
else if(y<iYb || iB<iYb)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.2
int x;
x=iA-((iB-iYb)*(iA-iC)/(iB-iD));
if(iXl<=x && x<=iXr)//检验x的有校性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=x;
m_ptTo.y=iYb;
return true;
}else return false;//否则无交点
}
else if(y>iYt || iB>iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.3
int x;
x=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性
if(iXl<=x && x<=iXr)//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=x;
m_ptTo.y=iYt;
return true;
}else return false;//否则无交点
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}else if(iC>iXr)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(iA>iXr) return false;//
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int y;
y=iB-((iA-iXr)*(iB-iD)/(iA-iC));
if(iYb<=y && y<=iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=iXr;
m_ptTo.y=y;
return true;
}else if ((y<iYb || y>iYt) && (iYb<=iD && iD<=iYt))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return false;
}
else if(y<iYb || iD<iYb)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.2
int x;
x=iA-((iB-iYb)*(iA-iC)/(iB-iD));
if(iXl<=x && x<=iXr)//检验x的有校性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=x;
m_ptTo.y=iYb;
return true;
}else return false;//否则无交点
}
else if(y>iYt || iB>iYt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//1.3
int x;
x=iA-((iB-iYt)*(iA-iC)/(iB-iD));//检验x的有校性
if(iXl<=x && x<=iXr)//
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_ptTo.x=x;
m_ptTo.y=iYt;
return true;
}else return false;//否则无交点
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
return true;
}
![](/Images/OutliningIndicators/None.gif)
Feedback
# re: 05年写的直线裁剪算法 回复 更多评论
2007-06-30 13:18 by
好,谢谢
# re: 05年写的直线裁剪算法 回复 更多评论
2010-03-01 00:28 by
你这个算法也太复杂了吧
|