LINE.cpp
上传用户:shangwu01
上传日期:2013-04-22
资源大小:707k
文件大小:10k
源码类别:

CAD

开发平台:

Visual C++

  1. // LINE.cpp: implementation of the CLINE class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CAD.h"
  6. #include "LINE.h"
  7. #include "math.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CLINE::CLINE()
  17. {
  18. }
  19. CLINE::~CLINE()
  20. {
  21. }
  22. void CLINE::Line_drawMove(CPoint m_pointorg,CPoint m_pointfal,CDC *pDC,int m_line_style,int m_line_width,COLORREF m_line_color)
  23. {
  24. CPen pen;
  25. pen.CreatePen(m_line_style,m_line_width,m_line_color);
  26. pDC->SelectObject(&pen);
  27. pDC->MoveTo(m_pointorg);
  28. pDC->LineTo(m_pointfal);
  29. pen.DeleteObject();//直线输出
  30. pen.CreatePen(PS_DOT,1,RGB(0,0,255));
  31. pDC->SelectObject(&pen);
  32. pDC->MoveTo(CPoint(m_pointorg.x-100,m_pointorg.y));
  33. pDC->LineTo(CPoint(m_pointorg.x+100,m_pointorg.y));
  34. pDC->MoveTo(CPoint(m_pointorg.x,m_pointorg.y-100));
  35. pDC->LineTo(CPoint(m_pointorg.x,m_pointorg.y+100));
  36. pen.DeleteObject();//辅助线输出
  37. double sum1,sum2;
  38. CString str1,str2;
  39. sum1=sqrt(pow(m_pointorg.x-m_pointfal.x,2)+pow(m_pointorg.y-m_pointfal.y,2));
  40. sum2=-asin((m_pointfal.y-m_pointorg.y)/sum1)*180/3.14159265;
  41. if((m_pointfal.y-m_pointorg.y)<=0&&(m_pointfal.x-m_pointorg.x)<=0)
  42. sum2=180-sum2;
  43. else if((m_pointfal.y-m_pointorg.y)>0&&(m_pointfal.x-m_pointorg.x)<=0)
  44. sum2=180-sum2;
  45. else if((m_pointfal.y-m_pointorg.y)>0&&(m_pointfal.x-m_pointorg.x)>0)
  46. sum2=360+sum2;
  47. str1.Format("length:%f",sum1);
  48. str2.Format("Angle:%f",sum2);
  49. pDC->TextOut((m_pointfal.x+m_pointorg.x)/2-6,(m_pointfal.y+m_pointorg.y)/2-6,str2,13);
  50. pDC->TextOut((m_pointfal.x+m_pointorg.x)/2+10,(m_pointfal.y+m_pointorg.y)/2+10,str1,13);
  51. //文本输出
  52. }
  53. void CLINE::Line_drawAttribution(CDC *pDC)
  54. {
  55. if(m_line_save.GetSize())
  56. for(int i=0;i<m_line_save.GetSize();i++)
  57. {
  58. CPen pen;
  59. pen.CreatePen(((line_data*)m_line_save.GetAt(i))->m_line_style,
  60. ((line_data*)m_line_save.GetAt(i))->m_line_width,
  61. ((line_data*)m_line_save.GetAt(i))->m_line_color);
  62. pDC->SelectObject(&pen);
  63. pDC->MoveTo(((line_data*)m_line_save.GetAt(i))->m_pointorg);
  64. pDC->LineTo(((line_data*)m_line_save.GetAt(i))->m_pointfal);
  65. }
  66. }
  67. void CLINE::Line_store(CPoint m_pointorg,CPoint m_pointfal,int m_line_style,int m_line_width,COLORREF m_line_color)
  68. {
  69. line_data *pline_data=new line_data(m_pointorg,m_pointfal,m_line_style,m_line_width,m_line_color);
  70. m_line_save.Add(pline_data);//储存数据
  71. }
  72. void CLINE::Line_catchend(CPoint point,CRgn* prgn,CDC *pDC)
  73. {
  74. if(m_line_save.GetSize())
  75. for(int i=0;i<m_line_save.GetSize();i++)
  76. {
  77. if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointorg))
  78. Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointorg,pDC);
  79. if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointfal))
  80. Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointfal,pDC);
  81. }
  82. }
  83. void CLINE::Line_catchmiddle(CPoint point,CRgn* prgn,CDC *pDC)
  84. {
  85. if(m_line_save.GetSize())
  86. for(int i=0;i<m_line_save.GetSize();i++)
  87. {
  88. CPoint middlepoint;
  89. middlepoint.x=(((line_data*)m_line_save.GetAt(i))->m_pointorg.x+((line_data*)m_line_save.GetAt(i))->m_pointfal.x)/2;
  90. middlepoint.y=(((line_data*)m_line_save.GetAt(i))->m_pointorg.y+((line_data*)m_line_save.GetAt(i))->m_pointfal.y)/2;
  91. if(prgn->PtInRegion(middlepoint))
  92. Line_fillpoint(middlepoint,pDC);
  93. }
  94. }
  95. void CLINE::Line_fillpoint(CPoint point,CDC *pDC)
  96. {
  97. CBrush brush;
  98. brush.CreateSolidBrush( RGB(0,255,255));
  99. pDC->SelectObject(&brush);
  100. pDC->Rectangle(CRect(point.x-4,point.y-4,point.x+4,point.y+4));
  101. brush.DeleteObject();
  102. }
  103. void CLINE::Line_searchend(CPoint point,CPoint *pnearpoint,CRgn* prgn)
  104. {
  105. CPoint nearpoint;
  106. nearpoint=*pnearpoint;
  107. float nearrest;
  108. nearrest=sqrt(pow(point.x-nearpoint.x,2)+pow(point.y-nearpoint.y,2));
  109.  
  110. if(m_line_save.GetSize())
  111. for(int i=0;i<m_line_save.GetSize();i++)
  112. {
  113. if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointorg))
  114. {
  115. float length;
  116. length=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.x-point.x,2)+
  117. pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.y-point.y,2));
  118. if(length<nearrest)
  119. nearpoint=((line_data*)m_line_save.GetAt(i))->m_pointorg;
  120. }
  121. if(prgn->PtInRegion(((line_data*)m_line_save.GetAt(i))->m_pointfal))
  122. {
  123. float length;
  124. length=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.x-point.x,2)+
  125. pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.y-point.y,2));
  126. if(length<nearrest)
  127. nearpoint=((line_data*)m_line_save.GetAt(i))->m_pointfal;
  128. }
  129. }
  130. *pnearpoint=nearpoint;
  131. }
  132. void CLINE::Line_searchmiddle(CPoint point,CPoint *pnearpoint,CRgn* prgn)
  133. {
  134. CPoint nearpoint;
  135. nearpoint=*pnearpoint;
  136. float nearrest;
  137. nearrest=sqrt(pow(point.x-nearpoint.x,2)+pow(point.y-nearpoint.y,2));
  138. if(m_line_save.GetSize())
  139. for(int i=0;i<m_line_save.GetSize();i++)
  140. {
  141. CPoint middlepoint;
  142. middlepoint.x=(((line_data*)m_line_save.GetAt(i))->m_pointorg.x+((line_data*)m_line_save.GetAt(i))->m_pointfal.x)/2;
  143. middlepoint.y=(((line_data*)m_line_save.GetAt(i))->m_pointorg.y+((line_data*)m_line_save.GetAt(i))->m_pointfal.y)/2;
  144. if(prgn->PtInRegion(middlepoint))
  145. {
  146. float length;
  147. length=sqrt(pow(middlepoint.x-point.x,2)+pow(middlepoint.y-point.y,2));
  148. if(length<nearrest)
  149. nearpoint=middlepoint;
  150. }
  151. }
  152. *pnearpoint=nearpoint;
  153. }
  154. bool CLINE::Line_searchline(CPoint point,int *pNum,int *pnStyle,CDC *pmdc)
  155. {
  156. if(m_line_save.GetSize())
  157. for(int i=0;i<m_line_save.GetSize();i++)
  158. {
  159. float length,length1,length2;
  160. length=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.x-((line_data*)m_line_save.GetAt(i))->m_pointorg.x,2)+
  161. pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.y-((line_data*)m_line_save.GetAt(i))->m_pointorg.y,2));
  162. length1=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.x-point.x,2)+
  163. pow(((line_data*)m_line_save.GetAt(i))->m_pointfal.y-point.y,2));
  164. length2=sqrt(pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.x-point.x,2)+
  165. pow(((line_data*)m_line_save.GetAt(i))->m_pointorg.y-point.y,2));
  166. if(length2+length1-length<1)
  167. {
  168. CPoint middlepoint;
  169. middlepoint.x=(((line_data*)m_line_save.GetAt(i))->m_pointorg.x+((line_data*)m_line_save.GetAt(i))->m_pointfal.x)/2;
  170. middlepoint.y=(((line_data*)m_line_save.GetAt(i))->m_pointorg.y+((line_data*)m_line_save.GetAt(i))->m_pointfal.y)/2;
  171. Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointfal,pmdc);
  172. Line_fillpoint(((line_data*)m_line_save.GetAt(i))->m_pointorg,pmdc);
  173. Line_fillpoint(middlepoint,pmdc);
  174. *pNum=i;
  175. *pnStyle=1;
  176. return(true);
  177. }
  178. }
  179. return(false);
  180. }
  181. bool CLINE::Line_editselect(CPoint point,CPoint* ppointorg,int Num,int *pselect)
  182. {
  183. CPoint pointorg,pointfal,middlepoint;
  184. pointorg=((line_data*)m_line_save.GetAt(Num))->m_pointorg;
  185. pointfal=((line_data*)m_line_save.GetAt(Num))->m_pointfal;
  186. middlepoint.x=(((line_data*)m_line_save.GetAt(Num))->m_pointorg.x+((line_data*)m_line_save.GetAt(Num))->m_pointfal.x)/2;
  187. middlepoint.y=(((line_data*)m_line_save.GetAt(Num))->m_pointorg.y+((line_data*)m_line_save.GetAt(Num))->m_pointfal.y)/2;
  188. CRgn rgnorg,rgnfal,rgnmid;
  189. rgnorg.CreateRectRgn(pointorg.x-10,pointorg.y-10,pointorg.x+10,pointorg.y+10);
  190. rgnfal.CreateRectRgn(pointfal.x-10,pointfal.y-10,pointfal.x+10,pointfal.y+10);
  191. rgnmid.CreateRectRgn(middlepoint.x-10,middlepoint.y-10,middlepoint.x+10,middlepoint.y+10);
  192. if(rgnorg.PtInRegion(point))
  193. {
  194. *ppointorg=pointfal;
  195. *pselect=1;
  196. return(true);
  197. }
  198. else if(rgnfal.PtInRegion(point))
  199. {
  200. *ppointorg=pointorg;
  201. *pselect=2;
  202. return(true);
  203. }
  204. else if(rgnmid.PtInRegion(point))
  205. {
  206. *ppointorg=middlepoint;
  207. *pselect=3;
  208. return(true);
  209. }
  210. else
  211. return(false);
  212. }
  213. void CLINE::Line_editdraw(CPoint m_pointorg,CPoint m_pointfal,CDC* pDC)
  214. {
  215. CPen pen;
  216. pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  217. pDC->SelectObject(&pen);
  218. pDC->MoveTo(m_pointorg);
  219. pDC->LineTo(m_pointfal);
  220. pen.DeleteObject();
  221. }
  222. void CLINE::Line_editstore(CPoint pointorg,CPoint pointfal,int Num)
  223. {
  224. ((line_data*)m_line_save.GetAt(Num))->m_pointorg=pointorg;
  225. ((line_data*)m_line_save.GetAt(Num))->m_pointfal=pointfal;
  226. }
  227. void CLINE::Line_editmove(CPoint pointorg,CPoint pointfal,int Num,CDC *pDC)
  228. {
  229. CSize pointmove;
  230. pointmove=pointfal-pointorg;
  231. CPen pen;
  232. pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  233. pDC->SelectObject(&pen);
  234. pDC->MoveTo(((line_data*)m_line_save.GetAt(Num))->m_pointorg+pointmove);
  235. pDC->LineTo(((line_data*)m_line_save.GetAt(Num))->m_pointfal+pointmove);
  236. pen.DeleteObject();
  237. }
  238. void CLINE::Line_editmovestore(CPoint pointorg,CPoint pointfal,int Num)
  239. {
  240. CSize pointmove;
  241. pointmove=pointfal-pointorg;
  242. ((line_data*)m_line_save.GetAt(Num))->m_pointorg+=pointmove;
  243. ((line_data*)m_line_save.GetAt(Num))->m_pointfal+=pointmove;
  244. }
  245. void CLINE::Line_del(int Num)
  246. {
  247. line_data *pdel=(line_data*)m_line_save.GetAt(Num);
  248. if(pdel!=NULL)
  249. {
  250. m_line_save.RemoveAt(Num);
  251. delete pdel;
  252. }
  253. }
  254. ///////////////////////line_save//////////////////////////
  255. CLINE::line_data::line_data(CPoint m_pointorg,CPoint m_pointfal,int m_line_style,int m_line_width,COLORREF m_line_color)
  256. {
  257. this->m_pointfal=m_pointfal;
  258. this->m_pointorg=m_pointorg;
  259. this->m_line_style=m_line_style;
  260. this->m_line_width=m_line_width;
  261. this->m_line_color=m_line_color;
  262. }
  263. CLINE::line_data::~line_data()
  264. {
  265. }