-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathTurtle.cs
More file actions
366 lines (323 loc) · 10.3 KB
/
Turtle.cs
File metadata and controls
366 lines (323 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
namespace Nakov.TurtleGraphics
{
public static class Turtle
{
private static float x;
public static float X
{
get
{
InitOnDemand();
return x;
}
set
{
InitOnDemand();
x = value;
}
}
private static float y;
public static float Y
{
get
{
InitOnDemand();
return y;
}
set
{
InitOnDemand();
y = value;
}
}
private static float angle;
public static float Angle
{
get
{
InitOnDemand();
return angle;
}
set
{
InitOnDemand();
angle = value % 360;
if (angle < 0)
{
angle += 360;
}
}
}
public static Color PenColor
{
get
{
InitOnDemand();
return drawPen.Color;
}
set
{
InitOnDemand();
drawPen.Color = value;
}
}
public static float PenSize
{
get
{
InitOnDemand();
return drawPen.Width;
}
set
{
InitOnDemand();
drawPen.Width = value;
}
}
private static bool penVisible;
public static bool PenVisible
{
get
{
InitOnDemand();
return penVisible;
}
set
{
InitOnDemand();
penVisible = value;
}
}
public static bool ShowTurtle
{
get
{
InitOnDemand();
return turtleHeadImage.Visible;
}
set
{
InitOnDemand();
turtleHeadImage.Visible = value;
DrawTurtle();
}
}
private static int delay;
public static int Delay
{
get
{
InitOnDemand();
return delay;
}
set
{
InitOnDemand();
delay = value;
}
}
public const int DrawAreaSize = 10000;
public static readonly Color DefaultColor = Color.Blue;
public const int DefaultPenSize = 7;
private static Control drawControl;
private static Image drawImage;
private static Graphics drawGraphics;
private static Pen drawPen;
private static PictureBox turtleHeadImage;
public static void Init(Control targetControl = null)
{
// Dispose all resources if already allocated
Dispose();
// Initialize the drawing control (sufrace)
drawControl = targetControl;
if (drawControl == null)
{
// If no target control is provided, use the currently active form
drawControl = Form.ActiveForm;
}
SetDoubleBuffered(drawControl);
// Create an empty graphics area to be used by the turtle
drawImage = new Bitmap(DrawAreaSize, DrawAreaSize);
drawControl.Paint += DrawControl_Paint;
drawControl.ClientSizeChanged += DrawControl_ClientSizeChanged;
drawGraphics = Graphics.FromImage(drawImage);
drawGraphics.SmoothingMode = SmoothingMode.AntiAlias;
// Initialize the pen size and color
drawPen = new Pen(DefaultColor, DefaultPenSize);
drawPen.StartCap = LineCap.Round;
drawPen.EndCap = LineCap.Round;
// Initialize the turtle position and other settings
X = 0;
Y = 0;
Angle = 0;
PenVisible = true;
// Delay = 0; // Intentionally preserve the "Delay" settings
// Initialize the turtle head image
turtleHeadImage = new PictureBox();
turtleHeadImage.BackColor = Color.Transparent;
drawControl.Controls.Add(turtleHeadImage);
}
public static void Dispose()
{
if (drawControl != null)
{
// Remove the associated events for the drawing control
drawControl.Paint -= DrawControl_Paint;
drawControl.ClientSizeChanged -= DrawControl_ClientSizeChanged;
// Release the pen object
drawPen.Dispose();
drawPen = null;
// Release the graphic object
drawGraphics.Dispose();
drawGraphics = null;
// Release the draw surface (image) object
drawImage.Dispose();
drawImage = null;
// Release the turtle (head) image
drawControl.Controls.Remove(turtleHeadImage);
turtleHeadImage.Dispose();
turtleHeadImage = null;
// Release the drawing control
drawControl.Invalidate();
drawControl = null;
}
}
public static void Reset()
{
Dispose();
}
public static void Forward(float distance = 10)
{
var angleRadians = Angle * Math.PI / 180;
var newX = X + (float)(distance * Math.Sin(angleRadians));
var newY = Y + (float)(distance * Math.Cos(angleRadians));
MoveTo(newX, newY);
}
public static void Backward(float distance = 10)
{
Forward(-distance);
}
public static void MoveTo(float newX, float newY)
{
InitOnDemand();
var fromX = DrawAreaSize / 2 + X;
var fromY = DrawAreaSize / 2 - Y;
X = newX;
Y = newY;
if (PenVisible)
{
var toX = DrawAreaSize / 2 + X;
var toY = DrawAreaSize / 2 - Y;
drawGraphics.DrawLine(drawPen, fromX, fromY, toX, toY);
}
DrawTurtle();
PaintAndDelay();
}
public static void Rotate(float angleDelta)
{
InitOnDemand();
Angle += angleDelta;
DrawTurtle();
PaintAndDelay();
}
public static void RotateTo(float newAngle)
{
InitOnDemand();
Angle = newAngle;
DrawTurtle();
PaintAndDelay();
}
public static void PenUp()
{
PenVisible = false;
}
public static void PenDown()
{
PenVisible = true;
}
private static void SetDoubleBuffered(Control control)
{
// set instance non-public property with name "DoubleBuffered" to true
typeof(Control).InvokeMember("DoubleBuffered",
BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, control, new object[] { true });
}
private static void InitOnDemand()
{
// Create the drawing surface if it does not already exist
if (drawControl == null)
{
Init();
}
}
private static void DrawTurtle()
{
if (ShowTurtle)
{
var turtleImg = Nakov.TurtleGraphics.Properties.Resources.Turtle;
turtleImg = RotateImage(turtleImg, angle);
var turtleImgSize = Math.Max(turtleImg.Width, turtleImg.Height);
turtleHeadImage.BackgroundImage = turtleImg;
turtleHeadImage.Width = turtleImg.Width;
turtleHeadImage.Height = turtleImg.Height;
var turtleX = 1 + drawControl.ClientSize.Width / 2 + X - turtleHeadImage.Width / 2;
var turtleY = 1 + drawControl.ClientSize.Height / 2 - Y - turtleHeadImage.Height / 2;
turtleHeadImage.Left = (int)Math.Round(turtleX);
turtleHeadImage.Top = (int)Math.Round(turtleY);
}
}
private static Bitmap RotateImage(Bitmap bmp, float angleDegrees)
{
Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(rotatedImage))
{
// Set the rotation point as the center into the matrix
g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
// Rotate
g.RotateTransform(angleDegrees);
// Restore the rotation point into the matrix
g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
// Draw the image on the new bitmap
g.DrawImage(bmp, new Point(0, 0));
}
bmp.Dispose();
return rotatedImage;
}
private static void PaintAndDelay()
{
drawControl.Invalidate();
if (Delay == 0)
{
// No delay -> invalidate the control, so it will be repainted later
}
else
{
// Immediately paint the control and them delay
drawControl.Update();
Thread.Sleep(Delay);
Application.DoEvents();
}
}
private static void DrawControl_ClientSizeChanged(object sender, EventArgs e)
{
drawControl.Invalidate();
DrawTurtle();
}
private static void DrawControl_Paint(object sender, PaintEventArgs e)
{
if (drawControl != null)
{
var top = (drawControl.ClientSize.Width - DrawAreaSize) / 2;
var left = (drawControl.ClientSize.Height - DrawAreaSize) / 2;
// TODO: needs a fix -> does not work correctly when drawControl has AutoScroll
e.Graphics.DrawImage(drawImage, top, left);
}
}
}
}