CEllipseArc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #include"stdafx.h"
  2. #include"CEllipseArc.h"
  3. #include "EastDrawView.h"
  4. IMPLEMENT_SERIAL(CEllipseArc,CUnit,1)
  5. CEllipseArc::CEllipseArc()
  6. {
  7. CUnit::Initial();
  8. m_IsDrawingArc=false;
  9. m_IsCirCu=false;
  10. }
  11. void CEllipseArc::DrawActiveStepOne(CDC *pDC,CPoint movingPoint)
  12. {
  13. m_MovingPoint=movingPoint;
  14. this->ShowMovingLine(pDC,this->m_FirstPoint,this->m_SecondPoint);
  15. if(m_Xr!=0)
  16. this->ShowMovingLine(pDC,movingPoint,m_CenterPoint);
  17. this->ellipseMidpoint(pDC,this->m_CenterPoint.x,this->m_CenterPoint.y,this->m_Xr,this->m_Yr);
  18. }
  19. int CEllipseArc::ComputRadiusX(CPoint firstPoint, CPoint secondPoint)
  20. {
  21. m_Xr=sqrt((firstPoint.x-secondPoint.x)*(firstPoint.x-secondPoint.x)+(firstPoint.y-secondPoint.y)*(firstPoint.y-secondPoint.y));
  22. return 0;
  23. }
  24. int CEllipseArc::ComputRadiusY(CPoint firstPoint, CPoint secondPoint)
  25. {
  26. m_Yr=sqrt((firstPoint.x-secondPoint.x)*(firstPoint.x-secondPoint.x)+(firstPoint.y-secondPoint.y)*(firstPoint.y-secondPoint.y));
  27. m_Xr=0;
  28. return 0;
  29. }
  30. float CEllipseArc::ComputSloap(CPoint firstPoint, CPoint secondPoint)
  31. {
  32. m_SecondPoint=secondPoint;
  33. double sloap;
  34. if(firstPoint.x==secondPoint.x)
  35. {
  36. sloap=9999999999999.0;
  37. }
  38. else
  39. {
  40. sloap=double((firstPoint.y-secondPoint.y))/double((firstPoint.x-secondPoint.x));
  41. }
  42. if(sloap<=0)
  43. {
  44. m_Cos=fabs(sloap/sqrt(sloap*sloap+1.0));
  45. m_Sin=1.0/sqrt(sloap*sloap+1.0);
  46. }
  47. else
  48. {
  49. m_Cos=fabs(sloap/sqrt(sloap*sloap+1.0));
  50. m_Sin=-1.0/sqrt(sloap*sloap+1.0);
  51. }
  52. return 0;
  53. }
  54. void CEllipseArc::ellipseMidpoint(CDC *pDC, int xCenter, int yCenter, int Rx, int Ry)
  55. {
  56. int Rx2=Rx*Rx;
  57. int Ry2=Ry*Ry;
  58. int twoRx2=2*Rx2;
  59. int twoRy2=2*Ry2;
  60. int p;
  61. int x=0;
  62. int y=Ry;
  63. int px=0;
  64. int py=twoRx2*y;
  65. ellipsePlotPoints(pDC,xCenter,yCenter,x,y);
  66. p=ROUND(Ry2-(Rx2*Ry)+(0.25*Rx2));
  67. while(px<py)
  68. {
  69. x++;
  70. px+=twoRy2;
  71. if(p<0)
  72. p+=Ry2+px;
  73. else
  74. {
  75. y--;
  76. py-=twoRx2;
  77. p+=Ry2+px-py;
  78. }
  79. ellipsePlotPoints(pDC,xCenter,yCenter,x,y);
  80. }
  81. p=ROUND(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
  82. while(y>0)
  83. {
  84. y--;
  85. py-=twoRx2;
  86. if(p>0)
  87. p+=Rx2-py;
  88. else
  89. {
  90. x++;
  91. px+=twoRy2;
  92. p+=Rx2-py+px;
  93. }
  94. ellipsePlotPoints(pDC,xCenter,yCenter,x,y);
  95. }
  96. }
  97. void CEllipseArc::ellipsePlotPoints(CDC *pDC, int xCenter, int yCenter, int x, int y)
  98. {
  99. PdcSetPixel(pDC,xCenter+x,yCenter+y,m_PenColor);
  100. PdcSetPixel(pDC,xCenter-x,yCenter+y,m_PenColor);
  101. PdcSetPixel(pDC,xCenter+x,yCenter-y,m_PenColor);
  102. PdcSetPixel(pDC,xCenter-x,yCenter-y,m_PenColor);
  103. }
  104. void CEllipseArc::PdcSetPixel(CDC *pDC, int xCenter, int yCenter, COLORREF m_PenColor)
  105. {
  106. double vh[3][3]={m_Cos,-m_Sin,m_CenterPoint.x*(1-m_Cos)+m_CenterPoint.y*m_Sin,
  107. m_Sin, m_Cos,m_CenterPoint.y*(1-m_Cos)-m_CenterPoint.x*m_Sin,
  108. 0,0,1
  109. };
  110. double xy[3]={xCenter,yCenter,1};
  111. double x[3]={0,0,0};
  112. x[0]=vh[0][0]*xy[0]+vh[0][1]*xy[1]+vh[0][2]*xy[2];
  113. x[1]=vh[1][0]*xy[0]+vh[1][1]*xy[1]+vh[1][2]*xy[2];
  114. if(x[0]-int(x[0])>=0.5)
  115. x[0]+=1;
  116. if(x[1]-int(x[1])>=0.5)
  117. x[1]+=1;
  118. if(!m_IsDrawingArc)
  119. {
  120. for(int i=0;i<this->m_PenWidth;i++)
  121. pDC->SetPixel(x[0]+i,x[1]+i,m_PenColor);
  122. }
  123. if(m_IsDrawingArc)
  124. {
  125. double sloap;
  126. if(x[0]-m_CenterPoint.x==0)
  127. {
  128. sloap=999999999999999999.0;
  129. }
  130. else
  131. {
  132. sloap=double(x[1]-m_CenterPoint.y)/double(x[0]-m_CenterPoint.x);
  133. }
  134. double angle;
  135. angle=atan(fabs(sloap));
  136. double degree;
  137. degree=angle*180/3.1415927;
  138. if(x[0]-m_CenterPoint.x>=0&&x[1]-m_CenterPoint.y>=0)
  139. degree=degree;
  140. if(x[0]-m_CenterPoint.x>0&&x[1]-m_CenterPoint.y<=0)
  141. degree=360.0-degree;
  142. if(x[0]-m_CenterPoint.x<=0&&x[1]-m_CenterPoint.y>0)
  143. degree=180-degree;
  144. if(x[0]-m_CenterPoint.x<0&&x[1]-m_CenterPoint.y<0)
  145. degree=180+degree;
  146. if(fabs(degree-m_AngleFirst)<1)
  147. this->m_IntersectionPointFirst=CPoint(x[0],x[1]);
  148. if(fabs(degree-m_AngleSecond)<1)
  149. this->m_IntersectionPointSecond=CPoint(x[0],x[1]);
  150. if((m_AngleFirst<=m_AngleSecond)&&(degree>=m_AngleFirst&&degree<=m_AngleSecond))
  151. {
  152. for(int i=0;i<this->m_PenWidth;i++)
  153. pDC->SetPixel(x[0]+i,x[1]+i,m_PenColor);
  154. }
  155. if((m_AngleFirst>=m_AngleSecond)&&(degree>=m_AngleFirst||degree<=m_AngleSecond))
  156. {
  157. for(int i=0;i<this->m_PenWidth;i++)
  158. pDC->SetPixel(x[0]+i,x[1]+i,m_PenColor);
  159. }
  160. /*CRect rect(this->m_IntersectionPointFirst,this->m_IntersectionPointFirst);
  161. rect.InflateRect(9,9);
  162. pDC->Rectangle(rect);
  163. rect.SetRect(this->m_IntersectionPointSecond,this->m_IntersectionPointSecond);
  164. rect.InflateRect(9,9);
  165. pDC->Rectangle(rect);
  166. */
  167. }
  168. }
  169. void CEllipseArc::DrawStaticStepOne(CDC *pDC,CPoint movingPoint)
  170. {
  171. CPen m_pen;
  172. CBrush m_brush;
  173. int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  174. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  175. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  176. LOGBRUSH brushlog;
  177. brushlog.lbColor=m_BrushColor;
  178. brushlog.lbHatch=m_BrushHatch;
  179. brushlog.lbStyle=m_BrushStyle;
  180. m_brush.CreateBrushIndirect(&brushlog);
  181. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  182. int OldBkMode=pDC->SetBkMode(m_BkMode);
  183. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  184. ellipseMidpoint(pDC,m_CenterPoint.x,m_CenterPoint.y,this->m_Xr,this->m_Yr);
  185. pDC->SelectObject(brush);
  186. pDC->SelectObject(pen);
  187. pDC->SetBkMode(OldBkMode);
  188. pDC->SetBkColor(OldColor);
  189. pDC->SetROP2(oldDrawingMode);
  190. }
  191. void CEllipseArc::DrawActive(CDC *pDC,CPoint point)
  192. {
  193. this->DrawActiveStepOne(pDC,point);
  194. }
  195. void CEllipseArc::DrawStatic(CDC *pDC)
  196. {
  197. DrawStaticStepOne(pDC,this->m_FifthPoint);
  198. }
  199. CPoint CEllipseArc::ComputeIntersectionPointFirst(CPoint point)
  200. {
  201. m_IsDrawingArc=true;
  202. double sloap;
  203. if(point.x==m_CenterPoint.x)
  204. {
  205. sloap=999999999999999999.0;
  206. }
  207. else
  208. {
  209. sloap=double(point.y-m_CenterPoint.y)/double(point.x-m_CenterPoint.x);
  210. }
  211. double angle;
  212. angle=atan(fabs(sloap));
  213. double degree;
  214. degree=angle*180/3.1415927;
  215. if((point.y-m_CenterPoint.y)>=0&&(point.x-m_CenterPoint.x)>=0)
  216. degree=degree;
  217. if((point.y-m_CenterPoint.y)<0&&(point.x-m_CenterPoint.x)>=0)
  218. degree=360.0-degree;
  219. if((point.y-m_CenterPoint.y)>=0&&(point.x-m_CenterPoint.x)<0)
  220. degree=180-degree;
  221. if((point.y-m_CenterPoint.y)<0&&(point.x-m_CenterPoint.x)<0)
  222. degree=180+degree;
  223. this->m_AngleFirst=degree;
  224. return 0;
  225. }
  226. CPoint CEllipseArc::ComputeIntersectionPointSecond(CPoint point)
  227. {
  228. double sloap;
  229. if(point.x==m_CenterPoint.x)
  230. {
  231. sloap=999999999999999999.0;
  232. }
  233. else
  234. {
  235. sloap=double(point.y-m_CenterPoint.y)/double(point.x-m_CenterPoint.x);
  236. }
  237. double angle;
  238. angle=atan(fabs(sloap));
  239. double degree;
  240. degree=angle*180/3.1415927;
  241. if((point.y-m_CenterPoint.y)>=0&&(point.x-m_CenterPoint.x)>=0)
  242. degree=degree;
  243. if((point.y-m_CenterPoint.y)<0&&(point.x-m_CenterPoint.x)>=0)
  244. degree=360.0-degree;
  245. if((point.y-m_CenterPoint.y)>=0&&(point.x-m_CenterPoint.x)<0)
  246. degree=180-degree;
  247. if((point.y-m_CenterPoint.y)<0&&(point.x-m_CenterPoint.x)<0)
  248. degree=180+degree;
  249. this->m_AngleSecond=degree;
  250. return 0;
  251. }
  252. BOOL CEllipseArc::IsInRgn(CPoint point)
  253. {
  254. double vh[3][3]={m_Cos,-m_Sin,m_CenterPoint.x*(1-m_Cos)+m_CenterPoint.y*m_Sin,
  255. m_Sin, m_Cos,m_CenterPoint.y*(1-m_Cos)-m_CenterPoint.x*m_Sin,
  256. 0,0,1
  257. };
  258. double xy[3];
  259. CPoint point1=CPoint(m_CenterPoint.x-m_Xr,m_CenterPoint.y+m_Yr);
  260. CPoint point2=CPoint(m_CenterPoint.x+m_Xr,m_CenterPoint.y+m_Yr);
  261. CPoint point3=CPoint(m_CenterPoint.x+m_Xr,m_CenterPoint.y-m_Yr);
  262. CPoint point4=CPoint(m_CenterPoint.x-m_Xr,m_CenterPoint.y-m_Yr);
  263. CPoint point11=CPoint(m_CenterPoint.x-m_Xr/2,m_CenterPoint.y+m_Yr/2);
  264. CPoint point22=CPoint(m_CenterPoint.x+m_Xr/2,m_CenterPoint.y+m_Yr/2);
  265. CPoint point33=CPoint(m_CenterPoint.x+m_Xr/2,m_CenterPoint.y-m_Yr/2);
  266. CPoint point44=CPoint(m_CenterPoint.x-m_Xr/2,m_CenterPoint.y-m_Yr/2);
  267. CPoint pointList1[]={point1,point2,point3,point4};
  268. CPoint pointList2[]={point11,point22,point33,point44};
  269. for(int i=0;i<4;i++)
  270. {
  271. xy[0]=pointList1[i].x;
  272. xy[1]=pointList1[i].y;
  273. xy[2]=1;
  274. pointList1[i].x=vh[0][0]*xy[0]+vh[0][1]*xy[1]+vh[0][2]*xy[2];
  275. pointList1[i].y=vh[1][0]*xy[0]+vh[1][1]*xy[1]+vh[1][2]*xy[2];
  276. xy[0]=pointList2[i].x;
  277. xy[1]=pointList2[i].y;
  278. xy[2]=1;
  279. pointList2[i].x=vh[0][0]*xy[0]+vh[0][1]*xy[1]+vh[0][2]*xy[2];
  280. pointList2[i].y=vh[1][0]*xy[0]+vh[1][1]*xy[1]+vh[1][2]*xy[2];
  281. }
  282. CRgn rgn1;
  283. rgn1.CreatePolygonRgn(pointList1,4,ALTERNATE);
  284. CRgn rgn2;
  285. rgn2.CreatePolygonRgn(pointList2,4,ALTERNATE);
  286. if(rgn1.PtInRegion(point)&&!rgn2.PtInRegion(point))
  287. return true;
  288. return false;
  289. }
  290. int CEllipseArc::IsOnMarginPoint(CPoint point)
  291. {
  292. CRect rect(point,point);
  293. rect.InflateRect(4,4);
  294. if(rect.PtInRect(this->m_IntersectionPointFirst))
  295. {
  296. this->m_FoundPoint=this->m_IntersectionPointFirst;
  297. return 1;
  298. }
  299. if(rect.PtInRect(this->m_IntersectionPointSecond))
  300. {
  301. this->m_FoundPoint=this->m_IntersectionPointSecond;
  302. return 2;
  303. }
  304. if(rect.PtInRect(this->m_CenterPoint))
  305. {
  306. this->m_FoundPoint=this->m_CenterPoint;
  307. return 3;
  308. }
  309. return 0;
  310. }
  311. void CEllipseArc::ShowSelectPoint(CDC *pDC)
  312. {
  313. CBrush brush;
  314. brush.CreateSolidBrush(RGB(0,0,255));
  315. CPen m_pen;
  316. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  317. CPen *OldPen=pDC->SelectObject(&m_pen);
  318. int oldBkMode=pDC->SetBkMode(OPAQUE);
  319. CBrush *OldBrush=pDC->SelectObject(&brush);
  320. int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  321. CRect rect;
  322. rect.SetRect(this->m_IntersectionPointFirst,this->m_IntersectionPointFirst);
  323. rect.InflateRect(3,3);
  324. pDC->Rectangle(rect);
  325. rect.SetRect(this->m_IntersectionPointSecond,this->m_IntersectionPointSecond);
  326. rect.InflateRect(3,3);
  327. pDC->Rectangle(rect);
  328. rect.SetRect(this->m_CenterPoint,this->m_CenterPoint);
  329. pDC->SelectObject(OldBrush);
  330. brush.DeleteObject();
  331. brush.CreateSolidBrush(RGB(255,0,0));
  332. pDC->SelectObject(&brush);
  333. rect.InflateRect(3,3);
  334. pDC->Rectangle(rect);
  335. pDC->SelectObject(OldPen);
  336. pDC->SetBkMode(oldBkMode);
  337. pDC->SelectObject(OldBrush);
  338. pDC->SetROP2(oldDrawingMode);
  339. }
  340. void CEllipseArc::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  341. {
  342. if(m_HaveFindFirst)
  343. {
  344. pView->L_iPointCount=IsOnMarginPoint(point);
  345. if(pView->L_iPointCount!=0)
  346. {
  347. pView->m_CurrentDrawStatus=Change_Status;
  348. pView->m_CurrentDrawTool=EllipseArc_Tool;
  349. pView->m_pCurrentUnit=this;
  350. pView->m_bHaveFindSecond=true;
  351. if(pView->L_iPointCount==3)
  352. pView->m_CurrentDrawStatus=Drag_Status;
  353. pView->m_pCurrentUnit=this;
  354. ShowSelectPoint(pDC);
  355. m_DrawingMode=pDC->GetROP2();
  356. DrawStatic(pDC);
  357. DrawOldReferencePoint(pDC,m_FoundPoint);
  358. }
  359. }
  360. if(!pView->m_bHaveFindSecond&&IsInRgn(point))
  361. {
  362. if(m_HaveFindFirst)
  363. {
  364. m_HaveFindFirst=false;
  365. pView->m_bHaveFindFirst=true;
  366. }
  367. else
  368. {
  369. pView->m_bHaveFindFirst=true;
  370. m_HaveFindFirst=true;
  371. }
  372. ShowSelectPoint(pDC);
  373. }
  374. }
  375. void CEllipseArc::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  376. {
  377. if(pView->m_CurrentDrawStatus==Drag_Status)
  378. {
  379. DrawStatic(pDC);
  380. if(!m_IsCirCu)
  381. m_CenterPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  382. if(m_IsCirCu)
  383. {
  384. ComputSloap(pView->m_LastDownPoint,point);
  385. ComputRadiusX(pView->m_LastDownPoint,point);
  386. }
  387. DrawStatic(pDC);
  388. }
  389. else
  390. {
  391. //pDC->SetROP2(R2_NOTXORPEN);
  392. if(pView->m_LBDownTimes==1&&pView->m_CurrentDrawStatus==Change_Status)
  393. {
  394. DrawStatic(pDC);
  395. if(!m_IsCirCu)
  396. {
  397. if(pView->L_iPointCount==1)
  398. ComputeIntersectionPointFirst(point);
  399. if(pView->L_iPointCount==2)
  400. ComputeIntersectionPointSecond(point);
  401. }
  402. if(m_IsCirCu)
  403. {
  404. ComputSloap(this->m_CenterPoint,point);
  405. }
  406. pView->m_SecondPoint=point;
  407. DrawStatic(pDC);
  408. }
  409. if(pView->m_LBDownTimes==3&&pView->m_CurrentDrawStatus!=Change_Status)
  410. {
  411. DrawActive(pDC,pView->m_SecondPoint);
  412. ComputeIntersectionPointSecond(point);
  413. m_DrawingMode=pDC->GetROP2();
  414. m_FifthPoint=point;
  415. pView->m_SecondPoint=point;
  416. DrawActive(pDC,point);
  417. }
  418. if(pView->m_LBDownTimes==2&&pView->m_CurrentDrawStatus!=Change_Status)
  419. {
  420. // m_pCurrentUnit->ComputRadiusX(m_pCurrentUnit->m_CenterPoint,point);
  421. DrawActiveStepOne(pDC,pView->m_SecondPoint);
  422. ComputRadiusX(m_CenterPoint,point);
  423. m_DrawingMode=pDC->GetROP2();
  424. pView->m_SecondPoint=point;
  425. DrawActiveStepOne(pDC,point);
  426. }
  427. if(pView->m_LBDownTimes==1&&pView->m_CurrentDrawStatus!=Change_Status)
  428. {
  429. ShowMovingLine(pDC,pView->m_FirstPoint,pView->m_SecondPoint);
  430. pView->m_SecondPoint=point;
  431. ShowMovingLine(pDC,pView->m_FirstPoint,point);
  432. }
  433. }
  434. }
  435. void CEllipseArc::Serialize(CArchive &ar)
  436. {
  437. CUnit::Serialize(ar);
  438. if(ar.IsStoring())
  439. {
  440. ar<<m_BrushStyle<<m_BkMode<<m_BrushHatch<<m_BrushColor;
  441. ar<<m_Sin<<m_Cos;
  442. ar<<m_Xr<<m_Yr;
  443. ar<<m_CenterPoint;
  444. ar<<m_IntersectionPointFirst<<m_IntersectionPointSecond;
  445. ar<<m_AngleFirst<<m_AngleSecond;
  446. }
  447. else
  448. {
  449. ar>>m_BrushStyle>>m_BkMode>>m_BrushHatch>>m_BrushColor;
  450. ar>>m_Sin>>m_Cos;
  451. ar>>m_Xr>>m_Yr;
  452. ar>>m_CenterPoint;
  453. ar>>m_IntersectionPointFirst>>m_IntersectionPointSecond;
  454. ar>>m_AngleFirst>>m_AngleSecond;
  455. this->m_IsDrawingArc=true;
  456. }
  457. }
  458. void CEllipseArc::OnMenuitemCirCu(CDC *pDC, CEastDrawView *pView)
  459. {
  460. m_IsCirCu=!m_IsCirCu;
  461. }
  462. void CEllipseArc::OnContextMenu(CWnd *pWnd, CPoint point)
  463. {
  464. CMenu menuTextEdit;
  465. menuTextEdit.LoadMenu(IDR_MENU_Canvas_Edit);
  466. menuTextEdit.CheckMenuItem(ID_CirCu,m_IsCirCu?MF_CHECKED:MF_UNCHECKED);
  467. menuTextEdit.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,pWnd);
  468. }