algorytm.org

Implementacja w C#



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Algorytm Cohena-Sutherlanda - Implementacja w C#
Ocena użytkownikóww: *****  / 2
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 12 grudnia 2009 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

Algorytm Cohena-Sutherlanda - C#/Form1.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

//Algorytm Cohena-Sutherlanda
//(c) 2009 by Tomasz Lubinski
//www.algorytm.org

namespace Algorytm_Cohena_Sutherlanda
{
	/// <summary>
	/// Algorytm Cohena-Sutherlanda.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private Graphics graphic;

		private static int LEFT = 80;
		private static int RIGHT = 420;
		private static int TOP = 80;
		private static int BOTTOM = 320;
		/// <summary>
		/// Algorytm Cohena-Sutherlanda.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		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()
		{
			this.linkLabel1 = new System.Windows.Forms.LinkLabel();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// linkLabel1
			// 
			this.linkLabel1.Location = new System.Drawing.Point(456, 448);
			this.linkLabel1.Name = "linkLabel1";
			this.linkLabel1.Size = new System.Drawing.Size(96, 16);
			this.linkLabel1.TabIndex = 0;
			this.linkLabel1.TabStop = true;
			this.linkLabel1.Text = "www.algorytm.org";
			this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Location = new System.Drawing.Point(24, 16);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(500, 385);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(24, 440);
			this.button1.Name = "button1";
			this.button1.TabIndex = 2;
			this.button1.Text = "Start";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(112, 416);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(344, 24);
			this.label1.TabIndex = 3;
			this.label1.Text = "Czerwony - do odrzucenia - przypadek prosty - caly poza oknem";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(112, 432);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(344, 24);
			this.label2.TabIndex = 4;
			this.label2.Text = "Zolty - do odrzucenia - przypadek zlozony";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(112, 448);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(344, 24);
			this.label3.TabIndex = 5;
			this.label3.Text = "Zielony - do wyswietlenia - przypadek prosty - caly w oknie";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(112, 464);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(344, 24);
			this.label4.TabIndex = 6;
			this.label4.Text = "Niebieski - do wyswietlenia - przypadek zlozony";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(560, 484);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.linkLabel1);
			this.Name = "Form1";
			this.Text = "Algorytm Cohena-Sutherlanda - www.algorytm.org (c) 2009 by Tomasz Lubinski";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		/// <summary>
		/// Calculates region code
		/// </summary>
		/// <param name="x">x</param>
		/// <param name="y">y</param>
		/// <returns></returns>
		private int calcRegCode(int x, int y)
		{
			int result = 0;

			if (x < LEFT)   result |= 0x1;
			if (x > RIGHT)  result |= 0x2;
			if (y > BOTTOM) result |= 0x4;
			if (y < TOP)    result |= 0x8;

			return result;
		}

		private void Cohen_Sutherland(int x1, int y1, int x2, int y2)
		{
			int rcode1, rcode2, rcode;

			/* Algorytm Cohena-Sutherlanda */
			/* 1. Zakoduj końce odcinka zgodnie z kodami obszarów */
			rcode1 = calcRegCode(x1, y1);
			rcode2 = calcRegCode(x2, y2);
			/* 2. Jeżeli iloczyn logiczny (AND) tych kodów <>0,
			   to odcinek może być pominięty (w całości poza
			   oknem) - zaznacz go na czerwono */
			if ((rcode1 & rcode2) != 0)
			{
				graphic.DrawLine(Pens.Red, x1, y1, x2, y2);
			}
			/* 3. Jeżeli suma logiczna (OR)tych kodów = 0,
			   to odcinek w całości mieści się w okienku
			   - zaznacz go na zielono */
			else if ((rcode1 | rcode2) == 0)
			{
				graphic.DrawLine(Pens.Green, x1, y1, x2, y2);
			}
			else
			{
				/* pozostale przypadki - przeciecie z krawedzia okna */
				do
				{
					int x = x1, y = y1;

					if (rcode1 != 0)
					{
						rcode = rcode1;
					}
					else
					{
						rcode = rcode2;
					}

					/* pozostale przypadki - przeciecie z krawedzia okna */
					if ((rcode & 0x1) != 0)
					{
						y = y1+(y2-y1)*(LEFT-x1)/(x2-x1);
						x = LEFT;
					}
					else if ((rcode & 0x2) != 0)
					{
						y = y1+(y2-y1)*(RIGHT-x1)/(x2-x1);
						x = RIGHT;
					}
					else if ((rcode & 0x4) != 0)
					{
						x = x1+(x2-x1)*(BOTTOM-y1)/(y2-y1);
						y = BOTTOM;
					}
					else if ((rcode & 0x8) != 0)
					{
						x = x1+(x2-x1)*(TOP-y1)/(y2-y1);
						y = TOP;
					}

					if (rcode == rcode1)
					{
						graphic.DrawLine(Pens.Yellow, x1, y1, x, y);
						x1 = x;
						y1 = y;
						rcode1 = calcRegCode(x1, y1);
					}
					else
					{
						graphic.DrawLine(Pens.Yellow, x2, y2, x, y);
						x2 = x;
						y2 = y;
						rcode2 = calcRegCode(x2, y2);
					}
				} while ((rcode1 & rcode2) == 0 && (rcode1 | rcode2) != 0);

				if ((rcode1 | rcode2) == 0)
				{
					graphic.DrawLine(Pens.Blue, x1, y1, x2, y2);
				}
				else
				{
					graphic.DrawLine(Pens.Yellow, x1, y1, x2, y2);
				}
			}
		}


		/// <summary>
		/// Perform algorithm for a random set of lines
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			Bitmap bitmap = new Bitmap(pictureBox1.Size.Width,pictureBox1.Size.Height );
			pictureBox1.Image=bitmap;
			graphic = Graphics.FromImage(bitmap);

			

			/* wyczysc wszystko */
			graphic.FillRectangle(Brushes.White, 0, 0, pictureBox1.Size.Width, pictureBox1.Size.Height);

			/* narysuj okno */
			graphic.FillRectangle(Brushes.LightGray, LEFT, TOP, RIGHT-LEFT, BOTTOM-TOP);

			/* losuj odciniki */
			int i, x1, y1, x2, y2;
			Random rand = new Random();
			for (i=0; i<20; i++)
			{
				x1 = rand.Next(pictureBox1.Size.Width);
				y1 = rand.Next(pictureBox1.Size.Height);
				x2 = x1 + rand.Next(200) - 100;
				y2 = y1 + rand.Next(200) - 100;

				Cohen_Sutherland(x1, y1, x2, y2);
			}
		}

		/// <summary>
		/// Open www.algorytm.org site
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start("IExplore", "http://www.algorytm.org" );
		}
	}
}
Dodaj komentarz