vc++编程绘图.doc
2-1源程序:#include”math.h”void CHhhh02View:OnDraw(CDC* pDC)CHhhh02Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r);pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom/2);pDC->SetWindowExt(200,100);pDC->SetViewportExt(100,-80);int pp142=0,0, 0,120, -240,0, 240,0, 0,120, -160,0, -160,40,-80,0, -80,80, 0,0, 80,80, 80,0, 160,40, 160,0;pDC->MoveTo(pp00,pp01);for(int i=1;i<=4;i+) pDC->LineTo(ppi0,ppi1); pDC->MoveTo(pp50,pp51);for(i=6;i<=13;i+) pDC->LineTo(ppi0,ppi1);2-1运行结果:2-2源程序: #include"math.h"void CMy13View:OnDraw(CDC* pDC)CMy13Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r); pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/4,r.bottom/2);pDC->SetWindowExt(200,100);pDC->SetViewportExt(200,-100);for(int i=1;i<=15;i+)CPen pi(0,2,RGB(255-10*i,10*i,20*i); pDC->SelectObject(pi);pDC->MoveTo(300,100);pDC->Arc(300-10*i,100,300+10*i,100-20*i,300,100,300,100);2-2运行结果:2-3源程序:#include"math.h"void CMy24View:OnDraw(CDC* pDC)CRect r;GetClientRect(r); pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom/2);pDC->SetWindowExt(100,100);pDC->SetViewportExt(100,-100); CPen p1(0,2,RGB(0,0,0);double p352,p252;double a=3.1415926;for(int i=0;i<=4;i+)p3i0=100*cos(90+72*i)*a/180);p3i1=100*sin(90+72*i)*a/180);p2i0=38.2*cos(126+72*i)*a/180);p2i1=38.2*sin(126+72*i)*a/180);pDC->MoveTo(0,0);pDC->SelectObject(p1);for( i=0;i<=4;i+) pDC->MoveTo(0,0); pDC->LineTo(p3i0,p3i1); pDC->LineTo(p2i0,p2i1); pDC->MoveTo(0,0); pDC->LineTo(p2i0,p2i1); pDC->LineTo(p3i+10,p3i+11); pDC->MoveTo(p240,p241); pDC->LineTo(p300,p301);2-3运行结果:2-4源程序:void CMy2_4View:OnDraw(CDC* pDC)CMy2_4Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r); pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom*3/4);pDC->SetWindowExt(200,100);pDC->SetViewportExt(400,-200);double p142=0,0,10,0,90,0,80,10,20,10,20,70,10,80,10,100,0,100,0,80,-10,70,-10,10,-70,10,-80,0; pDC->MoveTo(p130,p131);pDC->LineTo(p20,p21);pDC->MoveTo(p00,p01);pDC->LineTo(p80,p81);pDC->MoveTo(p10,p11);pDC->LineTo(p70,p71);pDC->MoveTo(p70,p71);pDC->LineTo(p80,p81);pDC->MoveTo(p110,p111);pDC->LineTo(p120,p121);pDC->MoveTo(p30,p31);pDC->LineTo(p40,p41);pDC->MoveTo(p100,p101);pDC->LineTo(p110,p111);pDC->MoveTo(p40,p41);pDC->LineTo(p50,p51);pDC->Arc(70,10,90,-10,90,0,80,10);pDC->Arc(0,80,20,60,20,70,10,80);pDC->Arc(-10,80,10,60,0,80,-10,70);pDC->Arc(-80,10,-60,-10,-70,10,-80,0);2-4运行结果:2-5源程序:#include "math.h"void CHhhh01View:OnDraw(CDC* pDC)CHhhh01Doc* pDoc = GetDocument();ASSERT_VALID(pDoc); CRect r ; GetClientRect(r);CPen p01(0,8,RGB(0,0,0),p02(1,0,RGB(255,0,0);pDC->SetMapMode(7);pDC->SetWindowExt(200,100);pDC->SetViewportExt(100,-50);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom/2);int pp102=0,125, -280,125, -280,-100, 280,-100, 280,125, 0,125, -60,0, -60,-100, 60,0,60,-100;pp00=-sqrt(150*150-125*125);pp50=-pp00;pp61=-sqrt(100*100-60*60);pp81=pp61; pDC->SelectObject(p01);pDC->MoveTo(pp00,pp01);for(int i=1;i<=5;i+) pDC->LineTo(ppi0,ppi1);pDC->MoveTo(pp60,pp61); pDC->LineTo(pp70,pp71);pDC->MoveTo(pp80,pp81); pDC->LineTo(pp90,pp91);pDC->Arc(-150,-150,150,150,pp50,pp51,pp00,pp01);pDC->Arc(-100,-100,100,100,pp80,pp81,pp60,pp61);pDC->SelectObject(p02);pDC->MoveTo(-110,0); pDC->LineTo(110,0); pDC->MoveTo(0,-110); pDC->LineTo(0,160);2-5运行结果:2-6源程序:void CMy1View:OnDraw(CDC* pDC)CMy1Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r); pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom/2);pDC->SetWindowExt(200,100);pDC->SetViewportExt(200,-100);CPen p1(0,1,RGB(255,0,0); pDC->SelectObject(p1);pDC->MoveTo(0,0);pDC->Arc(-70,70,70,-70,70,0,-70,0); pDC->SetArcDirection(AD_COUNTERCLOCKWISE); pDC->Arc(-140,50,140,-50,-52.45,46.36,52.45,46.36);2-6运行结果:2-7源程序:#include"math.h"void CMy2_7View:OnDraw(CDC* pDC)CMy2_7Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r); double a=3.1415926; pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom/2);pDC->SetWindowExt(200,100);pDC->SetViewportExt(600,-300);pDC->Ellipse(-30,30,30,-30); pDC->Ellipse(-30*cos(60*a)/180),30,30*cos(60*a)/180),-30); pDC->Arc(-30*cos(30*a)/180),30,30*cos(30*a)/180),-30,0,30,0,30); for(int i=-20;i<=20;i+=10) pDC->MoveTo(-sqrt(30*30-i*i),i); pDC->LineTo(sqrt(30*30-i*i),i);2-7运行结果:2-8源程序:#include"math.h"#define R(theta) (cos(1.5*theta)*s1void Cmy110View:OnDraw(CDC* pDC)CMy2Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data hereint x0,y0,x,y,s1=120;double r,theta;CRect rc;GetClientRect(&rc);x0=rc.right/2;y0=rc.bottom/2;pDC->SetMapMode(7);pDC->SetWindowExt(200,100);pDC->SetViewportExt(200,-100);pDC->SetViewportOrg(x0,y0);for(theta=0;theta<=20*3.14;theta+=0.01)r=R(theta);x=(int)(r*cos(theta); y=(int)(r*sin(theta);pDC->MoveTo(0,0);pDC->LineTo(x,y);2-8运行结果:2-10源程序:#include"math.h"void CMy2_95View:OnDraw(CDC* pDC)CMy2_95Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r); pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/2,r.bottom/2);pDC->SetWindowExt(200,100);pDC->SetViewportExt(400,-200);pDC->Ellipse(-70,70,70,-70);pDC->Arc(-75,75,75,-75,75,-75,75,-75);double d=45;for(int i=(-60);i<=60;i+=10)pDC->MoveTo (-sqrt(70*70-i*i)*cos(d)-i*sin(d),(-sqrt(70*70-i*i)*sin(d)+i*cos(d);pDC->LineTo (sqrt(70*70-i*i)*cos(d)-i*sin(d),(sqrt(70*70-i*i)*sin(d)+i*cos(d);2-9运行结果:2-10源程序:void Cmy110View:OnDraw(CDC* pDC)CMy6Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect r;GetClientRect(r); pDC->SetMapMode(7);pDC->SetWindowOrg(0,0);pDC->SetViewportOrg(r.right/3,r.bottom*2/3);pDC->SetWindowExt(200,100);pDC->SetViewportExt(200,-100); CPen p1(0,1,RGB(0,0,0);pDC->SelectObject(p1);for(int i=0;i<=9;i+)pDC->MoveTo(20*i,20*i*0.4);pDC->LineTo(20*(i+1),20*(i+1)*0.4); pDC->LineTo(20*(i+1),65+20*(i+1)*0.4); pDC->LineTo(20*i,65+20*i*0.4); pDC->LineTo(20*i,20*i*0.4);for( i=0;i<=5;i+)pDC->MoveTo(40*i,40*i*0.4);pDC->LineTo(40*i,-20+40*i*0.4);2-10运行结果:2-11源程序:#include "math.h"#define x(t) a*cos(t)*cos(phi*3.1415926/180)-b*sin(t)*sin(phi*3.1415926/180)+xo#define y(t) a*cos(t)*sin(phi*3.1415926/180)+b*sin(t)*cos(phi*3.1415926/180)+yovoid CMy10View:OnDraw(CDC* pDC)CMy10Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect rc; GetClientRect(rc); pDC->SetMapMode(7); pDC->SetViewportOrg(rc.right/2,rc.bottom/2); pDC->SetWindowExt(100,100); pDC->SetViewportExt(100,-100); for(double phi=0;phi<(180-180/8);phi+=180/8) elli(0,0,100,40,phi);void CMy10View:elli(int xo, int yo, double a, double b, double phi) CClientDC dc(this); CRect rc; GetClientRect(rc); dc.SetMapMode(7); dc.SetViewportOrg(rc.right/2,rc.bottom/2); dc.SetWindowExt(100,100); dc.SetViewportExt(100,-100); for(double t=0; t<=2*3.1415926; t+=0.0001) double xf=a*cos(t); double yf=b*sin(t); if(t=0) dc.MoveTo(int)(x(t),(int)(y(t); else dc.LineTo(int)x(t),(int)y(t); 2-11运行结果:2-12源程序:#define fnx(x) (int)(x0+(x)*s*s1)#define fny(y) (int)(y0-(y)*s1)void CMy212View:OnDraw(CDC* pDC)CMy212Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CRect rc;GetClientRect(&rc);CString k="欢迎光临指导"TEXTMETRIC tm;int m=rc.right/2-45,j=rc.bottom/2-10;pDC->SetTextColor(RGB(255,0,0);CPen p1(0,1,RGB(255,0,0); pDC->SelectObject(p1);pDC->TextOut(m,j,k);pDC->GetTextMetrics(&tm);j+=tm.tmHeight+10*tm.tmExternalLeading;int s=1; double s1=0.5;double x0=rc.right/2;double y0=rc.bottom/2+100;draw(x0,y0,s,s1);s=-s;draw(x0,y0,s,s1);void CMy212View:draw(double x0, double y0, int s, double s1)int i;CClientDC dc(this);dc.MoveTo(fnx(0),fny(50);dc.LineTo(fnx(300),fny(50);dc.Rectangle(fnx(165),fny(50),fnx(215),fny(250);dc.Rectangle(fnx(155),fny(250),fnx(225),fny(260);dc.Arc(fnx(190-25),fny(260-25),fnx(190+25),fny(260+25),x0+300,fny(260),x0-300,fny(260);dc.MoveTo(fnx(215),fny(55);dc.LineTo(fnx(300),fny(55);dc.MoveTo(fnx(215),fny(190);dc.LineTo(fnx(300),fny(190);dc.MoveTo(fnx(215),fny(195);dc.LineTo(fnx(300),fny(195);for(i=1;i<=4;i+)dc.MoveTo(fnx(215+20*i),fny(55);dc.LineTo(fnx(215+20*i),fny(190);2-12运行结果:土木工程2008级Vc+编程绘图指导教师 韩太昌 班级 土木八班 姓名 学号 20080235 西南交通大学土木工程学院2009年11月14