dc37742d2fb9165a197ab176327a22136b1fb4bd
[mono.git] / mcs / class / Mono.C5 / current / UserGuideExamples / GCHForm.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 using System;\r
23 using System.Drawing;\r
24 using System.Collections;\r
25 using System.ComponentModel;\r
26 using System.Windows.Forms;\r
27 using System.Diagnostics;\r
28 using C5;\r
29 \r
30 namespace GConvexHull\r
31 {\r
32         /// <summary>\r
33         /// Summary description for Form1.\r
34         /// </summary>\r
35   public class TesterForm : System.Windows.Forms.Form\r
36   {\r
37     //My data\r
38 \r
39     //My GUI stuff\r
40     private System.Windows.Forms.Panel drawarea;\r
41 \r
42     private Graphics drawg;\r
43 \r
44     //Std stuff\r
45     private System.Windows.Forms.Button runButton;\r
46     private TextBox pointCount;\r
47 \r
48     /// <summary>\r
49     /// Required designer variable.\r
50     /// </summary>\r
51     private System.ComponentModel.Container components = null;\r
52 \r
53 \r
54     public TesterForm()\r
55     {\r
56       //\r
57       // Required for Windows Form Designer support\r
58       //\r
59       InitializeComponent();\r
60 \r
61       //\r
62       // TODO: Add any constructor code after InitializeComponent call\r
63       //\r
64       drawg = drawarea.CreateGraphics();\r
65       reset();\r
66     }\r
67 \r
68 \r
69     /// <summary>\r
70     /// Clean up any resources being used.\r
71     /// </summary>\r
72     protected override void Dispose(bool disposing)\r
73     {\r
74       if (disposing)\r
75       {\r
76         if (components != null)\r
77         {\r
78           components.Dispose();\r
79         }\r
80       }\r
81 \r
82       base.Dispose(disposing);\r
83     }\r
84 \r
85     #region Windows Form Designer generated code\r
86     /// <summary>\r
87     /// Required method for Designer support - do not modify\r
88     /// the contents of this method with the code editor.\r
89     /// </summary>\r
90     private void InitializeComponent()\r
91     {\r
92       this.drawarea = new System.Windows.Forms.Panel();\r
93       this.runButton = new System.Windows.Forms.Button();\r
94       this.pointCount = new System.Windows.Forms.TextBox();\r
95       this.SuspendLayout();\r
96       // \r
97       // drawarea\r
98       // \r
99       this.drawarea.BackColor = System.Drawing.Color.White;\r
100       this.drawarea.Location = new System.Drawing.Point(8, 9);\r
101       this.drawarea.Name = "drawarea";\r
102       this.drawarea.Size = new System.Drawing.Size(500, 500);\r
103       this.drawarea.TabIndex = 0;\r
104       this.drawarea.Paint += new System.Windows.Forms.PaintEventHandler(this.drawarea_Paint);\r
105       this.drawarea.Invalidated += new System.Windows.Forms.InvalidateEventHandler(this.drawarea_Invalidated);\r
106       this.drawarea.MouseMove += new System.Windows.Forms.MouseEventHandler(this.drawarea_MouseMove);\r
107       this.drawarea.MouseClick += new System.Windows.Forms.MouseEventHandler(this.drawarea_MouseClick);\r
108       // \r
109       // runButton\r
110       // \r
111       this.runButton.Location = new System.Drawing.Point(8, 516);\r
112       this.runButton.Name = "runButton";\r
113       this.runButton.Size = new System.Drawing.Size(42, 20);\r
114       this.runButton.TabIndex = 1;\r
115       this.runButton.Text = "Run";\r
116       this.runButton.Click += new System.EventHandler(this.runButton_Click);\r
117       // \r
118       // pointCount\r
119       // \r
120       this.pointCount.Location = new System.Drawing.Point(97, 517);\r
121       this.pointCount.Name = "pointCount";\r
122       this.pointCount.Size = new System.Drawing.Size(55, 20);\r
123       this.pointCount.TabIndex = 5;\r
124       // \r
125       // TesterForm\r
126       // \r
127       this.ClientSize = new System.Drawing.Size(524, 550);\r
128       this.Controls.Add(this.pointCount);\r
129       this.Controls.Add(this.runButton);\r
130       this.Controls.Add(this.drawarea);\r
131       this.Name = "TesterForm";\r
132       this.Text = "C5 Tester";\r
133       this.Load += new System.EventHandler(this.TesterForm_Load);\r
134       this.ResumeLayout(false);\r
135       this.PerformLayout();\r
136 \r
137     }\r
138     #endregion\r
139 \r
140     /// <summary>\r
141     /// The main entry point for the application.\r
142     /// </summary>\r
143     [STAThread]\r
144     static void Main()\r
145     {\r
146       Application.EnableVisualStyles();\r
147       Application.Run(new TesterForm());\r
148     }\r
149 \r
150     Point[] pts;\r
151     Point[] chpts;\r
152 \r
153     private void runButton_Click(object sender, System.EventArgs e)\r
154     {\r
155       int N = int.Parse(pointCount.Text);\r
156       pts = new Point[N];\r
157       for (int i = 0; i < N; i++)\r
158         pts[i] = Point.Random(500, 500);\r
159       chpts = Convexhull.ConvexHull(pts);\r
160 \r
161       drawarea.Invalidate();\r
162     }\r
163 \r
164 \r
165     private void drawarea_Paint(object sender, System.Windows.Forms.PaintEventArgs e)\r
166     {\r
167       mydraw();\r
168     }\r
169 \r
170 \r
171     private void resetButton_Click(object sender, System.EventArgs e)\r
172     {\r
173       reset();\r
174     }\r
175 \r
176 \r
177     private void reset()\r
178     {\r
179       drawarea.Invalidate();//(new Rectangle(0, 0, 40, 40));\r
180     }\r
181 \r
182 \r
183 \r
184     public void mydraw()\r
185     {\r
186       if (pts == null)\r
187       {\r
188         return;\r
189       }\r
190       for (int i = 0; i < pts.Length; i++)\r
191       {\r
192         Point p = pts[i];\r
193         drawg.DrawEllipse(new Pen(Color.Red), transx(p.x) - 2, transy(p.y) - 2, 4, 4);\r
194       }\r
195       for (int i = 0; i < chpts.Length; i++)\r
196       {\r
197         int j = i + 1 < chpts.Length ? i + 1 : 0;\r
198         drawg.DrawEllipse(new Pen(Color.Blue), transx(chpts[i].x) - 2, transy(chpts[i].y) - 2, 4, 4);\r
199         drawg.DrawLine(new Pen(Color.LawnGreen), transx(chpts[i].x), transx(chpts[i].y), transx(chpts[j].x), transx(chpts[j].y));\r
200       }\r
201     }\r
202 \r
203 \r
204 \r
205     private int transx(double x)\r
206     {\r
207       return (int)x;\r
208     }\r
209 \r
210 \r
211     private int transy(double y)\r
212     {\r
213       return (int)y;\r
214     }\r
215 \r
216 \r
217     private void dumpButton_Click(object sender, System.EventArgs e)\r
218     {\r
219       Debug.WriteLine("###############");\r
220       Debug.WriteLine("###############");\r
221     }\r
222 \r
223 \r
224     private void graphTypeControlArray_Click(object sender, System.EventArgs e)\r
225     {\r
226       Debug.WriteLine(e.GetType());\r
227       Debug.WriteLine(sender.GetType());\r
228       drawarea.Invalidate();\r
229     }\r
230 \r
231 \r
232     private void drawarea_MouseMove(object sender, MouseEventArgs e)\r
233     {\r
234     }\r
235 \r
236 \r
237     private void drawarea_MouseClick(object sender, MouseEventArgs e)\r
238     {\r
239       //double x = untransx(e.X), y = untransy(e.Y);\r
240 \r
241     }\r
242 \r
243 \r
244     private void drawarea_Invalidated(object sender, InvalidateEventArgs e)\r
245     {\r
246       //msg.Text = e.InvalidRect + "";\r
247       //mydraw();\r
248     }\r
249 \r
250 \r
251     private void preparedFigureSelection_SelectedIndexChanged(object sender, System.EventArgs e)\r
252     {\r
253     }\r
254 \r
255     private void voronoiButton_CheckedChanged(object sender, EventArgs e)\r
256     {\r
257       graphTypeControlArray_Click(sender, e);\r
258     }\r
259 \r
260     private void delaunayButton_CheckedChanged(object sender, EventArgs e)\r
261     {\r
262       graphTypeControlArray_Click(sender, e);\r
263     }\r
264 \r
265     private void TesterForm_Load(object sender, EventArgs e)\r
266     {\r
267 \r
268     }\r
269 \r
270   }\r
271 }\r