Trigon.cpp
资源名称:CAD2006.rar [点击查看]
上传用户:ckg1000
上传日期:2013-01-26
资源大小:630k
文件大小:15k
源码类别:
CAD
开发平台:
Visual C++
- // Trigon.cpp: implementation of the CTrigon class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CAD2006.h"
- #include "Trigon.h"
- #include "math.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- static bool blntrigon = false;
- static bool killround = false;
- //////////////////////////////////////////////////////////////////////
- // 三角形类, 陆立新 版本:2005-12-20
- //////////////////////////////////////////////////////////////////////
- CTrigon::CTrigon()
- {
- this->m_status = NoClicked;
- }
- CTrigon::~CTrigon()
- {
- }
- void CTrigon::Draw(CDC *pDC)
- {
- HPEN newPen = ::CreatePen(m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- pDC->SelectObject(newPen);
- int oldR2 = pDC->SetROP2(R2_XORPEN);
- if( m_status == FirstClicked)
- {
- pDC->MoveTo(m_firstPt.x, m_firstPt.y);
- pDC->LineTo(m_oldPt.x, m_oldPt.y);
- pDC->MoveTo(m_firstPt.x, m_firstPt.y);
- pDC->LineTo(m_secondPt.x, m_secondPt.y);
- }
- if( m_status == SecondClicked)
- {
- pDC->MoveTo( m_firstPt.x, m_firstPt.y);
- pDC->LineTo(m_oldPt.x, m_oldPt.y);
- pDC->MoveTo( m_secondPt.x, m_secondPt.y);
- pDC->LineTo(m_oldPt.x, m_oldPt.y);
- pDC->MoveTo( m_firstPt.x, m_firstPt.y);
- pDC->LineTo(m_thirdPt.x, m_thirdPt.y);
- pDC->MoveTo( m_secondPt.x, m_secondPt.y);
- pDC->LineTo(m_thirdPt.x, m_thirdPt.y);
- }
- pDC->SetROP2(oldR2);
- ::DeleteObject(newPen);
- if (KILLRECT == true)
- {
- HDC hdc = pDC->GetSafeHdc();
- ::SelectObject(hdc,GetStockObject(WHITE_BRUSH));
- DrawRound(pDC);
- KILLRECT = false;
- }
- }
- void CTrigon::DrawOnSedPtDown(CDC *pDC)
- {
- HPEN newPen = ::CreatePen(m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- pDC->SelectObject(newPen);
- int oldR2 = pDC->SetROP2(R2_XORPEN);
- pDC->MoveTo(m_firstPt.x, m_firstPt.y);
- pDC->LineTo(m_secondPt.x, m_secondPt.y);
- pDC->SetROP2(oldR2);
- DeleteObject(newPen);
- }
- void CTrigon::OnLbuttondown(CDC *pDC,CPoint point) //左键点击
- {
- POINT curPoint; //左键点击的点
- curPoint.x = point.x;
- curPoint.y = point.y;
- switch(m_status)
- {
- case NoClicked:
- {
- m_firstPt = m_oldPt = point;
- m_status = FirstClicked;
- }
- break;
- case FirstClicked:
- {
- m_secondPt = m_oldPt = point;
- m_status = SecondClicked;
- DrawOnSedPtDown(pDC); //先画好第一条边再说
- }
- break;
- case SecondClicked:
- {
- m_thirdPt = point;
- m_status = NoClicked;
- }
- break;
- }
- }
- //鼠标移动
- void CTrigon::Onmousemove(CDC *pDC,CPoint point)
- {
- POINT curPoint;
- curPoint.x = point.x;
- curPoint.y = point.y;
- if( m_status == FirstClicked )
- {
- m_secondPt = point;
- Draw(pDC);
- m_oldPt = point;
- }
- if( m_status == SecondClicked )
- {
- m_thirdPt = point;
- Draw(pDC);
- m_oldPt = point;
- }
- }
- //求两点之间的距离
- int CTrigon::Distance(POINT Pt1, POINT Pt2)
- {
- int nDist;
- nDist =(int)(sqrt((Pt1.x - Pt2.x)*(Pt1.x - Pt2.x) + (Pt1.y - Pt2.y)*(Pt1.y - Pt2.y)));
- return nDist;
- }
- //判断一个点是否在三角形上
- bool CTrigon::IsOnTrigon(POINT curPt)
- {
- bool yesno= false;
- int Dist1_2 = Distance(m_firstPt, m_secondPt);
- int Dist1_3 = Distance(m_firstPt, m_thirdPt);
- int Dist2_3 = Distance(m_secondPt, m_thirdPt);
- int DistPt_1 = Distance(curPt, m_firstPt);
- int DistPt_2 = Distance(curPt, m_secondPt);
- int DistPt_3 = Distance(curPt, m_thirdPt);
- if((DistPt_1 + DistPt_2 <= Dist1_2+1)||(DistPt_1 + DistPt_3 <= Dist1_3+1)||(DistPt_2 + DistPt_3 <= Dist2_3+1))
- {
- yesno= true;
- }
- return yesno;
- }
- //画小圆圈(默认红色)
- void CTrigon::DrawRound(CDC *pDC,COLORREF colorRound )
- {
- POINT centPt; //三角形重心
- centPt.x = (m_firstPt.x + m_secondPt.x + m_thirdPt.x)/3;
- centPt.y = (m_firstPt.y + m_secondPt.y + m_thirdPt.y)/3;
- POINT FitsgPt = CaleSignPt(centPt, m_firstPt);
- POINT SedsgPt = CaleSignPt(centPt, m_secondPt);
- POINT ThdsgPt = CaleSignPt(centPt, m_thirdPt);
- POINT FirstPtL;
- FirstPtL.x = FitsgPt.x-4, FirstPtL.y = FitsgPt.y-4;
- POINT FirstPtR;
- FirstPtR.x = FitsgPt.x+4, FirstPtR.y = FitsgPt.y+4;
- POINT SecondPtL;
- SecondPtL.x = SedsgPt.x-4, SecondPtL.y = SedsgPt.y-4;
- POINT SecondPtR;
- SecondPtR.x = SedsgPt.x+4, SecondPtR.y = SedsgPt.y+4;
- POINT ThirdPtL;
- ThirdPtL.x = ThdsgPt.x-4, ThirdPtL.y = ThdsgPt.y-4;
- POINT ThirdPtR;
- ThirdPtR.x = ThdsgPt.x+4, ThirdPtR.y = ThdsgPt.y+4;
- /* int i = pDC->SetROP2(R2_XORPEN);
- HPEN hpen = ::CreatePen(0,0,colorRound^pDC->GetBkColor());
- HPEN holdpen =(HPEN)pDC->SelectObject(hpen);
- pDC->SelectObject(GetStockObject(NULL_BRUSH));
- */
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = ::CreatePen(0,0, colorRound);
- HBRUSH newBrush = ::CreateSolidBrush(colorRound);
- SelectObject(hdc,newPen);
- SelectObject(hdc,newBrush);
- Ellipse(hdc,FirstPtL.x,FirstPtL.y,FirstPtR.x,FirstPtR.y);
- Ellipse(hdc,SecondPtL.x,SecondPtL.y,SecondPtR.x,SecondPtR.y);
- Ellipse(hdc,ThirdPtL.x,ThirdPtL.y,ThirdPtR.x,ThirdPtR.y);
- // pDC->SetROP2(i);
- DeleteObject(newPen);
- DeleteObject(newBrush);
- }
- //选定
- bool CTrigon::Select(CDC *pDC,CPoint point)
- {
- KILLRECT = true;
- bool yesno = false;
- POINT curPt;
- curPt.x = point.x ;
- curPt.y = point.y ;
- if(IsOnTrigon(curPt) == true)
- {
- DrawRound(pDC);
- m_oldPt = curPt;
- yesno = true;
- }
- return yesno;
- }
- //选定一个顶点
- bool CTrigon::SelectOnePt(CDC *pDC,CPoint point)
- {
- KILLRECT = true;
- bool yesno = false;
- POINT curPt;
- curPt.x = point.x ;
- curPt.y = point.y ;
- if ( (curPt.x == m_firstPt.x && curPt.y == m_firstPt.y)
- ||(curPt.x == m_secondPt.x && curPt.y == m_secondPt.y)
- ||(curPt.x == m_thirdPt.x && curPt.y == m_thirdPt.y) )
- {
- DrawRound(pDC);
- m_oldPt = curPt;
- yesno = true;
- }
- return yesno;
- }
- //三角形移动时的画方法
- void CTrigon::DrawMove(CDC *pDC,POINT FirstPt, POINT SecondPt, POINT ThirdPt,
- POINT FirstOldPt, POINT SecondOldPt, POINT ThirdOldPt)
- {
- HPEN newPen = ::CreatePen(m_borderStyle,
- m_borderWidth,m_borderColor^pDC->GetBkColor());
- pDC->SelectObject(newPen);
- int oldR2 = pDC->SetROP2(R2_XORPEN);
- pDC->MoveTo( FirstOldPt.x, FirstOldPt.y);
- pDC->LineTo(SecondOldPt.x, SecondOldPt.y);
- pDC->MoveTo(FirstOldPt.x, FirstOldPt.y);
- pDC->LineTo(ThirdOldPt.x, ThirdOldPt.y);
- pDC->MoveTo(SecondOldPt.x, SecondOldPt.y);
- pDC->LineTo(ThirdOldPt.x, ThirdOldPt.y);
- ////////
- pDC->MoveTo(FirstPt.x, FirstPt.y);
- pDC->LineTo(SecondPt.x, SecondPt.y);
- pDC->MoveTo(FirstPt.x, FirstPt.y);
- pDC->LineTo(ThirdPt.x, ThirdPt.y);
- pDC->MoveTo(SecondPt.x, SecondPt.y);
- pDC->LineTo(ThirdPt.x, ThirdPt.y);
- pDC->SetROP2(oldR2);
- ::DeleteObject(newPen);
- }
- //图形移动
- void CTrigon::Move(CDC *pDC,CPoint point)
- {
- static bool bln = false;
- POINT curPoint;
- curPoint.x = point.x;
- curPoint.y = point.y;
- POINT FirstOldPt = m_firstPt; //存老点
- POINT SecondOldPt = m_secondPt;
- POINT ThirdOldPt = m_thirdPt;
- m_firstPt.x = FirstOldPt.x + curPoint.x - m_oldPt.x; //重新计算每点坐标
- m_firstPt.y = FirstOldPt.y + curPoint.y - m_oldPt.y;
- m_secondPt.x = SecondOldPt.x + curPoint.x - m_oldPt.x;
- m_secondPt.y = SecondOldPt.y + curPoint.y - m_oldPt.y;
- m_thirdPt.x = ThirdOldPt.x + curPoint.x - m_oldPt.x;
- m_thirdPt.y = ThirdOldPt.y + curPoint.y - m_oldPt.y;
- DrawMove(pDC,m_firstPt,m_secondPt,m_thirdPt,FirstOldPt, SecondOldPt,ThirdOldPt);
- m_oldPt = curPoint;
- }
- //求一点关于直线对称的点
- POINT CTrigon::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 CTrigon::OnMirLBtnDn(CDC *pDC,CPoint point)
- {
- POINT curPoint; //左键点击的点
- curPoint.x = point.x ;
- curPoint.y = point.y ;
- m_MirBegPt = m_MirEndPt = m_oldPt = curPoint;
- }
- //画镜像直线
- void CTrigon::DrawMirLine(CDC *pDC,POINT MirBegPt, POINT MirEndPt)
- {
- HPEN newPen = CreatePen(0,0,RGB(255,0,0)^pDC->GetBkColor());
- HPEN oldPen = (HPEN)pDC->SelectObject(newPen);
- int oldR2 = pDC->SetROP2(R2_XORPEN);
- pDC->MoveTo(MirBegPt.x,MirBegPt.y);
- pDC->LineTo(m_oldPt.x,m_oldPt.y);
- pDC->MoveTo(MirBegPt.x,MirBegPt.y);
- pDC->LineTo(MirEndPt.x,MirEndPt.y);
- pDC->SetROP2(oldR2);
- pDC->SelectObject(oldPen);
- ::DeleteObject(newPen);
- ::DeleteObject(oldPen);
- }
- //销毁镜像直线
- void CTrigon::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 CTrigon::Mirror(CDC *pDC,CPoint point)
- {
- POINT curPt;
- curPt = point;
- m_oldPt = m_MirEndPt;
- m_MirEndPt = curPt;
- POINT FirstMirOldPt = m_FirstMirPt; //镜像后的老点
- POINT SecondMirOldPt = m_SecondMirPt;
- POINT ThirdMirOldPt = m_ThirdMirPt;
- m_FirstMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_firstPt);
- m_SecondMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_secondPt);
- m_ThirdMirPt = GetMirPt(m_MirBegPt, m_MirEndPt, m_thirdPt);
- DrawMirLine(pDC,m_MirBegPt,m_MirEndPt); //镜像线
- DrawMove(pDC,m_FirstMirPt, m_SecondMirPt, m_ThirdMirPt,
- FirstMirOldPt, SecondMirOldPt, ThirdMirOldPt);
- m_oldPt = curPt;
- this->blntemp = false;
- killround = true;
- }
- //更新图形
- void CTrigon::Update(CDC *pDC)
- {
- HDC hdc = pDC->GetSafeHdc();
- DrawRound(pDC,GetBkColor(hdc)); //先擦掉小圆圈
- KillMirLine(pDC); //擦掉镜像直线
- if( killround )
- {
- KillRound(pDC);
- killround = false;
- }
- }
- //缩放
- void CTrigon::Zoom(CDC *pDC,CPoint point) //固定两点,缩放另一点
- {
- POINT curPoint;
- curPoint.x = point.x ;
- curPoint.y = point.y ;
- POINT FirstOldPt = m_firstPt; //存老点
- POINT SecondOldPt = m_secondPt;
- POINT ThirdOldPt = m_thirdPt;
- if(FirstOldPt.x == m_oldPt.x && FirstOldPt.y == m_oldPt.y)
- {
- m_firstPt = curPoint;
- }
- if(SecondOldPt.x == m_oldPt.x && SecondOldPt.y == m_oldPt.y)
- {
- m_secondPt = curPoint;
- }
- if(ThirdOldPt.x == m_oldPt.x && ThirdOldPt.y == m_oldPt.y)
- {
- m_thirdPt = curPoint;
- }
- DrawMove(pDC,m_firstPt,m_secondPt,m_thirdPt,FirstOldPt, SecondOldPt,ThirdOldPt);
- m_oldPt = curPoint;
- }
- CPoint CTrigon::GetPosBegin()
- {
- if( blntemp )
- {
- return m_firstPt;
- }
- else
- {
- return m_FirstMirPt;
- }
- }
- CPoint CTrigon::GetPosCenter()
- {
- if( blntemp )
- {
- return m_secondPt;
- }
- else
- {
- return m_SecondMirPt;
- }
- }
- CPoint CTrigon::GetPosEnd()
- {
- if( blntemp )
- {
- return m_thirdPt;
- }
- else
- {
- blntemp = true;
- return m_ThirdMirPt;
- }
- }
- void CTrigon::SetPosBegin(CPoint point) //传起点
- {
- m_firstPt = point;
- }
- void CTrigon::SetPosCenter(CPoint point)
- {
- m_secondPt = point;
- }
- void CTrigon::SetPosEnd(CPoint point)
- {
- m_thirdPt = point;
- }
- //旋转
- void CTrigon::Rotate(CDC *pDC,CPoint point) //围绕第一个点转
- {
- Update(pDC);
- double Pi = 3.14159;
- double radian = Pi * m_angle/180; //化成弧度
- POINT secondOldPt = m_secondPt;
- POINT thirdOldPt = m_thirdPt;
- m_secondPt = CalRotatePt(m_firstPt, secondOldPt, radian);
- m_thirdPt = CalRotatePt(m_firstPt, thirdOldPt, radian);
- DrawMove(pDC,m_firstPt,m_secondPt,m_thirdPt,m_firstPt, secondOldPt,thirdOldPt);
- }
- // 功 能: 计算点绕指定点旋转dbAngle个角度后的点
- // 输入参数: 定点:POINT ptCenter 待求点:POINT ptPos
- // 弧度 double dbAngle
- // 返 回 值: 旋转到的位置 POINT ptDest;
- POINT CTrigon::CalRotatePt(POINT ptCenter,POINT ptPos,double dbAngle)
- {
- struct tagMatrix //旋转矩阵
- {
- double r11, r12, r13;
- double r21, r22, r23;
- double r31, r32, r33;
- }matrix;
- matrix.r11 = (double)cos(dbAngle);
- matrix.r21 = (double)sin(dbAngle);
- matrix.r31 = 0.0;
- matrix.r12 = (double)(-sin(dbAngle));
- matrix.r22 = (double)cos(dbAngle);
- matrix.r32 = 0.0;
- matrix.r13 = (double)((1 - cos(dbAngle)) * ptCenter.x + ptCenter.y * sin(dbAngle));
- matrix.r23 = (double)((1 - cos(dbAngle)) * ptCenter.y - ptCenter.x * sin(dbAngle));
- matrix.r33 = 1.0;
- POINT ptDest;
- ptDest.x = (int)(matrix.r11 * ptPos.x + matrix.r12 * ptPos.y + matrix.r13);
- ptDest.y = (int)(matrix.r21 * ptPos.x + matrix.r22 * ptPos.y + matrix.r23);
- return ptDest;
- }
- void CTrigon::Delete(CDC *pDC)
- {
- Update(pDC);
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = ::CreatePen(m_borderStyle,m_borderWidth, GetBkColor(hdc));
- ::SelectObject(hdc,newPen);
- ::MoveToEx(hdc, m_firstPt.x, m_firstPt.y, NULL);
- ::LineTo(hdc,m_secondPt.x, m_secondPt.y);
- ::MoveToEx(hdc, m_firstPt.x, m_firstPt.y, NULL);
- ::LineTo(hdc,m_thirdPt.x, m_thirdPt.y);
- ::MoveToEx(hdc, m_secondPt.x, m_secondPt.y, NULL);
- ::LineTo(hdc,m_thirdPt.x, m_thirdPt.y);
- ::DeleteObject(newPen);
- }
- void CTrigon::SetAngle(int angle)
- {
- m_angle = angle;
- }
- //计算一条线末端外的标记点
- POINT CTrigon::CaleSignPt(POINT centPt, POINT orderPt)
- {
- int dLen = 6; //标记点与末端点的距离
- POINT signPt;
- int r = Distance(centPt, orderPt);
- int xLen = abs(centPt.x - orderPt.x);
- int yLen = abs(centPt.y - orderPt.y);
- int xAdd = dLen * xLen/r;
- int yAdd = dLen * yLen/r;
- if(centPt.x <= orderPt.x && centPt.y >= orderPt.y) //第一象限
- {
- signPt.x = orderPt.x + xAdd;
- signPt.y = orderPt.y - yAdd;
- }
- if(centPt.x <= orderPt.x && centPt.y <= orderPt.y) //第二象限
- {
- signPt.x = orderPt.x + xAdd;
- signPt.y = orderPt.y + yAdd;
- }
- if(centPt.x >= orderPt.x && centPt.y <= orderPt.y) //第三象限
- {
- signPt.x = orderPt.x - xAdd;
- signPt.y = orderPt.y + yAdd;
- }
- if(centPt.x >= orderPt.x && centPt.y >= orderPt.y) //第四象限
- {
- signPt.x = orderPt.x - xAdd;
- signPt.y = orderPt.y - yAdd;
- }
- return signPt;
- }
- void CTrigon::KillRound(CDC *pDC)
- {
- POINT centPt; //三角形重心
- centPt.x = (m_firstPt.x + m_secondPt.x + m_thirdPt.x)/3;
- centPt.y = (m_firstPt.y + m_secondPt.y + m_thirdPt.y)/3;
- POINT FitsgPt = CaleSignPt(centPt, m_firstPt);
- POINT SedsgPt = CaleSignPt(centPt, m_secondPt);
- POINT ThdsgPt = CaleSignPt(centPt, m_thirdPt);
- POINT FirstPtL;
- FirstPtL.x = FitsgPt.x-4, FirstPtL.y = FitsgPt.y-4;
- POINT FirstPtR;
- FirstPtR.x = FitsgPt.x+4, FirstPtR.y = FitsgPt.y+4;
- POINT SecondPtL;
- SecondPtL.x = SedsgPt.x-4, SecondPtL.y = SedsgPt.y-4;
- POINT SecondPtR;
- SecondPtR.x = SedsgPt.x+4, SecondPtR.y = SedsgPt.y+4;
- POINT ThirdPtL;
- ThirdPtL.x = ThdsgPt.x-4, ThirdPtL.y = ThdsgPt.y-4;
- POINT ThirdPtR;
- ThirdPtR.x = ThdsgPt.x+4, ThirdPtR.y = ThdsgPt.y+4;
- HDC hdc = pDC->GetSafeHdc();
- HPEN newPen = ::CreatePen(0,0, RGB(255,255,255));
- // HBRUSH newBrush = ::CreateSolidBrush(colorRound);
- SelectObject(hdc,newPen);
- // SelectObject(hdc,newBrush);
- Ellipse(hdc,FirstPtL.x,FirstPtL.y,FirstPtR.x,FirstPtR.y);
- Ellipse(hdc,SecondPtL.x,SecondPtL.y,SecondPtR.x,SecondPtR.y);
- Ellipse(hdc,ThirdPtL.x,ThirdPtL.y,ThirdPtR.x,ThirdPtR.y);
- // DeleteObject(newBrush);
- }