All pastes #680 Raw Edit

Anonymous

public text v1 · immutable
#680 ·published 2004-08-19 01:40 UTC
rendered paste body
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Road
{
	public class Road : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;
		enum Direction { FORWARD, BACK, LEFT, RIGHT, UP, DOWN };
		const int ZDEPTH =  512;
		const long MAX_POINTS = 2048;
		private long index = 0;
		private int halfWidth;
		private int halfHeight;
		private Bitmap bmp;
		private Pen pn;
		Graphics dc;

		struct Point3D 
		{
			public double x, y, z;
		}

		Point3D[] map;

		public Road()
		{
			InitializeComponent();
			map = new Point3D[MAX_POINTS];
			halfWidth = (this.Width)/2;
			halfHeight = (this.Height)/2;
			bmp = new Bitmap(this.Width, this.Height);
			pn = new Pen(Color.Green);
			int x,z;

			for (x=-1024; x < 1024; x+=50)
			{
				for (z=0; z < 1024; z+=50)
				{
					if (index < MAX_POINTS)
					{
						map[index].x = x;
						map[index].y = 80;
						map[index].z = -z;
						index++;
					}
				}
			}
			dc = this.CreateGraphics();
			Render();
		}

		private void Render()
		{
			//Bitmap bmp = new Bitmap(this.Width, this.Height);
			//Graphics dc = Graphics.FromImage(bmp);
			
			dc.Clear(Color.Black);
			this.Show();
			for (int i=0; i < index; i++)
			{
				if (map[i].z == 0) 
					map[i].z = 1;
				int gx = halfWidth + (int)(ZDEPTH*map[i].x/map[i].z);
				int gy = halfHeight - (int)(ZDEPTH*map[i].y/map[i].z);
				if (gx < this.Width-1 && gy < this.Height-1 && gx > 1 && gy > 1)
					dc.FillEllipse(pn.Brush, gx, gy, 2, 2);
					//bmp.SetPixel(gx, gy, Color.DarkGreen);
				//dc.DrawImage(bmp, 0, 0);
			}
		}


		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Road
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.Black;
			this.ClientSize = new System.Drawing.Size(634, 448);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Road";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Road";
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Road_KeyDown);
			this.DoubleClick += new System.EventHandler(this.Road_DoubleClick);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Road());
		}

		private void Road_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
				Application.Exit();
			if (e.KeyCode == Keys.Up)
				RotateMap(map, Direction.FORWARD);
			if (e.KeyCode == Keys.Down)
				RotateMap(map, Direction.BACK);
			if (e.KeyCode == Keys.Left)
				RotateMap(map, Direction.LEFT);
			if (e.KeyCode == Keys.Right)
				RotateMap(map, Direction.RIGHT);
			if (e.KeyCode == Keys.A)
				RotateMap(map, Direction.UP);
			if (e.KeyCode == Keys.Z)
				RotateMap(map, Direction.DOWN);
		}

		private void RotateMap(Point3D[] m, Direction d)
		{
			
			switch (d)
			{
				case Direction.FORWARD:
					for (int i=0; i<MAX_POINTS; i++)
						map[i].z = map[i].z + 10;
					break;

				case Direction.BACK:
					for (int i=0; i<MAX_POINTS; i++)
						map[i].z = map[i].z - 10;
					break;

				case Direction.RIGHT:
					for (int i=0; i<MAX_POINTS; i++)
					{
						map[i].z = (map[i].z * Math.Cos(-0.04F) - map[i].x * Math.Sin(-0.04F));
						map[i].x = (map[i].x * Math.Cos(-0.04F) + map[i].z * Math.Sin(-0.04F));
					}
					break;

				case Direction.LEFT:
					for (int i=0; i<MAX_POINTS; i++)
					{
						map[i].z = (map[i].z * Math.Cos(0.04F) - map[i].x * Math.Sin(0.04F));
						map[i].x = (map[i].x * Math.Cos(0.04F) + map[i].z * Math.Sin(0.04F));
					}
					break;

				case Direction.UP:
					for (int i = 0; i < MAX_POINTS; i++)
					{
						map[i].z =  (map[i].z * Math.Cos(-0.04F) - map[i].y * Math.Sin(-0.04F));
						map[i].y =  (map[i].y * Math.Cos(-0.04F) + map[i].z * Math.Sin(-0.04F));
					}
					break;

				case Direction.DOWN:
					for (int i = 0; i < MAX_POINTS; i++)
					{
						map[i].z =  (map[i].z * Math.Cos(0.04F) - map[i].y * Math.Sin(0.04F));
						map[i].y =  (map[i].y * Math.Cos(0.04F) + map[i].z * Math.Sin(0.04F));
					}
					break;

			}
			Render();
		}

		private void Road_DoubleClick(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
	}
}