Ellipse.cpp
上传用户:ckg1000
上传日期:2013-01-26
资源大小:630k
文件大小:10k
源码类别:

CAD

开发平台:

Visual C++

  1. // Ellipse.cpp: implementation of the CEllipse class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CAD2006.h"
  6. #include "Ellipse.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. extern enum tagToolState{Init_State,FirstHit,SecondHit};
  17. CEllipse::CEllipse()
  18. {
  19. m_eToolState = Init_State;
  20. }
  21. CEllipse::~CEllipse()
  22. {
  23. }
  24. void CEllipse::OnLbuttondown(CDC *pDC,CPoint point)
  25. {
  26. if(m_eToolState == Init_State)
  27. {
  28. m_eToolState = FirstHit;
  29. m_ptBeginPos = m_ptPrePos = point;
  30. }
  31. else if(m_eToolState == FirstHit)
  32. {
  33. m_eToolState = SecondHit;
  34. }
  35. }
  36. void CEllipse::Onmousemove(CDC *pDC,CPoint point)
  37. {
  38. if(m_eToolState == FirstHit)
  39. {
  40. HDC  hdc      = pDC->GetSafeHdc();
  41. int  nOldMode = ::SetROP2(hdc,R2_XORPEN);
  42. HPEN hPen     = ::CreatePen( m_borderStyle,
  43. m_borderWidth,m_borderColor^pDC->GetBkColor() );
  44.  
  45.  
  46. HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
  47. //不进行填充
  48. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  49. //擦去前一次线段
  50. DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
  51. //画出这一次线段
  52. DrawEllipse(hdc,m_ptBeginPos,point);
  53. ::SetROP2(hdc,nOldMode);
  54. ::SelectObject(hdc,hOldPen);
  55. ::DeleteObject(hPen);
  56. m_ptPrePos = point;
  57. }
  58. }
  59. //画椭圆
  60. void CEllipse::DrawEllipse(HDC hdc,POINT ptCenter,POINT ptSide)
  61. {
  62. Ellipse(hdc,ptCenter.x,ptCenter.y,ptSide.x,ptSide.y);
  63. m_ptPrePos = ptSide;
  64. if (KILLRECT == true)
  65. {
  66. ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
  67. DrawRect(hdc);
  68. KILLRECT = false;
  69. }
  70. }
  71. void CEllipse::SetPosEnd(CPoint point)
  72. {
  73. m_ptBeginPos = point;
  74. }
  75. void CEllipse::SetPosBegin(CPoint point)
  76. {
  77. // nRadius = point.x;
  78. m_ptPrePos = point;
  79. }
  80. CPoint CEllipse::GetPosEnd()
  81. {
  82. return m_ptBeginPos;
  83. }
  84. CPoint CEllipse::GetPosBegin()
  85. {
  86. /* CPoint point;
  87. point.x = nRadius;
  88. point.y = 0;
  89. return point;*/
  90. return m_ptPrePos;
  91. }
  92. bool CEllipse::Select(CDC *pDC,CPoint point)
  93. {
  94. if( pick(point) )
  95. {
  96. HDC hdc = pDC->GetSafeHdc();
  97. SelectObject(hdc,GetStockObject(WHITE_BRUSH));
  98. DrawRect(hdc);
  99. m_oldpt = point;
  100. KILLRECT = true;
  101. return true;
  102. }
  103. return false;
  104. }
  105. void CEllipse::Move(CDC *pDoc, CPoint point)
  106. {
  107. HDC hdc = pDoc->GetSafeHdc();
  108. int nOldMode = ::SetROP2(hdc,R2_XORPEN);
  109. HPEN hPen    = ::CreatePen( m_borderStyle,
  110. m_borderWidth,m_borderColor^pDoc->GetBkColor());
  111. HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
  112. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  113. CPoint oldfristpoint = m_ptBeginPos;
  114. CPoint oldsecondpoint = m_ptPrePos;
  115. //擦去前一次
  116. DrawEllipse(hdc,oldfristpoint,oldsecondpoint);
  117. m_ptBeginPos.x = m_ptBeginPos.x + point.x - m_oldpt.x ;
  118. m_ptBeginPos.y = m_ptBeginPos.y + point.y - m_oldpt.y ;
  119. m_ptPrePos.x = m_ptPrePos.x + point.x - m_oldpt.x ;
  120. m_ptPrePos.y = m_ptPrePos.y + point.y - m_oldpt.y ;
  121. m_oldpt = point;
  122. //画出这一次
  123. DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
  124. ::SetROP2(hdc,nOldMode);
  125. ::SelectObject(hdc,hOldPen);
  126. ::DeleteObject(hPen);
  127. }
  128. bool CEllipse::pick(POINT ptCurPos)
  129. {
  130. POINT a1_Temp, a2_Temp, b1_Temp, b2_Temp;//椭圆四个顶点
  131. POINT Center_Temp;  //椭圆中心点
  132. POINT F1, F2;  //椭圆焦点
  133. a1_Temp.x = m_ptBeginPos.x-(int)(m_ptBeginPos.x - m_ptPrePos.x)/4;
  134. a1_Temp.y = m_ptBeginPos.y-(int)(m_ptBeginPos.y - m_ptPrePos.y)/2;
  135. a2_Temp.x = m_ptPrePos.x;
  136. a2_Temp.y = m_ptBeginPos.y + (int)(fabs(( m_ptBeginPos.y -m_ptPrePos.y )/2));
  137. b1_Temp.x = m_ptBeginPos.x +(int)(fabs((m_ptPrePos.x - m_ptBeginPos.x)/2));
  138. b1_Temp.y = m_ptBeginPos.y;
  139. b2_Temp.x = m_ptBeginPos.x +(int)(fabs((m_ptPrePos.x - m_ptBeginPos.x)/2));
  140. b2_Temp.y = m_ptPrePos.y;
  141. Center_Temp.x = m_ptBeginPos.x +(int)(fabs((m_ptPrePos.x - m_ptBeginPos.x)/2));
  142. Center_Temp.y = m_ptBeginPos.y + (int)(fabs(( m_ptBeginPos.y -m_ptPrePos.y )/2));
  143. double n_A = Distance(a1_Temp, Center_Temp);
  144. double n_B = Distance(b1_Temp, Center_Temp);
  145. double n_C;
  146. if (n_A > n_B)
  147. {
  148. n_C  = sqrt(n_A*n_A - n_B*n_B);
  149. F1.x = (int)(Center_Temp.x-n_C);
  150. F1.y = Center_Temp.y;
  151. F2.x = (int)(Center_Temp.x+n_C);
  152. F2.y = Center_Temp.y;
  153. int n_Temp  = Distance(F1, ptCurPos) + Distance(F2, ptCurPos);
  154. int n_1Temp = Distance(a1_Temp, a2_Temp);
  155. if( fabs(n_Temp - n_1Temp) < 8 )
  156. {
  157. return true;
  158. }
  159. }
  160. else
  161. {
  162. n_C = sqrt(n_B*n_B-n_A*n_A);
  163. F1.y = (int)(Center_Temp.y -n_C);
  164. F1.x = Center_Temp.x ;
  165. F2.y = (int)(Center_Temp.y +n_C);
  166. F2.x = Center_Temp.x ;
  167. int n_Temp  = Distance(F1, ptCurPos) + Distance(F2, ptCurPos);
  168. int n_1Temp = Distance(b1_Temp,b2_Temp);
  169. if( fabs(n_Temp - n_1Temp) < 8 )
  170. {
  171. return true;
  172. }
  173. }
  174.   return false;
  175. }
  176. void CEllipse::DrawRect(HDC hdc)
  177. {
  178. POINT t_ptFirstPos,t_ptSecondPos;
  179. //外接矩形的左上点和右下点坐标
  180. t_ptFirstPos.x  = m_ptBeginPos.x-(int)(m_ptBeginPos.x - m_ptPrePos.x)/4;
  181. t_ptFirstPos.y  = m_ptBeginPos.y-(int)(m_ptBeginPos.y - m_ptPrePos.y)/2;
  182. t_ptSecondPos.x = m_ptPrePos.x;
  183. t_ptSecondPos.y = m_ptPrePos.y;
  184. HPEN hpen;
  185. int i = ::SetROP2(hdc,R2_XORPEN);
  186. hpen = ::CreatePen(0,0,RGB(255,0,0)^::GetBkColor(hdc));
  187. HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
  188. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  189. //左中点
  190. Ellipse(hdc,m_ptBeginPos.x - 4,t_ptFirstPos.y - 4,
  191. m_ptBeginPos.x + 4,t_ptFirstPos.y + 4);
  192. //上边中点
  193. Ellipse(hdc,m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2))- 4,m_ptBeginPos.y - 4,
  194. m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2)) + 4,m_ptBeginPos.y + 4);
  195. //右中点
  196. Ellipse(hdc,t_ptSecondPos.x - 4,m_ptBeginPos.y + (int)fabs(( m_ptBeginPos.y -t_ptSecondPos.y )/2) - 4,
  197.          t_ptSecondPos.x + 4,m_ptBeginPos.y + (int)fabs(( m_ptBeginPos.y -t_ptSecondPos.y )/2)  + 4);
  198. //下底中点
  199. Ellipse(hdc,m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2)) - 4,t_ptSecondPos.y - 4,
  200. m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2)) + 4,t_ptSecondPos.y + 4);
  201. ::SetROP2(hdc,i);
  202. ::SelectObject(hdc,holdpen);
  203. ::DeleteObject(hpen);
  204. }
  205. //功能:求两点间距
  206. int CEllipse::Distance(POINT ptPos1, POINT ptPos2)
  207. {
  208. double dbD = sqrt( (ptPos1.x - ptPos2.x) * (ptPos1.x - ptPos2.x) + 
  209. (ptPos1.y - ptPos2.y) *(ptPos1.y - ptPos2.y) );
  210. return (int)dbD;
  211. }
  212. void CEllipse::SetPosCenter(CPoint point)
  213. {
  214. m_ptPrePos = point;
  215. }
  216. CPoint CEllipse::GetPosCenter()
  217. {
  218. return m_ptPrePos;
  219. }
  220. void CEllipse::Mirror(CDC *pDC, CPoint point)
  221. {
  222. POINT curPt;
  223. curPt = point;
  224. m_ptPrePos = m_MirEndPt;
  225. m_MirEndPt = curPt;
  226. HDC hdc = pDC->GetSafeHdc();
  227. static CPoint FirstMirOldPt(0,0);
  228. FirstMirOldPt = m_FirstMirPt;   //镜像后的老点
  229. m_FirstMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_ptBeginPos);
  230. DrawMirLine(pDC,m_MirBegPt,m_MirEndPt);  //镜像线
  231. HPEN hpen = ::CreatePen(1,1,RGB(0,0,0)^pDC->GetBkColor());
  232. pDC->SelectObject(hpen);
  233. int i = pDC->SetROP2(R2_XORPEN);
  234. HBRUSH hbrush = (HBRUSH)pDC->SelectObject(::GetStockObject(NULL_BRUSH));
  235. this->DrawEllipse(hdc,FirstMirOldPt,m_ptBeginPos);
  236. this->DrawEllipse(hdc,m_FirstMirPt,m_ptBeginPos);
  237. pDC->SelectObject(hbrush);
  238. ::DeleteObject(hpen);
  239. // this->DrawCircle(hdc,m_FirstMirPt,nRadius);
  240. FirstMirOldPt = m_FirstMirPt;
  241. m_ptPrePos = curPt;
  242. pDC->SetROP2(i);
  243. }
  244. void CEllipse::KillMirLine(CDC *pDC)
  245. {
  246. HPEN newPen = CreatePen(0,0,pDC->GetBkColor());  
  247. HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
  248. pDC->MoveTo(m_MirBegPt.x,m_MirBegPt.y);
  249. pDC->LineTo(m_MirEndPt.x,m_MirEndPt.y);
  250. pDC->SelectObject(oldPen);
  251. DeleteObject(newPen);
  252. ::DeleteObject(oldPen);
  253. }
  254. void CEllipse::DrawMirLine(CDC *pDC, POINT MirBegPt, POINT MirEndPt)
  255. {
  256. HDC  hdc = pDC->GetSafeHdc();
  257. int i = ::SetROP2(hdc,R2_XORPEN);
  258. HPEN hpen = ::CreatePen(0,0,RGB(255,0,0)^::GetBkColor(hdc));
  259. HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
  260. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  261. pDC->MoveTo(MirBegPt.x,MirBegPt.y);
  262. pDC->LineTo(m_ptPrePos.x,m_ptPrePos.y);
  263. pDC->MoveTo(MirBegPt.x,MirBegPt.y);
  264. pDC->LineTo(MirEndPt.x,MirEndPt.y);
  265. pDC->SetROP2(i);
  266. pDC->SelectObject(hpen);
  267. // ::DeleteObject(newPen);
  268. ::DeleteObject(hpen);
  269. }
  270. POINT CEllipse::GetMirPt(const POINT &ptBegin, const POINT &ptEnd, POINT ptCur)
  271. {
  272. POINT ptNew;
  273. double length = Distance(ptBegin,ptEnd);
  274. if(length == 0)
  275. return ptNew=ptCur;
  276. double t1 = 2. * ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.y-ptBegin.y)/length);
  277. double t2 = ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.x-ptBegin.x)/length)
  278. - ((ptEnd.y-ptBegin.y)/length) * ((ptEnd.y-ptBegin.y)/length);
  279. ptNew.x =(int)(ptCur.x*t2 + ptCur.y*t1 + ptBegin.x*(-t2) - ptBegin.y*t1 + ptBegin.x);
  280. ptNew.y =(int)(ptCur.x*t1 + ptCur.y*(-t2) + ptBegin.y*t2 - ptBegin.x*t1 + ptBegin.y);
  281. return ptNew;
  282. }
  283. void CEllipse::Zoom(CDC *pDoc, CPoint point)
  284. {
  285. // Update(pDoc);
  286. POINT curPt;
  287. curPt.x = point.x,curPt.y = point.y;
  288. POINT begOldPt = m_ptBeginPos;
  289. POINT endOldPt = m_ptPrePos;
  290. HPEN hpen;
  291. HDC hdc = pDoc->GetSafeHdc();
  292. int i = ::SetROP2(hdc,R2_XORPEN);
  293. hpen = ::CreatePen(0,0,RGB(0,0,0)^::GetBkColor(hdc));
  294. HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
  295. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  296. POINT oldfirstpoint;
  297. oldfirstpoint = m_ptBeginPos;
  298. POINT oldsecondpoint;
  299. oldsecondpoint = m_ptPrePos;
  300. DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
  301. if(m_oldpt.y == begOldPt.y)   //选到上边
  302. {
  303. m_ptBeginPos.y = curPt.y;
  304. }
  305. if(m_oldpt.y == endOldPt.y)   //下边
  306. {
  307. m_ptPrePos.y = curPt.y;
  308. }
  309. if(m_oldpt.x == begOldPt.x)   //左边
  310. {
  311. m_ptBeginPos.x = curPt.x;
  312. }
  313. if(m_oldpt.x == endOldPt.x)   //右边
  314. {
  315. m_ptPrePos.x = curPt.x;
  316. }
  317. DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
  318. ::SetROP2(hdc,i);
  319. ::SelectObject(hdc,holdpen);
  320. ::DeleteObject(hpen);
  321. m_oldpt = curPt; 
  322. }
  323. void CEllipse::Delete(CDC *pDC)
  324. {
  325. HDC hdc = pDC->GetSafeHdc();
  326. DrawRect(hdc);
  327. HPEN newPen = CreatePen(m_borderStyle,
  328. m_borderWidth,GetBkColor(hdc));
  329. SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
  330. ::SelectObject(hdc,newPen);
  331. DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
  332. ::DeleteObject(newPen);