Ellipse.cpp
资源名称:CAD2006.rar [点击查看]
上传用户:ckg1000
上传日期:2013-01-26
资源大小:630k
文件大小:10k
源码类别:
CAD
开发平台:
Visual C++
- // Ellipse.cpp: implementation of the CEllipse class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CAD2006.h"
- #include "Ellipse.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- extern enum tagToolState{Init_State,FirstHit,SecondHit};
- CEllipse::CEllipse()
- {
- m_eToolState = Init_State;
- }
- CEllipse::~CEllipse()
- {
- }
- void CEllipse::OnLbuttondown(CDC *pDC,CPoint point)
- {
- if(m_eToolState == Init_State)
- {
- m_eToolState = FirstHit;
- m_ptBeginPos = m_ptPrePos = point;
- }
- else if(m_eToolState == FirstHit)
- {
- m_eToolState = SecondHit;
- }
- }
- void CEllipse::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));
- //擦去前一次线段
- DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
- //画出这一次线段
- DrawEllipse(hdc,m_ptBeginPos,point);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- m_ptPrePos = point;
- }
- }
- //画椭圆
- void CEllipse::DrawEllipse(HDC hdc,POINT ptCenter,POINT ptSide)
- {
- Ellipse(hdc,ptCenter.x,ptCenter.y,ptSide.x,ptSide.y);
- m_ptPrePos = ptSide;
- if (KILLRECT == true)
- {
- ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRect(hdc);
- KILLRECT = false;
- }
- }
- void CEllipse::SetPosEnd(CPoint point)
- {
- m_ptBeginPos = point;
- }
- void CEllipse::SetPosBegin(CPoint point)
- {
- // nRadius = point.x;
- m_ptPrePos = point;
- }
- CPoint CEllipse::GetPosEnd()
- {
- return m_ptBeginPos;
- }
- CPoint CEllipse::GetPosBegin()
- {
- /* CPoint point;
- point.x = nRadius;
- point.y = 0;
- return point;*/
- return m_ptPrePos;
- }
- bool CEllipse::Select(CDC *pDC,CPoint point)
- {
- if( pick(point) )
- {
- HDC hdc = pDC->GetSafeHdc();
- SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRect(hdc);
- m_oldpt = point;
- KILLRECT = true;
- return true;
- }
- return false;
- }
- void CEllipse::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));
- CPoint oldfristpoint = m_ptBeginPos;
- CPoint oldsecondpoint = m_ptPrePos;
- //擦去前一次
- DrawEllipse(hdc,oldfristpoint,oldsecondpoint);
- m_ptBeginPos.x = m_ptBeginPos.x + point.x - m_oldpt.x ;
- m_ptBeginPos.y = m_ptBeginPos.y + point.y - m_oldpt.y ;
- m_ptPrePos.x = m_ptPrePos.x + point.x - m_oldpt.x ;
- m_ptPrePos.y = m_ptPrePos.y + point.y - m_oldpt.y ;
- m_oldpt = point;
- //画出这一次
- DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
- ::SetROP2(hdc,nOldMode);
- ::SelectObject(hdc,hOldPen);
- ::DeleteObject(hPen);
- }
- bool CEllipse::pick(POINT ptCurPos)
- {
- POINT a1_Temp, a2_Temp, b1_Temp, b2_Temp;//椭圆四个顶点
- POINT Center_Temp; //椭圆中心点
- POINT F1, F2; //椭圆焦点
- a1_Temp.x = m_ptBeginPos.x-(int)(m_ptBeginPos.x - m_ptPrePos.x)/4;
- a1_Temp.y = m_ptBeginPos.y-(int)(m_ptBeginPos.y - m_ptPrePos.y)/2;
- a2_Temp.x = m_ptPrePos.x;
- a2_Temp.y = m_ptBeginPos.y + (int)(fabs(( m_ptBeginPos.y -m_ptPrePos.y )/2));
- b1_Temp.x = m_ptBeginPos.x +(int)(fabs((m_ptPrePos.x - m_ptBeginPos.x)/2));
- b1_Temp.y = m_ptBeginPos.y;
- b2_Temp.x = m_ptBeginPos.x +(int)(fabs((m_ptPrePos.x - m_ptBeginPos.x)/2));
- b2_Temp.y = m_ptPrePos.y;
- Center_Temp.x = m_ptBeginPos.x +(int)(fabs((m_ptPrePos.x - m_ptBeginPos.x)/2));
- Center_Temp.y = m_ptBeginPos.y + (int)(fabs(( m_ptBeginPos.y -m_ptPrePos.y )/2));
- double n_A = Distance(a1_Temp, Center_Temp);
- double n_B = Distance(b1_Temp, Center_Temp);
- double n_C;
- if (n_A > n_B)
- {
- n_C = sqrt(n_A*n_A - n_B*n_B);
- F1.x = (int)(Center_Temp.x-n_C);
- F1.y = Center_Temp.y;
- F2.x = (int)(Center_Temp.x+n_C);
- F2.y = Center_Temp.y;
- int n_Temp = Distance(F1, ptCurPos) + Distance(F2, ptCurPos);
- int n_1Temp = Distance(a1_Temp, a2_Temp);
- if( fabs(n_Temp - n_1Temp) < 8 )
- {
- return true;
- }
- }
- else
- {
- n_C = sqrt(n_B*n_B-n_A*n_A);
- F1.y = (int)(Center_Temp.y -n_C);
- F1.x = Center_Temp.x ;
- F2.y = (int)(Center_Temp.y +n_C);
- F2.x = Center_Temp.x ;
- int n_Temp = Distance(F1, ptCurPos) + Distance(F2, ptCurPos);
- int n_1Temp = Distance(b1_Temp,b2_Temp);
- if( fabs(n_Temp - n_1Temp) < 8 )
- {
- return true;
- }
- }
- return false;
- }
- void CEllipse::DrawRect(HDC hdc)
- {
- POINT t_ptFirstPos,t_ptSecondPos;
- //外接矩形的左上点和右下点坐标
- t_ptFirstPos.x = m_ptBeginPos.x-(int)(m_ptBeginPos.x - m_ptPrePos.x)/4;
- t_ptFirstPos.y = m_ptBeginPos.y-(int)(m_ptBeginPos.y - m_ptPrePos.y)/2;
- t_ptSecondPos.x = m_ptPrePos.x;
- t_ptSecondPos.y = m_ptPrePos.y;
- HPEN hpen;
- int i = ::SetROP2(hdc,R2_XORPEN);
- hpen = ::CreatePen(0,0,RGB(255,0,0)^::GetBkColor(hdc));
- HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- //左中点
- Ellipse(hdc,m_ptBeginPos.x - 4,t_ptFirstPos.y - 4,
- m_ptBeginPos.x + 4,t_ptFirstPos.y + 4);
- //上边中点
- Ellipse(hdc,m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2))- 4,m_ptBeginPos.y - 4,
- m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2)) + 4,m_ptBeginPos.y + 4);
- //右中点
- Ellipse(hdc,t_ptSecondPos.x - 4,m_ptBeginPos.y + (int)fabs(( m_ptBeginPos.y -t_ptSecondPos.y )/2) - 4,
- t_ptSecondPos.x + 4,m_ptBeginPos.y + (int)fabs(( m_ptBeginPos.y -t_ptSecondPos.y )/2) + 4);
- //下底中点
- Ellipse(hdc,m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2)) - 4,t_ptSecondPos.y - 4,
- m_ptBeginPos.x +(int)(fabs((t_ptSecondPos.x - m_ptBeginPos.x)/2)) + 4,t_ptSecondPos.y + 4);
- ::SetROP2(hdc,i);
- ::SelectObject(hdc,holdpen);
- ::DeleteObject(hpen);
- }
- //功能:求两点间距
- int CEllipse::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 CEllipse::SetPosCenter(CPoint point)
- {
- m_ptPrePos = point;
- }
- CPoint CEllipse::GetPosCenter()
- {
- return m_ptPrePos;
- }
- void CEllipse::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->DrawEllipse(hdc,FirstMirOldPt,m_ptBeginPos);
- this->DrawEllipse(hdc,m_FirstMirPt,m_ptBeginPos);
- pDC->SelectObject(hbrush);
- ::DeleteObject(hpen);
- // this->DrawCircle(hdc,m_FirstMirPt,nRadius);
- FirstMirOldPt = m_FirstMirPt;
- m_ptPrePos = curPt;
- pDC->SetROP2(i);
- }
- void CEllipse::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 CEllipse::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);
- }
- POINT CEllipse::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 CEllipse::Zoom(CDC *pDoc, CPoint point)
- {
- // Update(pDoc);
- POINT curPt;
- curPt.x = point.x,curPt.y = point.y;
- POINT begOldPt = m_ptBeginPos;
- POINT endOldPt = m_ptPrePos;
- HPEN hpen;
- HDC hdc = pDoc->GetSafeHdc();
- int i = ::SetROP2(hdc,R2_XORPEN);
- hpen = ::CreatePen(0,0,RGB(0,0,0)^::GetBkColor(hdc));
- HPEN holdpen =(HPEN) ::SelectObject(hdc,hpen);
- ::SelectObject(hdc,GetStockObject(NULL_BRUSH));
- POINT oldfirstpoint;
- oldfirstpoint = m_ptBeginPos;
- POINT oldsecondpoint;
- oldsecondpoint = m_ptPrePos;
- DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
- if(m_oldpt.y == begOldPt.y) //选到上边
- {
- m_ptBeginPos.y = curPt.y;
- }
- if(m_oldpt.y == endOldPt.y) //下边
- {
- m_ptPrePos.y = curPt.y;
- }
- if(m_oldpt.x == begOldPt.x) //左边
- {
- m_ptBeginPos.x = curPt.x;
- }
- if(m_oldpt.x == endOldPt.x) //右边
- {
- m_ptPrePos.x = curPt.x;
- }
- DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
- ::SetROP2(hdc,i);
- ::SelectObject(hdc,holdpen);
- ::DeleteObject(hpen);
- m_oldpt = curPt;
- }
- void CEllipse::Delete(CDC *pDC)
- {
- HDC hdc = pDC->GetSafeHdc();
- DrawRect(hdc);
- HPEN newPen = CreatePen(m_borderStyle,
- m_borderWidth,GetBkColor(hdc));
- SelectObject(hdc,GetStockObject(NULL_BRUSH)); //空画刷
- ::SelectObject(hdc,newPen);
- DrawEllipse(hdc,m_ptBeginPos,m_ptPrePos);
- ::DeleteObject(newPen);
- }