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

CAD

开发平台:

Visual C++

  1. // Circle.cpp: implementation of the CCircle class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CAD2006.h"
  6. #include "Circle.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. CCircle::CCircle()
  17. {
  18. m_eToolState = Init_State;
  19. blncircletemp = true;
  20. }
  21. CCircle::~CCircle()
  22. {
  23. }
  24. void CCircle::OnLbuttondown(CDC *pDC,CPoint point)
  25. {
  26. if(m_eToolState == Init_State)
  27. {
  28. m_eToolState = FirstHit;
  29. m_pttempbeginpos = m_ptBeginPos = m_ptPrePos = point;
  30. }
  31. else if(m_eToolState == FirstHit)
  32. {
  33. m_eToolState = SecondHit;
  34. }
  35. }
  36. void CCircle::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. HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
  46. //不进行填充
  47. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  48. //擦去前一次线段
  49. nRadius = Distance(m_ptBeginPos,m_ptPrePos );
  50. DrawCircle(hdc,m_ptBeginPos,nRadius);
  51. //画出这一次线段
  52. nRadius = Distance(m_ptBeginPos,point );
  53. DrawCircle(hdc,m_ptBeginPos,nRadius);
  54. ::SetROP2(hdc,nOldMode);
  55. ::SelectObject(hdc,hOldPen);
  56. ::DeleteObject(hPen);
  57. m_ptPrePos = point;
  58. }
  59. }
  60. int CCircle::Distance(POINT ptPos1, POINT ptPos2)
  61. {
  62. double dbD = sqrt( (ptPos1.x - ptPos2.x) * (ptPos1.x - ptPos2.x) + 
  63. (ptPos1.y - ptPos2.y) *(ptPos1.y - ptPos2.y) );
  64. return (int)dbD;
  65. }
  66. void CCircle::DrawCircle(HDC hdc,POINT ptCenter,int nRadius)
  67. {
  68. // HDC  hdc      = pDC->GetSafeHdc();
  69. int nLeft   = ptCenter.x - nRadius;
  70. int nTop    = ptCenter.y - nRadius;
  71. int nRight  = ptCenter.x + nRadius;
  72. int nBottom = ptCenter.y + nRadius;
  73. ::Ellipse(hdc,nLeft,nTop,nRight,nBottom);
  74. m_pttempbeginpos = m_ptBeginPos;
  75. /* if (KILLRECT == true)
  76. {
  77. ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
  78. DrawRect(hdc,m_pttempbeginpos);
  79. KILLRECT = false;
  80. }*/
  81. }
  82. bool CCircle::Select(CDC *pDC,CPoint point)
  83. {
  84. if( !pick(point) )
  85. {
  86. HDC hdc = pDC->GetSafeHdc();
  87. SelectObject(hdc,GetStockObject(WHITE_BRUSH));
  88. DrawRect(hdc,m_ptBeginPos);
  89. KILLRECT = true;
  90. m_oldpt = point;
  91. return true;
  92. }
  93. return false;
  94. }
  95. bool CCircle::pick(POINT pt)
  96. {
  97. //计算当前鼠标位置与圆心的距离
  98. int dblLength = Distance(m_ptBeginPos,pt);
  99. if (fabs(dblLength - nRadius) < 5 )
  100. return FALSE;
  101. return TRUE;
  102. }
  103. void CCircle::DrawRect(HDC hdc,POINT m_ptBeginPo,COLORREF m_color)
  104. {
  105. CPoint t_ptFirstPos,t_ptSecondPos;
  106. HPEN hpen;
  107. int i = ::SetROP2(hdc,R2_XORPEN);
  108. hpen = ::CreatePen(0,0,m_color^::GetBkColor(hdc));
  109. HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
  110. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  111. //外接矩形的左上点和右下点坐标
  112. t_ptFirstPos.x  = m_ptBeginPo.x - nRadius;
  113. t_ptSecondPos.x = m_ptBeginPo.x + nRadius;
  114. t_ptFirstPos.y  = m_ptBeginPo.y - nRadius;
  115. t_ptSecondPos.y = m_ptBeginPo.y + nRadius;
  116. //圆心的坐标
  117. int mPtx = m_ptBeginPo.x;
  118. int mPty = m_ptBeginPo.y;
  119. int Rect = 4;
  120. //top left
  121. Ellipse(hdc,t_ptFirstPos.x - Rect,t_ptFirstPos.y - Rect,
  122. t_ptFirstPos.x + Rect,t_ptFirstPos.y + Rect);
  123. //top right
  124. Ellipse(hdc,t_ptSecondPos.x - Rect,t_ptFirstPos.y - Rect,
  125. t_ptSecondPos.x + Rect,t_ptFirstPos.y + Rect);
  126. //top middle
  127. Ellipse(hdc,mPtx - Rect,t_ptFirstPos.y - Rect,
  128. mPtx + Rect,t_ptFirstPos.y + Rect);
  129. //left middle
  130. Ellipse(hdc,t_ptFirstPos.x - Rect,mPty - Rect,
  131. t_ptFirstPos.x + Rect,mPty + Rect);
  132. //right middle 
  133. Ellipse(hdc,t_ptSecondPos.x - Rect,mPty - Rect,
  134. t_ptSecondPos.x + Rect,mPty + Rect); 
  135. //buttom middle
  136. Ellipse(hdc,mPtx - Rect,t_ptSecondPos.y - Rect,
  137. mPtx + Rect,t_ptSecondPos.y + Rect);
  138. //buttom left
  139. Ellipse(hdc,t_ptFirstPos.x - Rect,t_ptSecondPos.y - Rect,
  140. t_ptFirstPos.x + Rect,t_ptSecondPos.y + Rect);
  141. //buttom right
  142. Ellipse(hdc,t_ptSecondPos.x - Rect,t_ptSecondPos.y - Rect,
  143. t_ptSecondPos.x + Rect,t_ptSecondPos.y + Rect);
  144. //m_ptBeginPos = m_pttempbeginpos;
  145. ::SetROP2(hdc,i);
  146. ::SelectObject(hdc,holdpen);
  147. ::DeleteObject(hpen);
  148. }
  149. void CCircle::SetPosBegin(CPoint point)
  150. {
  151. m_ptBeginPos = point;
  152. }
  153. void CCircle::SetPosEnd(CPoint point)
  154. {
  155. nRadius = point.x;
  156. }
  157. CPoint CCircle::GetPosBegin()
  158. {
  159. if(blncircletemp)
  160. {
  161. return m_ptBeginPos;
  162. }
  163. else
  164. {
  165. return m_FirstMirPt;
  166. }
  167. }
  168. CPoint CCircle::GetPosEnd()
  169. {
  170. CPoint point;
  171. point.x = nRadius;
  172. point.y = 0;
  173. return point;
  174. }
  175. void CCircle::Move(CDC *pDoc,CPoint point)
  176. {
  177. HDC hdc = pDoc->GetSafeHdc();
  178. int nOldMode = ::SetROP2(hdc,R2_XORPEN);
  179. HPEN hPen    = ::CreatePen( m_borderStyle,
  180. m_borderWidth,m_borderColor^pDoc->GetBkColor());
  181. HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
  182. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  183. //擦去前一次
  184. DrawCircle(hdc,m_ptBeginPos,nRadius);
  185. m_ptBeginPos.x = m_ptBeginPos.x + point.x - m_oldpt.x ;
  186. m_ptBeginPos.y = m_ptBeginPos.y + point.y - m_oldpt.y ;
  187. m_oldpt = point;
  188. //画出这一次
  189. DrawCircle(hdc,m_ptBeginPos,nRadius);
  190. ::SetROP2(hdc,nOldMode);
  191. ::SelectObject(hdc,hOldPen);
  192. ::DeleteObject(hPen);
  193. }
  194. void CCircle::Update(CDC *pDC)
  195. {
  196. // this->DrawRect(pDC->GetSafeHdc(),m_pttempbeginpos,RGB(255,255,255));
  197. this->DrawRect(pDC->GetSafeHdc(),m_ptBeginPos,RGB(255,255,255));
  198. // HDC hdc = pDC->GetSafeHdc();
  199. // DrawCircle(hdc,m_ptBeginPos,nRadius);
  200. this->KillMirLine(pDC);
  201. }
  202. void CCircle::Mirror(CDC *pDC, CPoint point)
  203. {
  204. POINT curPt;
  205. curPt = point;
  206. m_ptPrePos = m_MirEndPt;
  207. m_MirEndPt = curPt;
  208. HDC hdc = pDC->GetSafeHdc();
  209. static CPoint FirstMirOldPt(0,0);
  210. FirstMirOldPt = m_FirstMirPt;   //镜像后的老点
  211. m_FirstMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_ptBeginPos);
  212. DrawMirLine(pDC,m_MirBegPt,m_MirEndPt);  //镜像线
  213. HPEN hpen = ::CreatePen(1,1,RGB(0,0,0)^pDC->GetBkColor());
  214. pDC->SelectObject(hpen);
  215. int i = pDC->SetROP2(R2_XORPEN);
  216. HBRUSH hbrush = (HBRUSH)pDC->SelectObject(::GetStockObject(NULL_BRUSH));
  217. this->DrawCircle(hdc,FirstMirOldPt,nRadius);
  218. this->DrawCircle(hdc,m_FirstMirPt,nRadius);
  219. pDC->SelectObject(hbrush);
  220. ::DeleteObject(hpen);
  221. // this->DrawCircle(hdc,m_FirstMirPt,nRadius);
  222. FirstMirOldPt = m_FirstMirPt;
  223. m_ptPrePos = curPt;
  224. pDC->SetROP2(i);
  225. blncircletemp = false;
  226. }
  227. POINT CCircle::GetMirPt(const POINT &ptBegin, const POINT &ptEnd, POINT ptCur)
  228. {
  229. POINT ptNew;
  230. double length = Distance(ptBegin,ptEnd);
  231. if(length == 0)
  232. return ptNew=ptCur;
  233. double t1 = 2. * ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.y-ptBegin.y)/length);
  234. double t2 = ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.x-ptBegin.x)/length)
  235. - ((ptEnd.y-ptBegin.y)/length) * ((ptEnd.y-ptBegin.y)/length);
  236. ptNew.x =(int)(ptCur.x*t2 + ptCur.y*t1 + ptBegin.x*(-t2) - ptBegin.y*t1 + ptBegin.x);
  237. ptNew.y =(int)(ptCur.x*t1 + ptCur.y*(-t2) + ptBegin.y*t2 - ptBegin.x*t1 + ptBegin.y);
  238. return ptNew;
  239. }
  240. void CCircle::OnMirLBtnDn(CDC *pDC, CPoint point)
  241. {
  242. POINT curPoint;   //左键点击的点
  243. curPoint.x = point.x ;
  244. curPoint.y = point.y ;
  245. m_MirBegPt = m_MirEndPt = m_ptPrePos = curPoint;
  246. }
  247. void CCircle::DrawMirLine(CDC *pDC, POINT MirBegPt, POINT MirEndPt)
  248. {
  249. HDC  hdc      = pDC->GetSafeHdc();
  250. int i = ::SetROP2(hdc,R2_XORPEN);
  251. HPEN hpen = ::CreatePen(0,0,RGB(255,0,0)^::GetBkColor(hdc));
  252. HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
  253. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  254. pDC->MoveTo(MirBegPt.x,MirBegPt.y);
  255. pDC->LineTo(m_ptPrePos.x,m_ptPrePos.y);
  256. pDC->MoveTo(MirBegPt.x,MirBegPt.y);
  257. pDC->LineTo(MirEndPt.x,MirEndPt.y);
  258. pDC->SetROP2(i);
  259. pDC->SelectObject(hpen);
  260. // ::DeleteObject(newPen);
  261. ::DeleteObject(hpen);
  262. }
  263. void CCircle::KillMirLine(CDC *pDC)
  264. {
  265. HPEN newPen = CreatePen(0,0,pDC->GetBkColor());  
  266. HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
  267. pDC->MoveTo(m_MirBegPt.x,m_MirBegPt.y);
  268. pDC->LineTo(m_MirEndPt.x,m_MirEndPt.y);
  269. pDC->SelectObject(oldPen);
  270. DeleteObject(newPen);
  271. ::DeleteObject(oldPen);
  272. }
  273. void CCircle::Zoom(CDC *pDC, CPoint point)
  274. {
  275. HDC  hdc      = pDC->GetSafeHdc();
  276. int  nOldMode = ::SetROP2(hdc,R2_XORPEN);
  277. HPEN hPen     = ::CreatePen( m_borderStyle,
  278. m_borderWidth,m_borderColor^pDC->GetBkColor() );
  279. HPEN holdpen =(HPEN) ::SelectObject(hdc,hPen);
  280. ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
  281. int oldnRadius = nRadius;
  282. //擦去前一次线段
  283. DrawCircle(hdc,m_ptBeginPos,oldnRadius);
  284. //画出这一次线段
  285. nRadius = Distance(m_ptBeginPos,point );
  286. DrawCircle(hdc,m_ptBeginPos,nRadius);
  287. ::SetROP2(hdc,nOldMode);
  288. ::SelectObject(hdc,holdpen);
  289. ::DeleteObject(hPen);
  290. // DrawCircle(hdc,m_ptBeginPos,nRadius);
  291. }
  292. void CCircle::Delete(CDC *pDC)
  293. {
  294. HDC hdc = pDC->GetSafeHdc();
  295. DrawRect(hdc,m_ptBeginPos);
  296. HPEN newPen = CreatePen(m_borderStyle,
  297. m_borderWidth,GetBkColor(hdc));
  298. SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
  299. ::SelectObject(hdc,newPen);
  300. DrawCircle(hdc,m_ptBeginPos,nRadius);
  301. ::DeleteObject(newPen);
  302. }