⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cellipse.cpp

📁 绘图软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include"CEllipse.h"
#include"EastDrawView.h"
IMPLEMENT_SERIAL(CEllipse,CUnit,1)
void CEllipse::DrawStatic(CDC*pDC)
{  
  CPen m_pen;
  CBrush m_brush;
  int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  LOGBRUSH brushlog;
  brushlog.lbColor=m_BrushColor;
  brushlog.lbHatch=m_BrushHatch;
  brushlog.lbStyle=m_BrushStyle;
  m_brush.CreateBrushIndirect(&brushlog);
  SetBrushOrg(pDC,&m_brush);
  CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
 
 int OldBkMode=pDC->SetBkMode(m_BkMode);
 COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
 
 ellipseMidpoint(pDC,m_CenterPoint.x,m_CenterPoint.y,this->m_Xr,this->m_Yr);
 pDC->SelectObject(brush);
 pDC->SelectObject(pen);
 pDC->SetBkMode(OldBkMode);
 pDC->SetBkColor(OldColor);
 pDC->SetROP2(oldDrawingMode);

}

void CEllipse::Serialize(CArchive &ar)
{
	CUnit::Serialize(ar);
    if(ar.IsStoring())
	{
	  ar<<m_BrushStyle<<m_BkMode<<m_BrushHatch<<m_BrushColor;
	  ar<<m_Sin<<m_Cos;
	  ar<<m_Xr<<m_Yr;
	  ar<<m_CenterPoint;
	}
	else
	{
	  ar>>m_BrushStyle>>m_BkMode>>m_BrushHatch>>m_BrushColor;
	  ar>>m_Sin>>m_Cos;
	  ar>>m_Xr>>m_Yr;
	  ar>>m_CenterPoint;
	}
	
}
BOOL CEllipse::IsInRgn(CPoint point)
{
    
double vh[3][3]={m_Cos,-m_Sin,m_CenterPoint.x*(1-m_Cos)+m_CenterPoint.y*m_Sin,
		               m_Sin, m_Cos,m_CenterPoint.y*(1-m_Cos)-m_CenterPoint.x*m_Sin,
					   0,0,1
	                  };       
  
     
       
  double xy[3];
  CPoint point1=CPoint(m_CenterPoint.x-m_Xr,m_CenterPoint.y+m_Yr);
  CPoint point2=CPoint(m_CenterPoint.x+m_Xr,m_CenterPoint.y+m_Yr);
  CPoint point3=CPoint(m_CenterPoint.x+m_Xr,m_CenterPoint.y-m_Yr);
  CPoint point4=CPoint(m_CenterPoint.x-m_Xr,m_CenterPoint.y-m_Yr);
  CPoint point11=CPoint(m_CenterPoint.x-m_Xr/1.2,m_CenterPoint.y+m_Yr/1.2);
  CPoint point22=CPoint(m_CenterPoint.x+m_Xr/1.2,m_CenterPoint.y+m_Yr/1.2);
  CPoint point33=CPoint(m_CenterPoint.x+m_Xr/1.2,m_CenterPoint.y-m_Yr/1.2);
  CPoint point44=CPoint(m_CenterPoint.x-m_Xr/1.2,m_CenterPoint.y-m_Yr/1.2);
	   
  CPoint pointList1[]={point1,point2,point3,point4};
  CPoint pointList2[]={point11,point22,point33,point44};
  
  for(int i=0;i<4;i++)
  {
  xy[0]=pointList1[i].x;
  xy[1]=pointList1[i].y;
  xy[2]=1;
  pointList1[i].x=vh[0][0]*xy[0]+vh[0][1]*xy[1]+vh[0][2]*xy[2];
  pointList1[i].y=vh[1][0]*xy[0]+vh[1][1]*xy[1]+vh[1][2]*xy[2];
  
  xy[0]=pointList2[i].x;
  xy[1]=pointList2[i].y;
  xy[2]=1;
  pointList2[i].x=vh[0][0]*xy[0]+vh[0][1]*xy[1]+vh[0][2]*xy[2];
  pointList2[i].y=vh[1][0]*xy[0]+vh[1][1]*xy[1]+vh[1][2]*xy[2];
  }

  CRgn rgn1;
  rgn1.CreatePolygonRgn(pointList1,4,ALTERNATE);
  CRgn rgn2;
  rgn2.CreatePolygonRgn(pointList2,4,ALTERNATE);
  if(rgn1.PtInRegion(point)&&!rgn2.PtInRegion(point))
  return true;
  
  return false;
	
}
int CEllipse::IsOnMarginPoint(CPoint point)
{

      CRect rect;
	  for(int i=0;i<5;i++)
	  {
	  rect=CRect(this->m_MarginPoint[i],this->m_MarginPoint[i]);
	  rect.InflateRect(4,4);
      if(rect.PtInRect(point))
	  {   
		  m_FoundPoint=this->m_MarginPoint[i];
		  return i+1;
	  }
   }
return 0;
 }

void CEllipse::ellipseMidpoint(CDC*pDC,int xCenter, int yCenter, int Rx, int Ry)
{

int Rx2=Rx*Rx;
int Ry2=Ry*Ry;
int twoRx2=2*Rx2;
int twoRy2=2*Ry2;
int p;
int x=0;
int y=Ry;
int px=0;
int py=twoRx2*y;
ellipsePlotPoints(pDC,xCenter,yCenter,x,y);
p=ROUND(Ry2-(Rx2*Ry)+(0.25*Rx2));
while(px<py)
{
	x++;
	px+=twoRy2;
	if(p<0)
		p+=Ry2+px;
	else
	{
		y--;
		py-=twoRx2;
		p+=Ry2+px-py;
	}
	ellipsePlotPoints(pDC,xCenter,yCenter,x,y);
}
p=ROUND(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
while(y>0)
{
	y--;
	py-=twoRx2;
	if(p>0)
		p+=Rx2-py;
	else
	{
		x++;
		px+=twoRy2;
		p+=Rx2-py+px;
	}
	ellipsePlotPoints(pDC,xCenter,yCenter,x,y);
}
}

void CEllipse::ellipsePlotPoints(CDC*pDC,int xCenter, int yCenter, int x, int y)
{
 /* CPen m_pen;
  CBrush m_brush;
  int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  LOGBRUSH brushlog;
  brushlog.lbColor=m_BrushColor;
  brushlog.lbHatch=m_BrushHatch;
  brushlog.lbStyle=m_BrushStyle;

  m_brush.CreateBrushIndirect(&brushlog);
 
  CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
 
 int OldBkMode=pDC->SetBkMode(m_BkMode);
 COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
*/
PdcSetPixel(pDC,xCenter+x,yCenter+y,m_PenColor);
PdcSetPixel(pDC,xCenter-x,yCenter+y,m_PenColor);
PdcSetPixel(pDC,xCenter+x,yCenter-y,m_PenColor);
PdcSetPixel(pDC,xCenter-x,yCenter-y,m_PenColor);
/*
 pDC->SelectObject(brush);
 pDC->SelectObject(pen);
 pDC->SetBkMode(OldBkMode);
 pDC->SetBkColor(OldColor);
 pDC->SetROP2(oldDrawingMode);
 */
}

float CEllipse::ComputSloap(CPoint firstPoint, CPoint secondPoint)
{
    m_SecondPoint=secondPoint;
	double sloap;
	if(firstPoint.x==secondPoint.x)
	{
	  sloap=9999999999999.0;
	}
	else
	{
	sloap=double((firstPoint.y-secondPoint.y))/double((firstPoint.x-secondPoint.x));
	}
   
  if(sloap<0)
   {
   m_Cos=fabs(sloap/sqrt(sloap*sloap+1.0));
   m_Sin=1.0/sqrt(sloap*sloap+1.0);
   }
   else
   {
   m_Cos=fabs(sloap/sqrt(sloap*sloap+1.0));
   m_Sin=-1.0/sqrt(sloap*sloap+1.0);
   }

  
   return 0;
}

void CEllipse::PdcSetPixel(CDC *pDC, int xCenter, int yCenter, COLORREF m_PenColor)
{
      //xCenter+x,yCenter+y
	  
	double vh[3][3]={m_Cos,-m_Sin,m_CenterPoint.x*(1-m_Cos)+m_CenterPoint.y*m_Sin,
		               m_Sin, m_Cos,m_CenterPoint.y*(1-m_Cos)-m_CenterPoint.x*m_Sin,
					   0,0,1
	                  };       
  
      double xy[3]={xCenter,yCenter,1};
	  double x[3]={0,0,0};
	 
	  x[0]=vh[0][0]*xy[0]+vh[0][1]*xy[1]+vh[0][2]*xy[2];
	  x[1]=vh[1][0]*xy[0]+vh[1][1]*xy[1]+vh[1][2]*xy[2];
	   if(x[0]-int(x[0])>=0.5)
		  x[0]+=1;
	  if(x[1]-int(x[1])>=0.5)
		  x[1]+=1;
	  
	    for(int i=0;i<this->m_PenWidth;i++)
	  pDC->SetPixel(x[0]+i,x[1]-i,m_PenColor);
	  
	 
	   
	  //pDC->LineTo(x[0],x[1]);
	  // pDC->LineTo(x[0]+1,x[1]-1);
	  
	  
}

int CEllipse::ComputRadiusX(CPoint firstPoint,CPoint secondPoint)
{
 m_Xr=sqrt((firstPoint.x-secondPoint.x)*(firstPoint.x-secondPoint.x)+(firstPoint.y-secondPoint.y)*(firstPoint.y-secondPoint.y));
 return 0;
}

int CEllipse::ComputRadiusY(CPoint firstPoint,CPoint secondPoint)
{
  m_Yr=sqrt((firstPoint.x-secondPoint.x)*(firstPoint.x-secondPoint.x)+(firstPoint.y-secondPoint.y)*(firstPoint.y-secondPoint.y));
  return 0;
}



void CEllipse::ShowMovingLine(CDC*pDC,CPoint firstPoint, CPoint secondPoint)
{
CPen m_pen;
m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
CPen *OldPen=pDC->SelectObject(&m_pen);
pDC->MoveTo(firstPoint);
pDC->LineTo(secondPoint);
pDC->SelectObject(OldPen);

}

void CEllipse::ComputeOffSet()
{
double vh[3][3]={m_Cos,-m_Sin,m_CenterPoint.x*(1-m_Cos)+m_CenterPoint.y*m_Sin,
		               m_Sin, m_Cos,m_CenterPoint.y*(1-m_Cos)-m_CenterPoint.x*m_Sin,
					   0,0,1
	            };       

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -