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

📄 form1.cs

📁 这是《C#图形程序设计》这本书的源代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 三维图形程序设计
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		public Point p1=new Point();
		public Point p2=new Point();
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuItem11;
		private System.Windows.Forms.MenuItem menuItem12;
		private System.Windows.Forms.MenuItem menuItem13;
		private System.Windows.Forms.MenuItem menuItem14;
		private System.Windows.Forms.MenuItem menuItem15;
		private System.Windows.Forms.MenuItem menuItem16;
		private System.Windows.Forms.MenuItem menuItem17;
		private System.Windows.Forms.MenuItem menuItem18;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			this.menuItem12 = new System.Windows.Forms.MenuItem();
			this.menuItem13 = new System.Windows.Forms.MenuItem();
			this.menuItem14 = new System.Windows.Forms.MenuItem();
			this.menuItem15 = new System.Windows.Forms.MenuItem();
			this.menuItem16 = new System.Windows.Forms.MenuItem();
			this.menuItem17 = new System.Windows.Forms.MenuItem();
			this.menuItem18 = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2,
																					  this.menuItem3});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem4,
																					  this.menuItem5,
																					  this.menuItem6,
																					  this.menuItem7});
			this.menuItem1.Text = "三维几何图形";
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 0;
			this.menuItem4.Text = "棱柱";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "棱锥体";
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 2;
			this.menuItem6.Text = "函数立体图";
			this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 3;
			this.menuItem7.Text = "带经线纬线立体球";
			this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem8,
																					  this.menuItem9,
																					  this.menuItem10,
																					  this.menuItem11,
																					  this.menuItem12,
																					  this.menuItem13,
																					  this.menuItem14});
			this.menuItem2.Text = "三维图形变换";
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 0;
			this.menuItem8.Text = "四边形饶y轴旋转";
			this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 1;
			this.menuItem9.Text = "四边形饶x轴旋转";
			this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 2;
			this.menuItem10.Text = "立方体的平移变换";
			this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 2;
			this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem15,
																					  this.menuItem16,
																					  this.menuItem17,
																					  this.menuItem18});
			this.menuItem3.Text = "隐藏线处理";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(560, 432);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 3;
			this.menuItem11.Text = "画立方体";
			this.menuItem11.Click += new System.EventHandler(this.menuItem11_Click);
			// 
			// menuItem12
			// 
			this.menuItem12.Index = 4;
			this.menuItem12.Text = "立方体比例变换";
			this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
			// 
			// menuItem13
			// 
			this.menuItem13.Index = 5;
			this.menuItem13.Text = "立方体旋转变换";
			this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
			// 
			// menuItem14
			// 
			this.menuItem14.Index = 6;
			this.menuItem14.Text = "立方体透视变换";
			this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);
			// 
			// menuItem15
			// 
			this.menuItem15.Index = 0;
			this.menuItem15.Text = "双环";
			this.menuItem15.Click += new System.EventHandler(this.menuItem15_Click);
			// 
			// menuItem16
			// 
			this.menuItem16.Index = 1;
			this.menuItem16.Text = "立体球";
			this.menuItem16.Click += new System.EventHandler(this.menuItem16_Click);
			// 
			// menuItem17
			// 
			this.menuItem17.Index = 2;
			this.menuItem17.Text = "正弦曲线";
			this.menuItem17.Click += new System.EventHandler(this.menuItem17_Click);
			// 
			// menuItem18
			// 
			this.menuItem18.Index = 3;
			this.menuItem18.Text = "立方体隐藏线";
			this.menuItem18.Click += new System.EventHandler(this.menuItem18_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(576, 449);
			this.Controls.Add(this.pictureBox1);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			Graphics g=pictureBox1.CreateGraphics();
			Pen pen1=new Pen(Color.Blue);
			g.Clear(this.BackColor);
			int flag,r,h,n,nn,gx,gy;
			double pi,x,y,z,cx,cy,thx,thy,th;
			double xw,yw,zw;
			double[] ax=new double[9];
			double[] ay=new double[9];
			double[] az=new double[9];
			double[] bx=new double[9];
			double[] by=new double[9];
			double[] bz=new double[9];
			pi=3.14159;
			cx=200;
			cy=140;
			h=80;
			thy=0.2;
			nn=1;
			r=100;
			n=6;
			thx=0.2;
			flag=0;
			for(th=0;th<=2*pi+0.1;th+=2*pi/n)
			{
				x=r*Math.Cos(th);
				y=h;
				z=r*Math.Sin(th);
				zw=z;
				xw=x;
				x=zw*Math.Cos(thy)-xw*Math.Sin(thy);
				z=zw*Math.Sin(thy)+xw*Math.Cos(thy);
				yw=y;
				zw=z;
				y=yw*Math.Cos(thx)-zw*Math.Sin(thx);
				z=yw*Math.Sin(thx)+zw*Math.Cos(thx);
				ax[nn]=x;
				ay[nn]=y;
				nn=nn+1;
			}
			flag=0;
			nn=1;
			for(th=0;th<=2*pi+0.1;th+=2*pi/n)
			{
				x=r*Math.Cos(th);
				y=-h;
				z=r*Math.Sin(th);
				zw=z;
				xw=x;
				x=zw*Math.Cos(thy)-xw*Math.Sin(thy);
				z=zw*Math.Sin(thy)+xw*Math.Cos(thy);
				yw=y;
				zw=z;
				y=yw*Math.Cos(thx)-zw*Math.Sin(thx);
				z=yw*Math.Sin(thx)+zw*Math.Cos(thx);
				bx[nn]=x;
				by[nn]=y;
				nn=nn+1;
			}
			for(nn=1;nn<=n;nn++)
			{
				flag=0;
				x=ax[nn];
				y=ay[nn];
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
				x=ax[nn+1];
				y=ay[nn+1];
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
				x=bx[nn+1];
				y=by[nn+1];
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
				x=bx[nn];
				y=by[nn];
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
			}
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			Graphics g=pictureBox1.CreateGraphics();
			Pen pen1=new Pen(Color.Blue);
			g.Clear(this.BackColor);
			int flag,r,h,n,nn,gx,gy;
			double pi,x,y,z,cx,cy,thx,thy,th;
			double xw,yw,zw;
			double[] ax=new double[9];
			double[] ay=new double[9];
			double[] az=new double[9];
			double[] bx=new double[9];
			double[] by=new double[9];
			double[] bz=new double[9];
			pi=3.14159;
			cx=200;
			cy=240;
			h=200;
			thy=0.2;
			nn=1;
			r=100;
			n=4;
			thx=0.2;
			flag=0;
			for(th=0;th<=2*pi+0.1;th+=2*pi/n)
			{
				x=r*Math.Cos(th);
				y=h;
				z=r*Math.Sin(th);
				zw=z;
				xw=x;
				x=zw*Math.Cos(thy)-xw*Math.Sin(thy);
				z=zw*Math.Sin(thy)+xw*Math.Cos(thy);
				yw=y;
				zw=z;
				y=yw*Math.Cos(thx)-zw*Math.Sin(thx);
				z=yw*Math.Sin(thx)+zw*Math.Cos(thx);
				ax[nn]=x;
				ay[nn]=y;
				nn=nn+1;
			}
			flag=0;
			nn=1;
			for(th=0;th<=2*pi+0.1;th+=2*pi/n)
			{
				x=r*Math.Cos(th);
				y=-h;
				z=r*Math.Sin(th);
				zw=z;
				xw=x;
				x=zw*Math.Cos(thy)-xw*Math.Sin(thy);
				z=zw*Math.Sin(thy)+xw*Math.Cos(thy);
				yw=y;
				zw=z;
				y=yw*Math.Cos(thx)-zw*Math.Sin(thx);
				z=yw*Math.Sin(thx)+zw*Math.Cos(thx);
				bx[nn]=x;
				by[nn]=y;
				bz[nn]=z;
				nn=nn+1;
			}
			for(nn=1;nn<=n;nn++)
			{
				flag=0;
				x=ax[nn]/200;
				y=ay[nn]/200;
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
				x=bx[nn+1];
				y=by[nn+1];
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
				x=bx[nn];
				y=by[nn];
				gx=(int)(cx+x);
				gy=(int)(cy+y);
				if(flag!=1)
				{
					p1.X=gx;
					p1.Y=gy;
					flag=1;
				}
				else
				{
					p2.X=gx;
					p2.Y=gy;
					g.DrawLine(pen1,p1,p2);
					p1=p2;
				}
			}
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			Graphics g=pictureBox1.CreateGraphics();
			Pen pen1=new Pen(Color.Blue);
			g.Clear(this.BackColor);
			double x,y,s,cx,cy,zz,xx,yy,x1,x2,y1,y2;
			int sx,sy;
			double xw,yw,sx1,sy1,sx2,sy2,h,A,B;
			const double pi=3.14159;
			s=pi/16.0;
			h=80.0;
			A=3.0;
			B=3.0;
			x1=-3.0;
			x2=3.0;
			y1=-11.0;
			y2=11.0;
			xw=x2-x1;
			yw=y2-y1;
			sx1=100f;
			sx2=400f;
			sy1=360.0;
			sy2=180.0;
			cx=(sx2-sx1)/xw;
			cy=(sy1-sy2)/yw;
			for(y=-3.0;y<=3.0;y+=.2)
			{
				for(x=-3.0;x<=3.0;x+=.04)
				{
					zz=h*Math.Exp(-x*x/A*A-y*y/B*B);
					xx=x*cx+sx1+160f;
					yy=sy1-y*cy;
					sx=(int)(xx+y*cy*Math.Cos(s));
					sy=(int)(yy-zz-100f);
					if(x==-3)
					{
						p1.X=sx-60;
						p1.Y=sy-150;
					}
					else
					{
						p2.X=sx-60;
						p2.Y=sy-150;
						g.DrawLine(pen1,p1,p2);
						p1=p2;
					}
				}
			}
		}

		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			Graphics g=pictureBox1.CreateGraphics();
			Pen pen1=new Pen(Color.Blue);
			g.Clear(this.BackColor);
			int[] mx=new int[100];
			int[] my=new int[100];
			int[] px=new int[100];
			int[] py=new int[100];
			int i=1,r=100;
			double pi=3.14159;
			double s=pi/9,q,p,x,y,z;
			q=-pi/2;
			for(p=0;p<=2*pi;p+=pi/24)
			{
				x=r*Math.Cos(q)*Math.Sin(p);
				y=r*Math.Sin(q);
				z=r*Math.Cos(q)*Math.Cos(p);
				px[i]=(int)(x*1.4+200);
				py[i]=(int)(140-y+z*Math.Sin(s));
				mx[i]=px[i];
				my[i]=py[i];
				i++;
			}
			for(q=-pi/2+pi/24;q<=pi/2;q+=pi/24)
			{
				i=1;
				for(p=0;p<=2*pi;p+=pi/24)
				{
					x=r*Math.Cos(q)*Math.Sin(p);
					y=r*Math.Sin(q);
					z=r*Math.Cos(q)*Math.Cos(p);
					px[i]=(int)(x*1.4+200);
					py[i]=(int)(140-y+z*Math.Sin(s));
					if(i==1)goto l1;
					g.DrawLine(pen1,px[i-1],py[i-1],px[i],py[i]);
				l1:
					g.DrawLine(pen1,mx[i],my[i],px[i],py[i]);
					mx[i]=px[i];
					my[i]=py[i];
					i++;
				}
			}
			g.DrawLine(pen1,px[1],py[1],px[i-1],py[i-1]);
		}

		private void menuItem8_Click(object sender, System.EventArgs e)
		{
			double pi=3.1415926;
			int i;
			double th;
			double x,y,z;
			double xo,yo,zo;
			xo=0.0;yo=0.0;zo=0.0;
			double[,] f=new double[4,4];
			double[] xx=new double[]{150.0,200.0,210.0,60.0,150.0};
			double[] yy=new double[]{-55.0,-20.0,50.0,40.0,-55.0};
			double[] zz=new double[]{0.0,0.0,0.0,0.0,0.0};
			Graphics g=pictureBox1.CreateGraphics();
			Pen pen1=new Pen(Color.Blue);
			g.Clear(this.BackColor);
			double co=Math.Cos(pi/20);
			double si=Math.Sin(pi/20);
			f[0,0]=co;
			f[0,1]=0.0;
			f[0,2]=-si;
			f[0,3]=0.0;
			f[1,0]=0.0;
			f[1,1]=1.0;
			f[1,2]=0.0;
			f[1,3]=0.0;
			f[2,0]=si;
			f[2,1]=0.0;
			f[2,2]=co;
			f[2,3]=0.0;
			f[3,0]=0.0;
			f[3,1]=0.0;
			f[3,2]=0.0;
			f[3,3]=1.0;
			for(i=0;i<=3;i++)
			{
				for(th=0;th<=2*pi;th+=pi/20)
				{
					x=xx[i];
					y=yy[i];
					z=zz[i];
					double _x,_y,_z;
					_x=x;
					_y=y;
					_z=z;
					x=_x*f[0,0]+_y*f[1,0]+_z*f[2,0]+f[3,0];
					y=_x*f[0,1]+_y*f[1,1]+_z*f[2,1]+f[3,1];
					z=_x*f[0,2]+_y*f[1,2]+_z*f[2,2]+f[3,2];
					if(th==0)
					{
						g.DrawLine(pen1,250+(int)x,100-(int)y,250+(int)x,100-(int)y);
					}
					else
					{
						g.DrawLine(pen1,250+(int)xo,100-(int)yo,250+(int)x,100-(int)y);
						xx[i]=x;xo=x;
						yy[i]=y;yo=y;
						zz[i]=z;zo=z;
					}
				}
			}
			double co1=Math.Cos(pi/36);
			double si1=Math.Sin(pi/36);
			f[0,0]=co1;
			f[0,1]=0.0;
			f[0,2]=-si1;
			f[0,3]=0.0;
			f[1,0]=0.0;
			f[1,1]=1.0;
			f[1,2]=0.0;
			f[1,3]=0.0;
			f[2,0]=si1;
			f[2,1]=0.0;
			f[2,2]=co1;
			f[2,3]=0.0;
			f[3,0]=0.0;
			f[3,1]=0.0;
			f[3,2]=0.0;
			f[3,3]=1.0;
			for(th=0;th<=2*pi;th+=pi/36)
			{
				for(i=0;i<=4;i++)
				{
					x=xx[i];
					y=yy[i];

⌨️ 快捷键说明

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