📄 aprilfourteenview.cpp
字号:
// AprilFourteenView.cpp : implementation of the CAprilFourteenView class
//
#include "stdafx.h"
#include "AprilFourteen.h"
#include "Math.h"
#include "AprilFourteenDoc.h"
#include "AprilFourteenView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAprilFourteenView
IMPLEMENT_DYNCREATE(CAprilFourteenView, CView)
BEGIN_MESSAGE_MAP(CAprilFourteenView, CView)
//{{AFX_MSG_MAP(CAprilFourteenView)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAprilFourteenView construction/destruction
CAprilFourteenView::CAprilFourteenView()
{
// TODO: add construction code here
}
CAprilFourteenView::~CAprilFourteenView()
{
}
BOOL CAprilFourteenView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CAprilFourteenView drawing
void CAprilFourteenView::OnDraw(CDC* pDC)
{
CAprilFourteenDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Bresenham直线
/*int x1,x2,y1,y2,i;
float dx,dy,dk,x,y,n,k;
x1=200;y1=100;x2=100;y2=300;
dx=x2-x1;
dy=y2-y1;
k=dy/dx;
y=(y1<y2)?y1:y2;
i=(y1>y2)?y1:y2;
x=(x1<x2)?x1:x2;
n=(x1>x2)?x1:x2;
if(0<k&&k<1)
{
dk=2*(i-y)-(n-x);
for(x;x<=n;x++)
{
pDC->SetPixel(x,y,RGB(0,0,0));
dk=dk+2*(i-y);
if(dk>=0)
{
y=y+1;
dk=dk-2*(n-x);
}
}
}
else if (k>1)
{
dk=2*(n-x)-(i-y);
for(y;y<=i;y++)
{
pDC->SetPixel(x,y,RGB(255,0,0));
dk=dk+2*(n-x);
if(dk>=0)
{
x=x+1;
dk=dk-2*(i-y);
}
}
}
else if (-1<k&&k<0)
{
dk=2*(i-y)-(n-x);
for(x;x<=n;x++)
{
pDC->SetPixel(x,i,RGB(0,0,255));
dk=dk+2*(i-y);
if(dk>=0)
{
i=i-1;
dk=dk-2*(n-x);
}
}
}
else if (k<-1)
{
dk=2*(n-x)-(i-y);
for(y;y<=i;y++)
{
pDC->SetPixel(n,y,RGB(0,255,0));
dk=dk+2*(n-x);
if(dk>=0)
{
n=n-1;
dk=dk-2*(i-y);
}
}
}
}*/
//画圆
/*int x,y,r,d,xc,yc;
int a=255,b=255,c=0;
for( r=100;r>=10;r--)
{
xc=300;yc=300;x=0;y=r;d=3-2*r;
a-=10;
b-=5;
c+=4;
pDC->SetPixel(xc+x,yc+y,RGB(a,b,c));
pDC->SetPixel(xc-x,yc-y,RGB(a,b,c));
pDC->SetPixel(xc+y,yc+x,RGB(a,b,c));
pDC->SetPixel(xc-y,yc-x,RGB(a,b,c));
while(x<y)
{
if(d<0)
{
d+=4*x+6;
}
else
{
d+=4*(x-y)+10;
y--;
}
x++;
pDC->SetPixel(xc+x,yc+y,RGB(a,b,c));
pDC->SetPixel(xc-x,yc+y,RGB(a,b,c));
pDC->SetPixel(xc+x,yc-y,RGB(a,b,c));
pDC->SetPixel(xc-x,yc-y,RGB(a,b,c));
pDC->SetPixel(xc+y,yc+x,RGB(a,b,c));
pDC->SetPixel(xc-y,yc+x,RGB(a,b,c));
pDC->SetPixel(xc+y,yc-x,RGB(a,b,c));
pDC->SetPixel(xc-y,yc-x,RGB(a,b,c));
}
}*/
//中点分割裁减算法
//int XL=100,YB=300,XR=400,YT=100;
//POINT begin={200,200},end={350,250};
//POINT begin={200,80},end={350,50};
//POINT begin={200,350},end={350,500};
//POINT begin={10,200},end={80,300};
//POINT begin={450,200},end={600,300};
//POINT begin={200,200},end={400,400};
//POINT begin={200,200},end={400,40};
//POINT begin={20,100},end={400,350};
//POINT begin={20,200},end={500,200};
//POINT begin={200,20},end={200,400};
/*POINT p1,p2;
p1=ClipMid(begin,end,XL,XR,YB,YT);
p2=ClipMid(end,begin,XL,XR,YB,YT);
CPen P1(PS_SOLID,1,RGB(255,100,0));
CPen P2(PS_SOLID,1,RGB(0,0,0));
pDC->SelectObject(&P1);
pDC->Rectangle(XL,YT,XR,YB);
pDC->Rectangle(XL+400,YT,XR+400,YB);
pDC->SelectObject(&P2);
pDC->MoveTo(p1.x,p1.y);
pDC->LineTo(p2.x,p2.y);
pDC->MoveTo(begin.x+400,begin.y);
pDC->LineTo(end.x+400,end.y);*/
//N边形的几种图形
/*float t;
int x0=320,y0=240,r=150,n=20;
int i,j,x[50],y[50];
for(i=0;i<=n;i++)
{
t=(360*3.14159/180)/n;
x[i]=r*cos(i*t)+x0;
y[i]=r*sin(i*t)+y0;
}
//正N边体
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
pDC->MoveTo(x[i],y[i]);
pDC->LineTo(x[j],y[j]);
}
}
//正N边形
for(i=0;i<=n-1;i++)
{
pDC->MoveTo(x[i]+500,y[i]);
pDC->LineTo(x[i+1]+500,y[i+1]);
}
//N个多边形
for( n=3;n<=7;n++){
for(i=0;i<=n;i++)
{
t=(360*3.14159/180)/n;
x[i]=r*cos(i*t)+x0;
y[i]=r*sin(i*t)+y0;
}
for(i=0;i<=n-1;i++)
{
pDC->MoveTo(x[i],y[i]+300);
pDC->LineTo(x[i+1],y[i+1]+300);
}
}*/
//int p1[3][3],p2[3][3];
//比例变换
/* POINT pt1[3]={{100,260},{100,100},{200,100}};
double p[3][2]={{1.2,0},{0,1.2},{0,0}};
pDC->Polygon(pt1,3);
POINT pt2[3];
for(int i=0,j=0;i<3,j<3;i++,j++)
{
pt2[j].x=pt1[i].x*p[0][0]+pt1[i].y*p[1][0]+p[2][0];
pt2[j].y=pt1[i].x*p[0][1]+pt1[i].y*p[1][1]+p[2][1];
}*/
//对称变换
/*pDC->Polygon(pt2,3);
POINT pt3[3]={{300,260},{300,100},{400,100}};
pDC->Polygon(pt3,3);
POINT pt4[3];
float p1[3][2]={{0,1},{1,0},{0,0}};
for(int m=0,n=0;m<3,n<3;m++,n++)
{
pt4[n].x=pt3[m].x*p1[0][0]+pt3[m].y*p1[1][0]+p1[2][0];
pt4[n].y=pt3[m].x*p1[0][1]+pt3[m].y*p1[1][1]+p1[2][1];
}
pDC->Polygon(pt4,3);
//错切变换
POINT pt5[3]={{500,260},{500,100},{600,100}};
pDC->Polygon(pt5,3);
POINT pt6[3];
float p2[3][2]={{1,0},{2,1},{0,0}};
for(int k=0,l=0;k<3,l<3;k++,l++)
{
pt6[l].x=pt3[k].x*p2[0][0]+pt3[k].y*p2[1][0]+p2[2][0];
pt6[l].y=pt3[k].x*p2[0][1]+pt3[k].y*p2[1][1]+p2[2][1];
}
pDC->Polygon(pt6,3);*/
//二维组合变换
//CPen P1(PS_SOLID,1,RGB(0,0,0));
//pDC->SelectObject(&P1);
/*
int xr=400,yr=350;
CPen P2(PS_SOLID,1,RGB(255,0,0));
pDC->SelectObject(&P2);
POINT pt1[50];
POINT pt2[50];
for(int i=0,j=0;i<360;i+=30,j++)
{
int x=650,y=350;
pDC->MoveTo(xr,yr);
double n=i*3.1415/180;
pt1[j].x=x*cos(n)-y*sin(n)+xr-xr*cos(n)+yr*sin(n);
pt1[j].y=x*sin(n)+y*cos(n)-xr*sin(n)+yr-yr*cos(n);
pDC->LineTo(pt1[j].x,pt1[j].y);
for(int m=0;m<10000;m++)
{
for(int k=0;k<10000;k++)
{
}
}
}
for(int a=0;a<j;a+=2)
{
pDC->MoveTo(pt1[a].x,pt1[a].y);
pDC->LineTo(pt1[a+1].x,pt1[a+1].y);
for(int m=0;m<10000;m++)
{
for(int k=0;k<10000;k++)
{
}
}
}
int c1=255,c2=0,c3=0;
for(int r=50;r>=0;r-=10)
{
CBrush B1(RGB(c1,c2,c3));
pDC->SelectObject(&B1);
pDC->Ellipse(400-r,350-r,400+r,350+r);
c1-=50;
c2+=50;
c3+=30;
for(int m=0;m<10000;m++)
{
for(int k=0;k<10000;k++)
{
}
}
}
int c4=255,c5=100,c6=30;
int b=0;
for(i=0;i<360;i+=60,b++)
{
CBrush B2(RGB(c4,c5,c6));
pDC->SelectObject(&B2);
double n=i*3.1415/180;
pt2[b].x=600*cos(n)-400*sin(n)+xr-xr*cos(n)+yr*sin(n);
pt2[b].y=600*sin(n)+400*cos(n)-xr*sin(n)+yr-yr*cos(n);
pDC->FloodFill(pt2[b].x,pt2[b].y,RGB(255,0,0));
c4-=30;
c5+=20;
c6+=40;
for(int m=0;m<10000;m++)
{
for(int k=0;k<10000;k++)
{
}
}
}*/
//正棱柱三视图通用程序
/* CPen pen(PS_SOLID,2,RGB(0,255,0));
CPen*pOldpen=NULL;
pOldpen=pDC->SelectObject(&pen);
double pi,cx,cy,cz,th;
double ax[9],ay[9],az[9],x1[9],y1[9],x2[9],y2[9];
int i,r,h,n,m,lx,ly;
cx=150; cy=80; cz=50; r=100; h=80; n=6; m=1; lx=300; ly=200;
pi=3.14159/180;
for (i=0;i<=360;i+=360/n)
{
th=i*pi;
ax[m]=cx+r*cos(th); ay[m]=cy-r*sin(th); az[m]=cz;
m+=1;
}
for(i=1;i<=3;i++)
{
if (i==1) // V
{
for (m=1;m<=n+1;m++)
{
x1[m]=ax[m]; y1[m]=az[m];
x2[m]=ax[m]; y2[m]=az[m]+h;
}
}
if (i==2) // W
{
for(m=1;m<=n+1;m++)
{
x1[m]=ay[m]+lx;
x2[m]=x1[m];
}
}
if (i==3) // H
{
for(m=1;m<=n+1;m++)
{
x1[m]=ax[m]; y1[m]=ay[m]+ly;
x2[m]=ax[m]; y2[m]=y1[m];
}
}
pDC->MoveTo(x1[1],y1[1]);
for (m=2;m<=n+1;m++)
{
pDC->LineTo(x1[m],y1[m]);
}
pDC->MoveTo(x2[1],y2[1]);
for (m=2;m<=n+1;m++)
{
pDC->LineTo(x2[m],y2[m]);
}
for (m=1;m<=n;m++)
{
pDC->MoveTo(x1[m],y1[m]);
pDC->LineTo(x2[m],y2[m]);
}
// pDC->SelectObject(pOldpen);
}*/
//正棱锥三视图通用程序
CPen pen(PS_SOLID,2,RGB(0,255,0));
CPen*pOldpen=NULL;
pOldpen=pDC->SelectObject(&pen);
double pi,cx,cy,cz,th;
double ax[9],ay[9],az[9],x1[9],y1[9],x2,y2;
int i,r,h,n,m,lx,ly;
cx=100; cy=60; cz=50; r=100; h=100; n=7; m=1; lx=300; ly=200;
for (i=0;i<=n;i++)
{
th=(360*3.14159/180)/n;
ax[m]=cx+r*cos(i*th); ay[m]=cy-r*sin(i*th); az[m]=cz;
m+=1;
}
for(i=1;i<=3;i++)
{
if (i==1) // V
{
for (m=1;m<=n+1;m++)
{
x1[m]=ax[m]; y1[m]=az[m]+h;
x2=cx; y2=cz;
}
}
if (i==2) // W
{
for(m=1;m<=n+1;m++)
{
x1[m]=ay[m]+lx;
x2=cy+lx;
}
}
if (i==3) // H
{
for(m=1;m<=n+1;m++)
{
x1[m]=ax[m]; y1[m]=ay[m]+ly;
x2=cx; y2=cy+ly;
}
}
pDC->MoveTo(x1[1],y1[1]);
for (m=2;m<=n+1;m++)
{
pDC->LineTo(x1[m],y1[m]);
}
for (m=1;m<=n;m++)
{
pDC->MoveTo(x1[m],y1[m]);
pDC->LineTo(x2,y2);
}
// pDC->SelectObject(pOldpen);
}
}
BOOL CAprilFourteenView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CAprilFourteenView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CAprilFourteenView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CAprilFourteenView diagnostics
#ifdef _DEBUG
void CAprilFourteenView::AssertValid() const
{
CView::AssertValid();
}
void CAprilFourteenView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CAprilFourteenDoc* CAprilFourteenView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAprilFourteenDoc)));
return (CAprilFourteenDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAprilFourteenView message handlers
void CAprilFourteenView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
CDC * pDC=GetDC();
char str[20];
sprintf(str,"x=%4d y=%4d",point.x,point.y);
pDC->TextOut(0,0,str);
}
bool CAprilFourteenView::IsInArea(POINT point, int XL, int XR, int YB, int YT)
{
if(point.x>=XL&&point.x<=XR&&point.y<=YB&&point.y>=YT)
return true;
else
return false;
}
bool CAprilFourteenView::NotIntersect(POINT begin, POINT end, int XL, int XR, int YB, int YT)
{
int maxx,minx,maxy,miny;
maxx=(begin.x>end.x)?begin.x:end.x;
minx=(begin.x<end.x)?begin.x:end.x;
maxy=(begin.y>end.y)?begin.y:end.y;
miny=(begin.y<end.y)?begin.y:end.y;
if(maxx<XL||minx>XR||miny>YB||maxy<YT)
return true;
else
return false;
}
POINT CAprilFourteenView::ClipMid(POINT begin, POINT end, int XL, int XR, int YB, int YT)
{
POINT mid,temp;
if(IsInArea(begin,XL,XR,YB,YT))
temp=begin;
else
{
if(NotIntersect(begin,end,XL,XR,YB,YT))
return temp;
else
{
mid.x=(begin.x+end.x)/2;
mid.y=(begin.y+end.y)/2;
if(abs(mid.x-end.x)<=1&&abs(mid.y-end.y)<=1)
temp=mid;
else
{
if(NotIntersect(begin,mid,XL,XR,YB,YT))
temp=ClipMid(mid,end,XL,XR,YB,YT);
else
temp=ClipMid(begin,mid,XL,XR,YB,YT);
}
}
}
return temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -