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

CAD

开发平台:

Visual C++

  1. // ARC.cpp: implementation of the CARC class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CAD.h"
  6. #include "ARC.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. CARC::CARC()
  17. {
  18. }
  19. CARC::~CARC()
  20. {
  21. }
  22. void CARC::arc_drawAttribution(CDC *pDC)
  23. {
  24. if(m_arc_save.GetSize())
  25. {
  26. float R;
  27. for(int i=0;i<m_arc_save.GetSize();i++)
  28. {
  29. CPen pen;
  30. pen.CreatePen(((arc_data *)m_arc_save.GetAt(i))->m_line_style,((arc_data *)m_arc_save.GetAt(i))->m_line_width,((arc_data *)m_arc_save.GetAt(i))->m_line_color);
  31. pDC->SelectObject(&pen);
  32. R=sqrt(pow(((arc_data *)m_arc_save.GetAt(i))->endpoint.y-((arc_data *)m_arc_save.GetAt(i))->certrepoint.y,2)+
  33.    pow(((arc_data *)m_arc_save.GetAt(i))->endpoint.x-((arc_data *)m_arc_save.GetAt(i))->certrepoint.x,2));
  34. pDC->Arc(CRect(((arc_data *)m_arc_save.GetAt(i))->certrepoint.x-R,((arc_data *)m_arc_save.GetAt(i))->certrepoint.y-R,
  35. ((arc_data *)m_arc_save.GetAt(i))->certrepoint.x+R,((arc_data *)m_arc_save.GetAt(i))->certrepoint.y+R),
  36. ((arc_data *)m_arc_save.GetAt(i))->startpoint,((arc_data *)m_arc_save.GetAt(i))->endpoint);
  37. pen.DeleteObject();
  38. }
  39. }
  40. }
  41. void CARC::arc_drawMoveline(CPoint m_pointorg,CPoint m_pointfal,CDC *pDC,int m_line_style,int m_line_width,COLORREF m_line_color)
  42. {
  43. CPen pen;
  44. pen.CreatePen(PS_DOT,1,RGB(0,0,255));
  45. pDC->SelectObject(&pen);
  46. pDC->MoveTo(m_pointorg);
  47. pDC->LineTo(m_pointfal);
  48. pen.DeleteObject();
  49. }
  50. void CARC::arc_drawMovearc(CPoint m_pointorg,CPoint m_pointfal,CPoint m_pointend,CDC *pDC,int m_line_style,int m_line_width,COLORREF m_line_color)
  51. {
  52. float R,r,arc1,arc2,arc;
  53. R=sqrt(pow(m_pointend.x-m_pointorg.x,2)+pow(m_pointend.y-m_pointorg.y,2));
  54. r=sqrt(pow(m_pointfal.x-m_pointorg.x,2)+pow(m_pointfal.y-m_pointorg.y,2));
  55. CPoint m_startpoint;
  56. m_startpoint.x=(R*m_pointfal.x)/r-((R-r)*m_pointorg.x)/r;
  57. m_startpoint.y=(R*m_pointfal.y)/r-((R-r)*m_pointorg.y)/r;
  58. arc1=-asin((m_pointfal.y-m_pointorg.y)/r)*180/3.14159265;
  59. if((m_pointfal.y-m_pointorg.y)<=0&&(m_pointfal.x-m_pointorg.x)<=0)
  60. arc1=180-arc1;
  61. else if((m_pointfal.y-m_pointorg.y)>0&&(m_pointfal.x-m_pointorg.x)<=0)
  62. arc1=180-arc1;
  63. else if((m_pointfal.y-m_pointorg.y)>0&&(m_pointfal.x-m_pointorg.x)>0)
  64. arc1=360+arc1;
  65. arc2=-asin((m_pointend.y-m_pointorg.y)/R)*180/3.14159265;
  66. if((m_pointfal.y-m_pointorg.y)<=0&&(m_pointend.x-m_pointorg.x)<=0)
  67. arc2=180-arc2;
  68. else if((m_pointend.y-m_pointorg.y)>0&&(m_pointend.x-m_pointorg.x)<=0)
  69. arc2=180-arc2;
  70. else if((m_pointend.y-m_pointorg.y)>0&&(m_pointend.x-m_pointorg.x)>0)
  71. arc2=360+arc2;
  72. arc=arc2-arc1;
  73. CPen pen;
  74. pen.CreatePen(PS_DOT,1,RGB(0,0,255));
  75. pDC->SelectObject(&pen);
  76. pDC->MoveTo(m_pointorg);
  77. pDC->LineTo(m_startpoint);
  78. pDC->MoveTo(m_pointend);
  79. pDC->LineTo(m_pointorg);
  80. pen.DeleteObject();//辅助线
  81. CString str1,str2;
  82. str1.Format("R:%f",R);
  83. str2.Format("Angle:%f",arc);
  84. pDC->TextOut(m_pointend.x+40,m_pointend.y+40,str1,8);
  85. pDC->TextOut(m_pointend.x+20,m_pointend.y+20,str2,13);
  86. pen.CreatePen(m_line_style,m_line_width,m_line_color);
  87. pDC->SelectObject(&pen);
  88. pDC->Arc(CRect(CPoint(m_pointorg.x-R,m_pointorg.y-R),CPoint(m_pointorg.x+R,m_pointorg.y+R)),
  89. m_startpoint,m_pointend);
  90. pen.DeleteObject();
  91. }
  92. void CARC::arc_drawstore(CPoint m_pointorg,CPoint m_pointfal,CPoint m_pointend,int m_line_style,int m_line_width,COLORREF m_line_color)
  93. {
  94. float R,r;
  95. R=sqrt(pow(m_pointend.x-m_pointorg.x,2)+pow(m_pointend.y-m_pointorg.y,2));
  96. r=sqrt(pow(m_pointfal.x-m_pointorg.x,2)+pow(m_pointfal.y-m_pointorg.y,2));
  97. CPoint m_startpoint;
  98. m_startpoint.x=(R*m_pointfal.x)/r-((R-r)*m_pointorg.x)/r;
  99. m_startpoint.y=(R*m_pointfal.y)/r-((R-r)*m_pointorg.y)/r;
  100. arc_data *parc_data=new arc_data(m_pointorg,m_startpoint,m_pointend,m_line_style,m_line_width,m_line_color);
  101. m_arc_save.Add(parc_data);
  102. }
  103.  void CARC::arc_catchend(CPoint point,CRgn* prgn,CDC *pDC)
  104.  {
  105.   if(m_arc_save.GetSize())
  106.   for(int i=0;i<m_arc_save.GetSize();i++)
  107.   {
  108. if(prgn->PtInRegion(((arc_data*)m_arc_save.GetAt(i))->startpoint))
  109. arc_fillpoint(((arc_data*)m_arc_save.GetAt(i))->startpoint,pDC);
  110.  
  111.   if(prgn->PtInRegion(((arc_data*)m_arc_save.GetAt(i))->endpoint))
  112.   arc_fillpoint(((arc_data*)m_arc_save.GetAt(i))->endpoint,pDC);
  113.  
  114.   }
  115.  
  116.  
  117.  }
  118. void CARC::arc_catchcertre(CPoint point,CRgn* prgn,CDC *pDC)
  119. {
  120. if(m_arc_save.GetSize())
  121. for(int i=0;i<m_arc_save.GetSize();i++)
  122. {
  123. if(prgn->PtInRegion(((arc_data*)m_arc_save.GetAt(i))->certrepoint))
  124. arc_fillpoint(((arc_data*)m_arc_save.GetAt(i))->certrepoint,pDC);
  125. }
  126. }
  127. void CARC::arc_fillpoint(CPoint point,CDC *pDC)
  128. {
  129. CBrush brush;
  130. brush.CreateSolidBrush( RGB(0,255,255));
  131. pDC->SelectObject(&brush);
  132. pDC->Rectangle(CRect(point.x-4,point.y-4,point.x+4,point.y+4));
  133. brush.DeleteObject();
  134. }
  135. void CARC::arc_searchend(CPoint point,CPoint *pnearpoint,CRgn *prgn)
  136. {
  137. CPoint nearpoint;
  138. nearpoint=*pnearpoint;
  139. float nearrest;
  140. nearrest=sqrt(pow(point.x-nearpoint.x,2)+pow(point.y-nearpoint.y,2));
  141. if(m_arc_save.GetSize())
  142. for(int i=0;i<m_arc_save.GetSize();i++)
  143. {
  144. if(prgn->PtInRegion(((arc_data*)m_arc_save.GetAt(i))->startpoint))
  145. {
  146. float length;
  147. length=sqrt(pow(((arc_data*)m_arc_save.GetAt(i))->startpoint.x-point.x,2)+
  148. pow(((arc_data*)m_arc_save.GetAt(i))->startpoint.y-point.y,2));
  149. if(length<nearrest)
  150. nearpoint=((arc_data*)m_arc_save.GetAt(i))->startpoint;
  151. }
  152. if(prgn->PtInRegion(((arc_data*)m_arc_save.GetAt(i))->endpoint))
  153. {
  154. float length;
  155. length=sqrt(pow(((arc_data*)m_arc_save.GetAt(i))->endpoint.x-point.x,2)+
  156. pow(((arc_data*)m_arc_save.GetAt(i))->endpoint.y-point.y,2));
  157. if(length<nearrest)
  158. nearpoint=((arc_data*)m_arc_save.GetAt(i))->endpoint;
  159. }
  160. }
  161. *pnearpoint=nearpoint;
  162. }
  163. void CARC::arc_searchcertre(CPoint point,CPoint *pnearpoint,CRgn *prgn)
  164. {
  165. CPoint nearpoint;
  166. nearpoint=*pnearpoint;
  167. float nearrest;
  168. nearrest=sqrt(pow(point.x-nearpoint.x,2)+pow(point.y-nearpoint.y,2));
  169. if(m_arc_save.GetSize())
  170. for(int i=0;i<m_arc_save.GetSize();i++)
  171. if(prgn->PtInRegion(((arc_data*)m_arc_save.GetAt(i))->certrepoint))
  172. {
  173. float length;
  174. length=sqrt(pow(((arc_data*)m_arc_save.GetAt(i))->certrepoint.x-point.x,2)+
  175. pow(((arc_data*)m_arc_save.GetAt(i))->certrepoint.y-point.y,2));
  176. if(length<nearrest)
  177. nearpoint=((arc_data*)m_arc_save.GetAt(i))->certrepoint;
  178. }
  179. *pnearpoint=nearpoint;
  180. }
  181. bool CARC::arc_searcharc(CPoint point,int *pNum,int *pnStyle,CDC*pmdc)
  182. {
  183. if(m_arc_save.GetSize())
  184. for(int i=0;i<m_arc_save.GetSize();i++)
  185. {
  186. float r,R;
  187. r=sqrt(pow(((arc_data*)m_arc_save.GetAt(i))->certrepoint.x-point.x,2)+
  188. pow(((arc_data*)m_arc_save.GetAt(i))->certrepoint.y-point.y,2));
  189. R=sqrt(pow(((arc_data*)m_arc_save.GetAt(i))->certrepoint.x-((arc_data*)m_arc_save.GetAt(i))->startpoint.x,2)+
  190. pow(((arc_data*)m_arc_save.GetAt(i))->certrepoint.y-((arc_data*)m_arc_save.GetAt(i))->startpoint.y,2));
  191. //CRgn rgn;
  192. //rgn.CreateRectRgn(((arc_data*)m_arc_save.GetAt(i))->startpoint.x,((arc_data*)m_arc_save.GetAt(i))->startpoint.y,
  193. // ((arc_data*)m_arc_save.GetAt(i))->endpoint.x,((arc_data*)m_arc_save.GetAt(i))->endpoint.y);
  194. if(fabs(r-R)<5)
  195. {
  196. arc_fillpoint(((arc_data*)m_arc_save.GetAt(i))->startpoint,pmdc);
  197. arc_fillpoint(((arc_data*)m_arc_save.GetAt(i))->endpoint,pmdc);
  198. arc_fillpoint(((arc_data*)m_arc_save.GetAt(i))->certrepoint,pmdc);
  199. *pNum=i;
  200. *pnStyle=7;
  201. return(true);
  202. }
  203. }
  204. return(false);
  205. }
  206. bool CARC::arc_editselect(CPoint point,CPoint *ppointorg,int Num)
  207. {
  208. CPoint certrepoint;
  209. certrepoint=((arc_data*)m_arc_save.GetAt(Num))->certrepoint;
  210. CRgn rgn;
  211. rgn.CreateRectRgn(certrepoint.x-10,certrepoint.y-10,certrepoint.x+10,certrepoint.y+10);
  212. if(rgn.PtInRegion(point))
  213.  *ppointorg=certrepoint;
  214. return(true);
  215. }
  216. else
  217. return(false);
  218. }
  219. void CARC::arc_editmove(CPoint pointorg,CPoint pointfal,int Num,CDC*pDC)
  220. {
  221. CSize pointmove;
  222. pointmove=pointfal-pointorg;
  223. CPoint certrepoint,startpoint,endpoint;
  224. certrepoint=((arc_data*)m_arc_save.GetAt(Num))->certrepoint+pointmove;
  225. startpoint=((arc_data*)m_arc_save.GetAt(Num))->startpoint+pointmove;
  226. endpoint=((arc_data*)m_arc_save.GetAt(Num))->endpoint+pointmove;
  227. float R;
  228. R=sqrt(pow(endpoint.x-certrepoint.x,2)+pow(endpoint.y-certrepoint.y,2));
  229. CPen pen;
  230. pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  231. pDC->SelectObject(&pen);
  232. pDC->Arc(CRect(CPoint(certrepoint.x-R,certrepoint.y-R),CPoint(certrepoint.x+R,certrepoint.y+R)),startpoint,endpoint);
  233. pen.DeleteObject();
  234. }
  235. void CARC::arc_editmovestore(CPoint pointorg,CPoint pointfal,int Num)
  236. {
  237. CSize pointmove;
  238. pointmove=pointfal-pointorg;
  239. ((arc_data*)m_arc_save.GetAt(Num))->certrepoint+=pointmove;
  240. ((arc_data*)m_arc_save.GetAt(Num))->startpoint+=pointmove;
  241. ((arc_data*)m_arc_save.GetAt(Num))->endpoint+=pointmove;
  242. }
  243. void CARC::arc_del(int Num)
  244. {
  245. arc_data *pdel=(arc_data*)m_arc_save.GetAt(Num);
  246. if(pdel!=NULL)
  247. {
  248. m_arc_save.RemoveAt(Num);
  249. delete pdel;
  250. }
  251. }
  252. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  253. CARC::arc_data::arc_data(CPoint certrepoint,CPoint startpoint,CPoint endpoint,int m_line_style,int m_line_width,COLORREF m_line_color)
  254. {
  255. this->certrepoint=certrepoint;
  256. this->startpoint=startpoint;
  257. this->endpoint=endpoint;
  258. this->m_line_style=m_line_style;
  259. this->m_line_width=m_line_width;
  260. this->m_line_color=m_line_color;
  261. }
  262. CARC::arc_data::~arc_data()
  263. {
  264. }