Circle.cpp
资源名称:CAD2006.rar [点击查看]
上传用户:ckg1000
上传日期:2013-01-26
资源大小:630k
文件大小:9k
源码类别:
CAD
开发平台:
Visual C++
- // Circle.cpp: implementation of the CCircle class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CAD2006.h"
- #include "Circle.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CCircle::CCircle()
- {
- m_eToolState = Init_State;
- blncircletemp = true;
- }
- CCircle::~CCircle()
- {
- }
- void CCircle::OnLbuttondown(CDC *pDC,CPoint point)
- {
- if(m_eToolState == Init_State)
- {
- m_eToolState = FirstHit;
- m_pttempbeginpos = m_ptBeginPos = m_ptPrePos = point;
- }
- else if(m_eToolState == FirstHit)
- {
- m_eToolState = SecondHit;
- }
- }
- void CCircle::Onmousemove(CDC *pDC,CPoint point)
- {
- if(m_eToolState == FirstHit)
- {
- HDC hdc = pDC->GetSafeHdc();
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor() );
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- //不进行填充
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- //擦去前一次线段
- nRadius = Distance(m_ptBeginPos,m_ptPrePos );
- DrawCircle(hdc,m_ptBeginPos,nRadius);
- //画出这一次线段
- nRadius = Distance(m_ptBeginPos,point );
- DrawCircle(hdc,m_ptBeginPos,nRadius);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- m_ptPrePos = point;
- }
- }
- int CCircle::Distance(POINT ptPos1, POINT ptPos2)
- {
- double dbD = sqrt( (ptPos1.x - ptPos2.x) * (ptPos1.x - ptPos2.x) +
- (ptPos1.y - ptPos2.y) *(ptPos1.y - ptPos2.y) );
- return (int)dbD;
- }
- void CCircle::DrawCircle(HDC hdc,POINT ptCenter,int nRadius)
- {
- // HDC hdc = pDC->GetSafeHdc();
- int nLeft = ptCenter.x - nRadius;
- int nTop = ptCenter.y - nRadius;
- int nRight = ptCenter.x + nRadius;
- int nBottom = ptCenter.y + nRadius;
- ::Ellipse(hdc,nLeft,nTop,nRight,nBottom);
- m_pttempbeginpos = m_ptBeginPos;
- /* if (KILLRECT == true)
- {
- ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRect(hdc,m_pttempbeginpos);
- KILLRECT = false;
- }*/
- }
- bool CCircle::Select(CDC *pDC,CPoint point)
- {
- if( !pick(point) )
- {
- HDC hdc = pDC->GetSafeHdc();
- SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRect(hdc,m_ptBeginPos);
- KILLRECT = true;
- m_oldpt = point;
- return true;
- }
- return false;
- }
- bool CCircle::pick(POINT pt)
- {
- //计算当前鼠标位置与圆心的距离
- int dblLength = Distance(m_ptBeginPos,pt);
- if (fabs(dblLength - nRadius) < 5 )
- return FALSE;
- return TRUE;
- }
- void CCircle::DrawRect(HDC hdc,POINT m_ptBeginPo,COLORREF m_color)
- {
- CPoint t_ptFirstPos,t_ptSecondPos;
- HPEN hpen;
- int i = ::SetROP2(hdc,R2_XORPEN);
- hpen = ::CreatePen(0,0,m_color^::GetBkColor(hdc));
- HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- //外接矩形的左上点和右下点坐标
- t_ptFirstPos.x = m_ptBeginPo.x - nRadius;
- t_ptSecondPos.x = m_ptBeginPo.x + nRadius;
- t_ptFirstPos.y = m_ptBeginPo.y - nRadius;
- t_ptSecondPos.y = m_ptBeginPo.y + nRadius;
- //圆心的坐标
- int mPtx = m_ptBeginPo.x;
- int mPty = m_ptBeginPo.y;
- int Rect = 4;
- //top left
- Ellipse(hdc,t_ptFirstPos.x - Rect,t_ptFirstPos.y - Rect,
- t_ptFirstPos.x + Rect,t_ptFirstPos.y + Rect);
- //top right
- Ellipse(hdc,t_ptSecondPos.x - Rect,t_ptFirstPos.y - Rect,
- t_ptSecondPos.x + Rect,t_ptFirstPos.y + Rect);
- //top middle
- Ellipse(hdc,mPtx - Rect,t_ptFirstPos.y - Rect,
- mPtx + Rect,t_ptFirstPos.y + Rect);
- //left middle
- Ellipse(hdc,t_ptFirstPos.x - Rect,mPty - Rect,
- t_ptFirstPos.x + Rect,mPty + Rect);
- //right middle
- Ellipse(hdc,t_ptSecondPos.x - Rect,mPty - Rect,
- t_ptSecondPos.x + Rect,mPty + Rect);
- //buttom middle
- Ellipse(hdc,mPtx - Rect,t_ptSecondPos.y - Rect,
- mPtx + Rect,t_ptSecondPos.y + Rect);
- //buttom left
- Ellipse(hdc,t_ptFirstPos.x - Rect,t_ptSecondPos.y - Rect,
- t_ptFirstPos.x + Rect,t_ptSecondPos.y + Rect);
- //buttom right
- Ellipse(hdc,t_ptSecondPos.x - Rect,t_ptSecondPos.y - Rect,
- t_ptSecondPos.x + Rect,t_ptSecondPos.y + Rect);
- //m_ptBeginPos = m_pttempbeginpos;
- ::SetROP2(hdc,i);
- ::SelectObject(hdc,holdpen);
- ::DeleteObject(hpen);
- }
- void CCircle::SetPosBegin(CPoint point)
- {
- m_ptBeginPos = point;
- }
- void CCircle::SetPosEnd(CPoint point)
- {
- nRadius = point.x;
- }
- CPoint CCircle::GetPosBegin()
- {
- if(blncircletemp)
- {
- return m_ptBeginPos;
- }
- else
- {
- return m_FirstMirPt;
- }
- }
- CPoint CCircle::GetPosEnd()
- {
- CPoint point;
- point.x = nRadius;
- point.y = 0;
- return point;
- }
- void CCircle::Move(CDC *pDoc,CPoint point)
- {
- HDC hdc = pDoc->GetSafeHdc();
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( m_borderStyle,
- m_borderWidth,m_borderColor^pDoc->GetBkColor());
- HPEN hOldPen = (HPEN)::SelectObject(hdc,hPen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- //擦去前一次
- DrawCircle(hdc,m_ptBeginPos,nRadius);
- m_ptBeginPos.x = m_ptBeginPos.x + point.x - m_oldpt.x ;
- m_ptBeginPos.y = m_ptBeginPos.y + point.y - m_oldpt.y ;
- m_oldpt = point;
- //画出这一次
- DrawCircle(hdc,m_ptBeginPos,nRadius);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- }
- void CCircle::Update(CDC *pDC)
- {
- // this->DrawRect(pDC->GetSafeHdc(),m_pttempbeginpos,RGB(255,255,255));
- this->DrawRect(pDC->GetSafeHdc(),m_ptBeginPos,RGB(255,255,255));
- // HDC hdc = pDC->GetSafeHdc();
- // DrawCircle(hdc,m_ptBeginPos,nRadius);
- this->KillMirLine(pDC);
- }
- void CCircle::Mirror(CDC *pDC, CPoint point)
- {
- POINT curPt;
- curPt = point;
- m_ptPrePos = m_MirEndPt;
- m_MirEndPt = curPt;
- HDC hdc = pDC->GetSafeHdc();
- static CPoint FirstMirOldPt(0,0);
- FirstMirOldPt = m_FirstMirPt; //镜像后的老点
- m_FirstMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_ptBeginPos);
- DrawMirLine(pDC,m_MirBegPt,m_MirEndPt); //镜像线
- HPEN hpen = ::CreatePen(1,1,RGB(0,0,0)^pDC->GetBkColor());
- pDC->SelectObject(hpen);
- int i = pDC->SetROP2(R2_XORPEN);
- HBRUSH hbrush = (HBRUSH)pDC->SelectObject(::GetStockObject(NULL_BRUSH));
- this->DrawCircle(hdc,FirstMirOldPt,nRadius);
- this->DrawCircle(hdc,m_FirstMirPt,nRadius);
- pDC->SelectObject(hbrush);
- ::DeleteObject(hpen);
- // this->DrawCircle(hdc,m_FirstMirPt,nRadius);
- FirstMirOldPt = m_FirstMirPt;
- m_ptPrePos = curPt;
- pDC->SetROP2(i);
- blncircletemp = false;
- }
- POINT CCircle::GetMirPt(const POINT &ptBegin, const POINT &ptEnd, POINT ptCur)
- {
- POINT ptNew;
- double length = Distance(ptBegin,ptEnd);
- if(length == 0)
- return ptNew=ptCur;
- double t1 = 2. * ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.y-ptBegin.y)/length);
- double t2 = ((ptEnd.x-ptBegin.x)/length) * ((ptEnd.x-ptBegin.x)/length)
- - ((ptEnd.y-ptBegin.y)/length) * ((ptEnd.y-ptBegin.y)/length);
- ptNew.x =(int)(ptCur.x*t2 + ptCur.y*t1 + ptBegin.x*(-t2) - ptBegin.y*t1 + ptBegin.x);
- ptNew.y =(int)(ptCur.x*t1 + ptCur.y*(-t2) + ptBegin.y*t2 - ptBegin.x*t1 + ptBegin.y);
- return ptNew;
- }
- void CCircle::OnMirLBtnDn(CDC *pDC, CPoint point)
- {
- POINT curPoint; //左键点击的点
- curPoint.x = point.x ;
- curPoint.y = point.y ;
- m_MirBegPt = m_MirEndPt = m_ptPrePos = curPoint;
- }
- void CCircle::DrawMirLine(CDC *pDC, POINT MirBegPt, POINT MirEndPt)
- {
- HDC hdc = pDC->GetSafeHdc();
- int i = ::SetROP2(hdc,R2_XORPEN);
- HPEN hpen = ::CreatePen(0,0,RGB(255,0,0)^::GetBkColor(hdc));
- HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- pDC->MoveTo(MirBegPt.x,MirBegPt.y);
- pDC->LineTo(m_ptPrePos.x,m_ptPrePos.y);
- pDC->MoveTo(MirBegPt.x,MirBegPt.y);
- pDC->LineTo(MirEndPt.x,MirEndPt.y);
- pDC->SetROP2(i);
- pDC->SelectObject(hpen);
- // ::DeleteObject(newPen);
- ::DeleteObject(hpen);
- }
- void CCircle::KillMirLine(CDC *pDC)
- {
- HPEN newPen = CreatePen(0,0,pDC->GetBkColor());
- HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
- pDC->MoveTo(m_MirBegPt.x,m_MirBegPt.y);
- pDC->LineTo(m_MirEndPt.x,m_MirEndPt.y);
- pDC->SelectObject(oldPen);
- DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- void CCircle::Zoom(CDC *pDC, CPoint point)
- {
- HDC hdc = pDC->GetSafeHdc();
- int nOldMode = ::SetROP2(hdc,R2_XORPEN);
- HPEN hPen = ::CreatePen( m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor() );
- HPEN holdpen =(HPEN) ::SelectObject(hdc,hPen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- int oldnRadius = nRadius;
- //擦去前一次线段
- DrawCircle(hdc,m_ptBeginPos,oldnRadius);
- //画出这一次线段
- nRadius = Distance(m_ptBeginPos,point );
- DrawCircle(hdc,m_ptBeginPos,nRadius);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,holdpen);
- ::DeleteObject(hPen);
- // DrawCircle(hdc,m_ptBeginPos,nRadius);
- }
- void CCircle::Delete(CDC *pDC)
- {
- HDC hdc = pDC->GetSafeHdc();
- DrawRect(hdc,m_ptBeginPos);
- HPEN newPen = CreatePen(m_borderStyle,
- m_borderWidth,GetBkColor(hdc));
- SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
- ::SelectObject(hdc,newPen);
- DrawCircle(hdc,m_ptBeginPos,nRadius);
- ::DeleteObject(newPen);
- }