交互式画线、画圆及编码裁剪

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

交互式画线、画圆及编码裁剪
#define MaxNumber 1024
protected: // create from serialization only
CFIGUREView();
DECLARE_DYNCREA TE(CFIGUREView)
POINT m_start,m_end;
POINT m_start2,m_end2;
// Attributes
private:
bool bVCFlag,bDDAFlag,bMidFlag,bBreFlag;
bool m_bPNCircleFlag;
bool m_bLine;
int m_iCurrenctCount;
void encode (int x,int y,int * code);
void C_S_Line();
void INTERACTIVECIRCLE(CPoint start,CPoint end,COLORREF color); void INTERACTIVELINE(CPoint start2,CPoint end2,COLORREF color); CPoint m_ptStart,m_ptEnd;
CPoint ptStart[MaxNumber],ptEnd[MaxNumber];
//鼠标事件
void CFIGUREView::OnLButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default CView::OnLButtonDown(nFlags, point);
m_start=point;
m_start2=point;
}
void CFIGUREView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point);
m_end=point;
m_end2=point;
INTERACTIVELINE(m_start,m_end,RGB(255,0,0));
INTERACTIVECIRCLE(m_start2,m_end2,RGB(0,0,255));
}
void CFIGUREView::OnRButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default m_ptStart = point;
CView::OnRButtonDown(nFlags, point);
}
void CFIGUREView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptEnd = point;
m_bLine = true;
Invalidate();
CView::OnRButtonUp(nFlags, point);
}
//交互式画线
void CFIGUREView::INTERACTIVELINE(CPoint start,CPoint end,COLORREF color) {
{
CClientDC dc(this);
int dx,dy,steps,i;
float x,y,xinc,yinc;
dx=end.x-start.x;
dy=end.y-start.y;
x=start.x;
y=start.y;
steps=max(dx,dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
dc.SetPixel((int)(x+0.5),(int)(y+0.5),RGB(255,0,0));
for (i=0;i<steps;i++)
{
x+=xinc;
y+=yinc;
dc.SetPixel((int)(x+0.5),(int)(y+0.5),RGB(255,0,0));
}
}
}
//交互式画圆
void CFIGUREView::INTERACTIVECIRCLE(CPoint start2,CPoint end2,COLORREF color) {
{
CClientDC dc(this);
int x , y ,R,f;
R=sqrt((end2.x-start2.x)*(end2.x-start2.x)+(end2.y-start2.y)*(end2.y-start2.y));
x=0;
y=R;
f=1-R;
dc.SetPixel(start2.x+x,end2.y+y,RGB(0,0,255)); dc.SetPixel(start2.x+x,end2.y-y,RGB(0,0,255)); dc.SetPixel(start2.x-x,end2.y+y,RGB(0,0,255)); dc.SetPixel(start2.x+y,end2.y+x,RGB(0,0,255)); dc.SetPixel(start2.x+y,end2.y-x,RGB(0,0,255)); dc.SetPixel(start2.x-y,end2.y+x,RGB(0,0,255)); dc.SetPixel(start2.x-x,end2.y-y,RGB(0,0,255)); dc.SetPixel(start2.x-y,end2.y-x,RGB(0,0,255)); while (x<y)
{
if (f<0)
{
f=f+2*x+3;
x++;
}
else
{
f=f+2*(x-y)+5;
x++;
y--;
}
dc.SetPixel(start2.x+x,end2.y+y,RGB(0,0,255)); dc.SetPixel(start2.x+x,end2.y-y,RGB(0,0,255)); dc.SetPixel(start2.x-x,end2.y+y,RGB(0,0,255)); dc.SetPixel(start2.x+y,end2.y+x,RGB(0,0,255)); dc.SetPixel(start2.x+y,end2.y-x,RGB(0,0,255)); dc.SetPixel(start2.x-y,end2.y+x,RGB(0,0,255)); dc.SetPixel(start2.x-x,end2.y-y,RGB(0,0,255)); dc.SetPixel(start2.x-y,end2.y-x,RGB(0,0,255)); }
}
}
//编码裁剪
void CFIGUREView::OnClipCodeclip()
{
// TODO: Add your command handler code here C_S_Line();
Invalidate();
}
void CFIGUREView::encode (int x,int y,int * code) {
int c=0;
if(x<xl)
c=c|LEFT;
else if(x>xr)
c=c|RIGHT;
if (y<yb)
c=c|BOTTOM;
else if (y>yt)
c=c|TOP;
*code=c;
}
void CFIGUREView::C_S_Line()
{
int code1,code2,code, x,y;
encode(m_ptStart.x,m_ptStart.y,&code1);
encode(m_ptEnd.x,m_ptEnd.y,&code2);
while(code1!=0 || code2!=0)
{
if ((code1 & code2) !=0)
{
m_bLine = false;
return;
}
code=code1;
if (code1==0) code=code2;
if ((LEFT & code) !=0)
{
x=xl;
y=m_ptStart.y+(m_ptEnd.y-m_ptStart.y)*(xl-m_ptStart.x)/(m_ptEnd.x-m_ptStart.x); }
else if((RIGHT & code)!=0)
{
x=xr;
y=m_ptStart.y+(m_ptEnd.y-m_ptStart.y)*(xr-m_ptStart.x)/(m_ptEnd.x-m_ptStart.x); }
else if((BOTTOM & code) != 0)
{
y=yb;
x=m_ptStart.x+(m_ptEnd.x-m_ptStart.x)*(yb-m_ptStart.y)/(m_ptEnd.y-m_ptStart.y); }
else if((TOP & code)!=0)
{
y=yt;
x=m_ptStart.x+(m_ptEnd.x-m_ptStart.x)*(yt-m_ptStart.y)/(m_ptEnd.y-m_ptStart.y); }
if(code==code1)
{
m_ptStart.x=x; m_ptStart.y=y;
encode(x,y,&code1);
}
else
{
m_ptEnd.x=x; m_ptEnd.y=y;
encode(x,y,&code2);
}
}
Invalidate();
}
//根据个人窗口设计的不同,稍作修改即可。

裁剪窗口根据个人情况绘制,以及Ondraw函数的制定等。

相关文档
最新文档