LINE.cpp
上传用户:shangwu01
上传日期:2013-04-22
资源大小:707k
文件大小:10k
- // LINE.cpp: implementation of the CLINE class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CAD.h"
- #include "LINE.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLINE::CLINE()
- {
-
- }
- CLINE::~CLINE()
- {
-
- }
- void CLINE::Line_drawMove(CPoint m_pointorg,CPoint m_pointfal,CDC *pDC,int m_line_style,int m_line_width,COLORREF m_line_color)
- {
- CPen pen;
- pen.CreatePen(m_line_style,m_line_width,m_line_color);
- pDC->SelectObject(&pen);
- pDC->MoveTo(m_pointorg);
- pDC->LineTo(m_pointfal);
- pen.DeleteObject();//直线输出
- pen.CreatePen(PS_DOT,1,RGB(0,0,255));
- pDC->SelectObject(&pen);
- pDC->MoveTo(CPoint(m_pointorg.x-100,m_pointorg.y));
- pDC->LineTo(CPoint(m_pointorg.x+100,m_pointorg.y));
- pDC->MoveTo(CPoint(m_pointorg.x,m_pointorg.y-100));
- pDC->LineTo(CPoint(m_pointorg.x,m_pointorg.y+100));
- pen.DeleteObject();//辅助线输出
- double sum1,sum2;
- CString str1,str2;
- sum1=sqrt(pow(m_pointorg.x-m_pointfal.x,2)+pow(m_pointorg.y-m_pointfal.y,2));
- sum2=-asin((m_pointfal.y-m_pointorg.y)/sum1)*180/3.14159265;
- if((m_pointfal.y-m_pointorg.y)<=0&&(m_pointfal.x-m_pointorg.x)<=0)
- sum2=180-sum2;
- else if((m_pointfal.y-m_pointorg.y)>0&&(m_pointfal.x-m_pointorg.x)<=0)
- sum2=180-sum2;
- else if((m_pointfal.y-m_pointorg.y)>0&&(m_pointfal.x-m_pointorg.x)>0)
- sum2=360+sum2;
-
- str1.Format("length:%f",sum1);
- str2.Format("Angle:%f",sum2);
- pDC->TextOut((m_pointfal.x+m_pointorg.x)/2-6,(m_pointfal.y+m_pointorg.y)/2-6,str2,13);
-
- pDC->TextOut((m_pointfal.x+m_pointorg.x)/2+10,(m_pointfal.y+m_pointorg.y)/2+10,str1,13);
- //文本输出
- }
- void CLINE::Line_drawAttribution(CDC *pDC)
- {
- if(m_line_save.GetSize())
- for(int i=0;i<m_line_save.GetSize();i++)
- {
- CPen pen;
- pen.CreatePen(((line_data*)m_line_save.GetAt(i))->m_line_style,
- ((line_data*)m_line_save.GetAt(i))->m_line_width,
- ((line_data*)m_line_save.GetAt(i))->m_line_color);
- pDC->SelectObject(&pen);
- pDC->MoveTo(((line_data*)m_line_save.GetAt(i))->m_pointorg);
- pDC->LineTo(((line_data*)m_line_save.GetAt(i))->m_pointfal);
- }
- }
- void CLINE::Line_store(CPoint m_pointorg,CPoint m_pointfal,int m_line_style,int m_line_width,COLORREF m_line_color)
- {
- line_data *pline_data=new line_data(m_pointorg,m_pointfal,m_line_style,m_line_width,m_line_color);
- m_line_save.Add(pline_data);//储存数据
- }
- void CLINE::Line_catchend(CPoint point,CRgn* prgn,CDC *pDC)
- {
- if(m_line_save.GetSize())
- for(int i=0;i<m_line_save.GetSize();i++)
- {
- if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointorg))
- Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointorg,pDC);
- if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointfal))
- Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointfal,pDC);
-
- }
- }
- void CLINE::Line_catchmiddle(CPoint point,CRgn* prgn,CDC *pDC)
- {
-
- if(m_line_save.GetSize())
- for(int i=0;i<m_line_save.GetSize();i++)
- {
- CPoint middlepoint;
- middlepoint.x=(((line_data*)m_line_save.GetAt(i))->m_pointorg.x+((line_data*)m_line_save.GetAt(i))->m_pointfal.x)/2;
- middlepoint.y=(((line_data*)m_line_save.GetAt(i))->m_pointorg.y+((line_data*)m_line_save.GetAt(i))->m_pointfal.y)/2;
- if(prgn->PtInRegion(middlepoint))
- Line_fillpoint(middlepoint,pDC);
- }
- }
- void CLINE::Line_fillpoint(CPoint point,CDC *pDC)
- {
- CBrush brush;
- brush.CreateSolidBrush( RGB(0,255,255));
- pDC->SelectObject(&brush);
- pDC->Rectangle(CRect(point.x-4,point.y-4,point.x+4,point.y+4));
- brush.DeleteObject();
- }
- void CLINE::Line_searchend(CPoint point,CPoint *pnearpoint,CRgn* prgn)
- {
- CPoint nearpoint;
- nearpoint=*pnearpoint;
- float nearrest;
- nearrest=sqrt(pow(point.x-nearpoint.x,2)+pow(point.y-nearpoint.y,2));
-
-
- if(m_line_save.GetSize())
- for(int i=0;i<m_line_save.GetSize();i++)
- {
- if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointorg))
- {
-
- float length;
- length=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.x-point.x,2)+
- pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.y-point.y,2));
- if(length<nearrest)
- nearpoint=((line_data*)m_line_save.GetAt(i))->m_pointorg;
-
- }
-
- if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointfal))
- {
- float length;
- length=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.x-point.x,2)+
- pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.y-point.y,2));
- if(length<nearrest)
- nearpoint=((line_data*)m_line_save.GetAt(i))->m_pointfal;
- }
-
- }
- *pnearpoint=nearpoint;
-
- }
- void CLINE::Line_searchmiddle(CPoint point,CPoint *pnearpoint,CRgn* prgn)
- {
- CPoint nearpoint;
- nearpoint=*pnearpoint;
- float nearrest;
- nearrest=sqrt(pow(point.x-nearpoint.x,2)+pow(point.y-nearpoint.y,2));
-
- if(m_line_save.GetSize())
- for(int i=0;i<m_line_save.GetSize();i++)
- {
- CPoint middlepoint;
- middlepoint.x=(((line_data*)m_line_save.GetAt(i))->m_pointorg.x+((line_data*)m_line_save.GetAt(i))->m_pointfal.x)/2;
- middlepoint.y=(((line_data*)m_line_save.GetAt(i))->m_pointorg.y+((line_data*)m_line_save.GetAt(i))->m_pointfal.y)/2;
- if(prgn->PtInRegion(middlepoint))
- {
- float length;
- length=sqrt(pow(middlepoint.x-point.x,2)+pow(middlepoint.y-point.y,2));
- if(length<nearrest)
- nearpoint=middlepoint;
- }
- }
- *pnearpoint=nearpoint;
- }
- bool CLINE::Line_searchline(CPoint point,int *pNum,int *pnStyle,CDC *pmdc)
- {
- if(m_line_save.GetSize())
- for(int i=0;i<m_line_save.GetSize();i++)
- {
- float length,length1,length2;
- length=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.x-((line_data*)m_line_save.GetAt(i))->m_pointorg.x,2)+
- pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.y-((line_data*)m_line_save.GetAt(i))->m_pointorg.y,2));
- length1=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.x-point.x,2)+
- pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.y-point.y,2));
- length2=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.x-point.x,2)+
- pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.y-point.y,2));
- if(length2+length1-length<1)
- {
- CPoint middlepoint;
- middlepoint.x=(((line_data*)m_line_save.GetAt(i))->m_pointorg.x+((line_data*)m_line_save.GetAt(i))->m_pointfal.x)/2;
- middlepoint.y=(((line_data*)m_line_save.GetAt(i))->m_pointorg.y+((line_data*)m_line_save.GetAt(i))->m_pointfal.y)/2;
-
- Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointfal,pmdc);
- Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointorg,pmdc);
- Line_fillpoint(middlepoint,pmdc);
-
- *pNum=i;
- *pnStyle=1;
- return(true);
- }
-
- }
- return(false);
-
- }
- bool CLINE::Line_editselect(CPoint point,CPoint* ppointorg,int Num,int *pselect)
- {
- CPoint pointorg,pointfal,middlepoint;
- pointorg=((line_data*)m_line_save.GetAt(Num))->m_pointorg;
- pointfal=((line_data*)m_line_save.GetAt(Num))->m_pointfal;
- middlepoint.x=(((line_data*)m_line_save.GetAt(Num))->m_pointorg.x+((line_data*)m_line_save.GetAt(Num))->m_pointfal.x)/2;
- middlepoint.y=(((line_data*)m_line_save.GetAt(Num))->m_pointorg.y+((line_data*)m_line_save.GetAt(Num))->m_pointfal.y)/2;
-
- CRgn rgnorg,rgnfal,rgnmid;
- rgnorg.CreateRectRgn(pointorg.x-10,pointorg.y-10,pointorg.x+10,pointorg.y+10);
- rgnfal.CreateRectRgn(pointfal.x-10,pointfal.y-10,pointfal.x+10,pointfal.y+10);
- rgnmid.CreateRectRgn(middlepoint.x-10,middlepoint.y-10,middlepoint.x+10,middlepoint.y+10);
- if(rgnorg.PtInRegion(point))
- {
- *ppointorg=pointfal;
- *pselect=1;
- return(true);
- }
- else if(rgnfal.PtInRegion(point))
- {
-
- *ppointorg=pointorg;
- *pselect=2;
- return(true);
- }
- else if(rgnmid.PtInRegion(point))
- {
- *ppointorg=middlepoint;
- *pselect=3;
- return(true);
- }
- else
- return(false);
-
-
- }
- void CLINE::Line_editdraw(CPoint m_pointorg,CPoint m_pointfal,CDC* pDC)
- {
- CPen pen;
- pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
- pDC->SelectObject(&pen);
- pDC->MoveTo(m_pointorg);
- pDC->LineTo(m_pointfal);
- pen.DeleteObject();
- }
- void CLINE::Line_editstore(CPoint pointorg,CPoint pointfal,int Num)
- {
- ((line_data*)m_line_save.GetAt(Num))->m_pointorg=pointorg;
- ((line_data*)m_line_save.GetAt(Num))->m_pointfal=pointfal;
- }
- void CLINE::Line_editmove(CPoint pointorg,CPoint pointfal,int Num,CDC *pDC)
- {
- CSize pointmove;
- pointmove=pointfal-pointorg;
- CPen pen;
- pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
- pDC->SelectObject(&pen);
- pDC->MoveTo(((line_data*)m_line_save.GetAt(Num))->m_pointorg+pointmove);
- pDC->LineTo(((line_data*)m_line_save.GetAt(Num))->m_pointfal+pointmove);
- pen.DeleteObject();
- }
- void CLINE::Line_editmovestore(CPoint pointorg,CPoint pointfal,int Num)
- {
- CSize pointmove;
- pointmove=pointfal-pointorg;
- ((line_data*)m_line_save.GetAt(Num))->m_pointorg+=pointmove;
- ((line_data*)m_line_save.GetAt(Num))->m_pointfal+=pointmove;
- }
- void CLINE::Line_del(int Num)
- {
- line_data *pdel=(line_data*)m_line_save.GetAt(Num);
- if(pdel!=NULL)
- {
- m_line_save.RemoveAt(Num);
- delete pdel;
- }
- }
- ///////////////////////line_save//////////////////////////
- CLINE::line_data::line_data(CPoint m_pointorg,CPoint m_pointfal,int m_line_style,int m_line_width,COLORREF m_line_color)
- {
- this->m_pointfal=m_pointfal;
- this->m_pointorg=m_pointorg;
- this->m_line_style=m_line_style;
- this->m_line_width=m_line_width;
- this->m_line_color=m_line_color;
- }
- CLINE::line_data::~line_data()
- {
- }