TTXView.cpp
资源名称:TTX.rar [点击查看]
上传用户:tengfei20
上传日期:2022-07-03
资源大小:11400k
文件大小:13k
源码类别:
界面编程
开发平台:
C/C++
- // TTXView.cpp : CTTXView 类的实现
- //
- #include "stdafx.h"
- #include "math.h"
- #include "TTX.h"
- #include "TTXDoc.h"
- #include "TTXView.h"
- #include "MyDialog1.h"
- #include "Mydialog2.h"
- #include "MyDialog3.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #define LEFT 1
- #define RIGHT 2
- #define BOTTOM 4
- #define TOP 8
- // CTTXView
- IMPLEMENT_DYNCREATE(CTTXView, CView)
- BEGIN_MESSAGE_MAP(CTTXView, CView)
- // 标准打印命令
- ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
- ON_COMMAND(ID_DDN, &CTTXView::OnDdn)
- ON_COMMAND(ID_MID, &CTTXView::OnMid)
- ON_COMMAND(ID_Circle, &CTTXView::OnCircle)
- ON_COMMAND(ID_Ellipse, &CTTXView::OnEllipse)
- ON_COMMAND(ID_RED, &CTTXView::OnRed)
- ON_COMMAND(ID_GREEN, &CTTXView::OnGreen)
- ON_COMMAND(ID_yellow, &CTTXView::Onyellow)
- ON_COMMAND(ID_mcut, &CTTXView::Onmcut)
- ON_WM_LBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_UPDATE_COMMAND_UI(ID_mcut, &CTTXView::OnUpdatemcut)
- ON_COMMAND(ID_Curve, &CTTXView::OnCurve)
- END_MESSAGE_MAP()
- // CTTXView 构造/析构
- CTTXView::CTTXView()
- : d_x(0)
- , d_x1(0)
- , d_x2(0)
- , d_y1(0)
- , d_y2(0)
- , fill(0)
- , WT(0)
- , WB(0)
- , WR(0)
- , WL(0)
- , m_list(0)
- , m_p1(0)
- , m_p2(0)
- , m_cut(false)
- {
- // TODO: 在此处添加构造代码
- WL=100;WR=500;WB=100;WT=300;
- m_p1.x=0; m_p1.y=0;
- m_p2.x=0; m_p2.y=0;
- m_list=0; //m_ist=0 表示起点,m_ist=1 表示终点
- }
- CTTXView::~CTTXView()
- {
- }
- BOOL CTTXView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: 在此处通过修改
- // CREATESTRUCT cs 来修改窗口类或样式
- return CView::PreCreateWindow(cs);
- }
- // CTTXView 绘制
- void CTTXView::OnDraw(CDC* /*pDC*/)
- {
- CTTXDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- if (!pDoc)
- return;
- // TODO: 在此处为本机数据添加绘制代码
- }
- // CTTXView 打印
- BOOL CTTXView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // 默认准备
- return DoPreparePrinting(pInfo);
- }
- void CTTXView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: 添加额外的打印前进行的初始化过程
- }
- void CTTXView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: 添加打印后进行的清理过程
- }
- // CTTXView 诊断
- #ifdef _DEBUG
- void CTTXView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CTTXView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CTTXDoc* CTTXView::GetDocument() const // 非调试版本是内联的
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTTXDoc)));
- return (CTTXDoc*)m_pDocument;
- }
- #endif //_DEBUG
- // CTTXView 消息处理程序
- //DDN算法画直线
- void CTTXView::OnDdn()
- {
- // TODO: Add your command handler code here
- MyDialog1 dlg;
- CDC* pDC;
- pDC=GetWindowDC();
- if(dlg.DoModal()==IDOK) //显示对话框
- {
- d_x1=dlg.XX1;
- d_y1=dlg.YY1;
- d_x2=dlg.XX2;
- d_y2=dlg.YY2;
- }
- //从对话框获取相应的数据
- float k;
- k=(float)(d_y2-d_y1)/(d_x2-d_x1);
- int x;
- float y=d_y1;
- for(x=d_x1;x<=d_x2;x++)
- {
- pDC->SetPixel(x,int(y+0.5),RGB(0,250,0));
- y=y+k;
- Sleep(20);
- }
- pDC->TextOut(d_x1+5,d_y1,_T("DDA 法"));
- m_cut=false;
- }
- void CTTXView::OnMid()
- {
- // TODO: Add your command handler code here
- MyDialog1 dlg;
- CDC* pDC;
- pDC=GetWindowDC();
- if(dlg.DoModal()==IDOK)
- {
- d_x1=dlg.XX1;
- d_y1=dlg.YY1;
- d_x2=dlg.XX2;
- d_y2=dlg.YY2;
- }
- //从对话框获取相应的数据
- float a,b,c;
- a=float(d_y1-d_y2);
- b=float(d_x2-d_x1);
- float k;
- k=(float)(d_y2-d_y1)/(d_x2-d_x1);
- float d;
- d=(1/k)*a+b;
- float x,y;
- y=float(d_y1);
- for(x=d_x1;x<=d_x2;x++)
- {
- pDC->SetPixel(x,y,RGB(255,0,255));
- if(d>=0)
- d=d+a;
- else
- {
- d=d+a+b;
- y=y+1;
- }
- Sleep(20);
- }
- pDC->TextOut(d_x1+5,d_y1,_T("中点法"));
- m_cut=false;
- }
- void CTTXView::OnCircle()
- {
- // TODO: Add your command handler code here
- RedrawWindow();
- Mydialog2 dlg;
- int r,a,b;
- if(dlg.DoModal()==IDOK)
- {
- r=dlg.R;
- a=dlg.X1;
- b=dlg.Y1;
- }
- int x=0,y=r; // 初始点(0,r)
- float d; // d--半径
- d=1.25-r;
- CDC* pDC;
- pDC=GetWindowDC();
- // 画八对称点
- pDC->SetPixel(a+x,b+y,RGB(250,0,0));
- pDC->SetPixel(a+y,b+x,RGB(250,0,0));
- pDC->SetPixel(a-y,b+x,RGB(250,0,0));
- pDC->SetPixel(a-x,b+y,RGB(250,0,0));
- pDC->SetPixel(a+y,b-x,RGB(250,0,0));
- pDC->SetPixel(a+x,b-y,RGB(250,0,0));
- pDC->SetPixel(a-x,b-y,RGB(250,0,0));
- pDC->SetPixel(a-y,b-x,RGB(250,0,0));
- while(x<=y)
- {
- if(d<0)
- {
- d+=2*x+3;
- }
- else
- {
- d+=2*(x-y)+5;
- y--;
- }
- x++;
- // 画八对称点
- pDC->SetPixel(a+x,b+y,RGB(250,0,0));
- pDC->SetPixel(a+y,b+x,RGB(250,0,0));
- pDC->SetPixel(a-y,b+x,RGB(250,0,0));
- pDC->SetPixel(a-x,b+y,RGB(250,0,0));
- pDC->SetPixel(a+y,b-x,RGB(250,0,0));
- pDC->SetPixel(a+x,b-y,RGB(250,0,0));
- pDC->SetPixel(a-x,b-y,RGB(250,0,0));
- pDC->SetPixel(a-y,b-x,RGB(250,0,0));
- Sleep(10);
- }
- m_cut=false;
- }
- void CTTXView::OnEllipse()
- {
- // TODO: Add your command handler code here
- RedrawWindow();
- CDC* pDC;
- pDC=GetWindowDC();
- MyDialog3 dlg;
- double d;
- int x,y;
- double ta2,tb2;
- double df1,df2;
- double a2,b2;
- int a,b;
- if(dlg.DoModal()==IDOK)
- {
- a=dlg.rrr;
- b=dlg.RRR;
- x=dlg.XXX;
- y=dlg.YYY;
- }
- x=0;y=b;
- a2=1.0*a*a;
- b2=1.0*b*b;
- ta2=a2*2;
- tb2=b2*2;
- d=b2+(0.25-b)*a2;
- df1=b2*3;
- df2=b2*3+ta2-ta2*b;
- while(b2*(x+1)<=a2*(y-0.5))
- {
- pDC->SetPixel(100+x,200+y,RGB(0,255,0));
- pDC->SetPixel(100-x,200+y,RGB(0,255,0));
- pDC->SetPixel(100+x,200-y,RGB(0,255,0));
- pDC->SetPixel(100-x,200-y,RGB(0,255,0));
- if(d<0)
- {
- d+=df1;
- x++;
- df1+=tb2;
- df2+=tb2;
- }
- else
- {
- d+=df2;
- x++;
- y--;
- df1+=tb2;
- df2+=tb2+ta2;
- }
- }
- df1=b2*(2*x+2)+a2*(3-2*y);
- df2=a2*(3-2*y);
- d=b2*(x+0.5)*(x+0.5)+a2*(y-1)*(y-1)-a2*b2;
- while(y>=0)
- {
- pDC->SetPixel(100+x,200+y,RGB(0,255,0));
- pDC->SetPixel(100-x,200+y,RGB(0,255,0));
- pDC->SetPixel(100+x,200-y,RGB(0,255,0));
- pDC->SetPixel(100-x,200-y,RGB(0,255,0));
- if(d<0)
- {
- d+=df1;
- x++;
- y--;
- df2+=ta2;
- df1+=ta2+tb2;
- }
- else
- {
- d+=df2;
- y--;
- df1+=ta2;
- df2+=ta2;
- }
- Sleep(1);
- }
- m_cut=false;
- }
- void CTTXView::OnRed()
- {
- // TODO: Add your command handler code here
- RedrawWindow();
- CPoint *poly;
- CDC* pDC;
- pDC=GetWindowDC();
- poly=new CPoint[5];
- poly[0].x=110;
- poly[0].y = 110; /* 第一个点的x坐标以及y坐标 */
- poly[1].x = 160; /* 第二点 */
- poly[1].y = 105;
- poly[2].x = 200; /* 第三点 */
- poly[2].y = 120;
- poly[3].x=150; /*第四点*/
- poly[3].y=170;
- poly[4].x=110; /*多边形的起点与终点一样*/
- poly[4].y=110;
- pDC->Polyline(poly,5);
- filling(pDC,150,110,RGB(250,0,0),RGB(0,0,0)); /*种子填充多边形*/
- m_cut=false;
- }
- void CTTXView::filling(CDC *pDc, int x,int y,COLORREF c_fill,COLORREF c_bound)
- {
- COLORREF c;
- c=pDc->GetPixel(CPoint(x,y)); /*获取当前点的颜色*/
- if((c!=c_bound)&&(c!=c_fill)) /*如果颜色为边界色则不填充*/
- {
- Sleep(1);
- pDc->SetPixel(x, y, c_fill); /*画点*/
- filling(pDc,x+1,y, c_fill, c_bound);
- filling(pDc,x-1,y, c_fill, c_bound);
- filling(pDc,x, y+1, c_fill, c_bound);
- filling(pDc,x, y-1, c_fill, c_bound);
- }
- m_cut=false;
- }
- void CTTXView::OnGreen()
- {
- // TODO: Add your command handler code here
- RedrawWindow();
- CPoint *poly;
- CDC* pDC;
- pDC=GetWindowDC();
- poly=new CPoint[5];
- poly[0].x=110;
- poly[0].y = 110; /* 第一个点的x坐标以及y坐标 */
- poly[1].x = 160; /* 第二点 */
- poly[1].y = 105;
- poly[2].x = 200; /* 第三点 */
- poly[2].y = 120;
- poly[3].x=150; /*第四点*/
- poly[3].y=170;
- //poly[4].x=110; /*多边形的起点与终点一样*/
- //poly[4].y=110;
- pDC->Polyline(poly,5);
- filling(pDC,150,110,RGB(0,255,0),RGB(0,0,0)); /*种子填充多边形*/
- m_cut=false;
- }
- void CTTXView::Onyellow()
- {
- // TODO: Add your command handler code here
- RedrawWindow();
- CPoint *poly;
- CDC* pDC;
- pDC=GetWindowDC();
- poly=new CPoint[5];
- poly[0].x=110;
- poly[0].y = 110; /* 第一个点的x坐标以及y坐标 */
- poly[1].x = 160; /* 第二点 */
- poly[1].y = 105;
- poly[2].x = 200; /* 第三点 */
- poly[2].y = 120;
- poly[3].x=150; /*第四点*/
- poly[3].y=170;
- poly[4].x=110; /*多边形的起点与终点一样*/
- poly[4].y=110;
- pDC->Polyline(poly,5);
- filling(pDC,150,110,RGB(250,250,0),RGB(0,0,0)); /*种子填充多边形*/
- m_cut=false;
- }
- void CTTXView::encode(int x, int y, int *code)
- {
- int c=0;
- if (x<WL) c=c|LEFT;
- else if (x>WR) c=c|RIGHT;
- if (y<WB) c=c|BOTTOM;
- else if (y>WT) c=c|TOP;
- *code=c;
- }
- int CTTXView::Line(CDC* pDC,int x1, int y1, int x2, int y2)
- {
- // CDC *pDC=GetDC();
- int code1,code2,code,x,y;
- encode(x1,y1,&code1); //(x1,y1)处的编码
- encode(x2,y2,&code2); //(x1,y1)处的编码
- while (code1!=0||code2!=0) //当 code1 不等于 0 或 code2 不等于 0
- {
- if ((code1&code2)!=0) return 0; //当 code1 与 code2 不等于 0,在同侧;
- code=code1;
- if (code1==0) code=code2;
- if ((LEFT&code)!=0) //求交点
- {
- x=WL;
- y=y1+(y2-y1)*(WL-x1)/(x2-x1);
- }
- else if ((RIGHT&code)!=0)
- {
- x=WR;
- y=y1+(y2-y1)*(WR-x1)/(x2-x1);
- }
- else if ((BOTTOM&code)!=0)
- {
- y=WB;
- x=x1+(x2-x1)*(WB-y1)/(y2-y1);
- }
- else if ((TOP&code)!=0)
- {
- y=WT;
- x=x1+(x2-x1)*(WT-y1)/(y2-y1);
- }
- if (code==code1)
- {
- x1=x;y1=y;
- encode(x,y,&code1);
- }
- else
- {
- x2=x;y2=y;
- encode(x,y,&code2);
- }
- }
- //end while,表示 code1,code2 都为 0,其中的线段为可视部分
- pDC->MoveTo(x1,y1);
- pDC->LineTo(x2,y2);
- }
- void CTTXView::Onmcut()
- {
- // TODO: Add your command handler code here
- CDC* pDC;
- pDC=GetWindowDC();
- CPen PenRed(PS_SOLID,1,RGB(255,0,0));//定义红色笔
- pDC->Rectangle(WL,WB,WR,WT);
- m_cut=true;
- }
- void CTTXView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(m_cut)
- {
- CDC *pDC=GetDC();
- pDC->SelectStockObject(NULL_BRUSH);
- if (!m_list) //是起点
- {
- m_p1=m_p2=point; //纪录第一次单击鼠标位置,定起点
- m_list++;
- }
- else
- {
- m_p2=point; //记录第二次单击鼠标的位置,定终点的点
- m_list--; // 为新绘图作准备
- pDC->MoveTo(m_p1.x,m_p1.y); //绘制新直线
- pDC->LineTo(m_p2.x,m_p2.y);
- }
- //用异或方式擦除直线
- int nDrawmode=pDC->SetROP2(R2_NOT);//设置异或绘图模式,并保存原来绘图模式
- pDC->MoveTo(m_p1.x,m_p1.y); //绘制新直线
- pDC->LineTo(m_p2.x,m_p2.y);
- pDC->SetROP2(nDrawmode); //恢复原绘图模式
- //用红色画出裁剪线段
- CPen PenRed(PS_SOLID,1,RGB(255,0,0));//定义红色笔
- pDC->SelectObject(&PenRed);
- Line(pDC,m_p1.x,m_p1.y,m_p2.x,m_p2.y);
- ReleaseDC(pDC); //释放设备环境
- }
- CView::OnLButtonDown(nFlags, point);
- }
- void CTTXView::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(m_cut)
- {
- CDC *pDC=GetDC();
- int nDrawmode=pDC->SetROP2(R2_NOT); //设置异或绘图模式,并保存原来绘图模式
- pDC->SelectStockObject(NULL_BRUSH);
- if(m_list==1)
- {
- CPoint prePnt,curPnt;
- prePnt=m_p2; //获得鼠标所在的前一位置
- curPnt=point;
- //绘制橡皮筋线
- pDC->MoveTo(m_p1.x,m_p1.y);
- pDC->LineTo(prePnt.x,prePnt.y);//用异或模式重复画直线,擦出所画的直线
- pDC->MoveTo(m_p1.x,m_p1.y);
- pDC->LineTo(curPnt.x,curPnt.y);//用当前位置,画直线
- m_p2=point;
- }
- pDC->SetROP2(nDrawmode); //恢复原绘图模式
- ReleaseDC(pDC); //释放设备环境
- }
- CView::OnMouseMove(nFlags, point);
- }
- void CTTXView::OnUpdatemcut(CCmdUI *pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(m_cut);
- }
- void CTTXView::OnCurve()
- {
- // TODO: Add your command handler code here
- SquareBezier(70/2,210/2,140/2,140/2,280/2,280/2,280/2,420/2,50);
- SquareBezier(70/2,210/2,120,280/2,140/2,420/2,280/2,420/2,50);
- //SquareBezier(140/2,420/2,0,560/2,280/2,840/2,560/2,840/2,100);
- }
- void CTTXView::SquareBezier(int x0,int y0,int x1,int y1,int x2,int y2,int x3,int y3,int m)
- {//三次Bezier曲线
- float C[2][4],t,dx,Newx,Newy;
- int Vx,Vy,Nx,Ny,i,j;
- C[0][0]=x0;
- C[0][1]=-3*x0+3*x1;
- C[0][2]=3*x0-6*x1+3*x2;
- C[0][3]=-1*x0+3*x1-3*x2+x3;
- C[1][0]=y0;
- C[1][1]=-3*y0+3*y1;
- C[1][2]=3*y0-6*y1+3*y2;
- C[1][3]=-1*y0+3*y1-3*y2+y3;
- Vx=x0,Vy=y0;
- dx=1.0/m,t=0.0;
- for(i=1;i<=m;i++)
- {t+=dx;
- Newx=C[0][0]+t*(C[0][1]+t*(C[0][2]+t*C[0][3]));
- Newy=C[1][0]+t*(C[1][1]+t*(C[1][2]+t*C[1][3]));
- Nx=(int)(Newx+0.5),Ny=(int)(Newy+0.5);
- MidLine(Vx,Vy,Nx,Ny); //调用中点法画直线
- Vx=Nx,Vy=Ny;
- }
- m_cut=false;
- }
- void CTTXView::MidLine(int x0, int y0, int x1, int y1) //中点法画直线
- {
- int dx,dy,dxy,s1,s2,flag,i,d,x,y,color = 0;
- CDC* pDC = GetDC();
- x = x0;
- y = y0;
- dx = abs(x1 - x0);
- dy = abs(y1 - y0);
- s1 = (x1 - x0) > 0 ? 1:-1;
- s2 = (y1 - y0) > 0 ? 1:-1;
- if(dy > dx)
- {
- dxy = dx;dx = dy;dy = dxy;
- flag = 1;
- }
- else
- flag = 0;
- d = 2*dy - dx;
- for(i = 1;i <= dx;i++)
- {
- pDC->SetPixel(x,y,color);
- pDC->SetPixel(350-y,x+70,color);
- pDC->SetPixel(-x+280,420-y,color);
- pDC->SetPixel(y-70,-x+350,color);
- while(d >= 0)
- {
- if(flag == 1)
- x += s1;
- else
- y += s2;
- d += -2*dx;
- }
- if(flag == 1)
- y += s2;
- else
- x += s1;
- d += 2*dy;
- }
- }