Skip to content

Commit d4fc437

Browse files
authored
Merge pull request #646 from nuhash/DrawCircle
Implemented additional drawing function as per issue #603
2 parents f3a10fb + ab56018 commit d4fc437

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

Demos/Cosmos Graphic Subsytem/Kernel.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ protected override void Run()
4848
pen.Color = Color.PaleVioletRed;
4949
canvas.DrawRectangle(pen, 350, 350, 80, 60);
5050

51+
pen.Color = Color.Chartreuse;
52+
canvas.DrawCircle(pen, 69, 69, 10);
53+
54+
pen.Color = Color.LightSalmon;
55+
canvas.DrawEllipse(pen, 400, 300, 100, 150);
56+
57+
pen.Color = Color.MediumPurple;
58+
canvas.DrawPolygon(pen, new Point(200, 250), new Point(250, 300), new Point(220, 350), new Point(210, 275));
59+
5160
/*
5261
* It will be really beautiful to do here:
5362
* canvas.DrawString(pen, "Please press any key to continue the Demo...");

Tests/GraphicTest/Kernel.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ protected override void Run()
5454
pen.Color = Color.PaleVioletRed;
5555
canvas.DrawRectangle(pen, 350, 350, 80, 60);
5656

57+
pen.Color = Color.Chartreuse;
58+
canvas.DrawCircle(pen, 69, 69, 10);
59+
60+
pen.Color = Color.DimGray;
61+
canvas.DrawEllipse(pen, 100, 69, 10, 50);
62+
63+
pen.Color = Color.MediumPurple;
64+
canvas.DrawPolygon(pen, new Point(200, 250), new Point(250, 300), new Point(220, 350), new Point(210, 275));
65+
5766
Console.ReadKey();
5867

5968
/* Let's try to change mode...*/

source/Cosmos.System/Graphics/Canvas.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using System.Drawing;
8+
using System.Collections;
9+
using System.Collections.ObjectModel;
810

911
namespace Cosmos.System.Graphics
1012
{
@@ -84,6 +86,8 @@ public virtual void Clear(Color color)
8486
}
8587
}
8688

89+
90+
8791
public abstract void DrawPoint(Pen pen, int x, int y);
8892

8993
public abstract void DrawPoint(Pen pen, float x, float y);
@@ -185,11 +189,100 @@ public void DrawLine(Pen pen, int x1, int y1, int x2, int y2)
185189
DrawDiagonalLine(pen, dx, dy, x1, y1);
186190
}
187191

192+
public void DrawLine(Pen pen, Point p1, Point p2)
193+
{
194+
DrawLine(pen, p1.X, p1.Y, p2.X, p2.Y);
195+
}
196+
188197
public void DrawLine(Pen pen, float x1, float y1, float x2, float y2)
189198
{
190199
throw new NotImplementedException();
191200
}
192201

202+
//https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
203+
public virtual void DrawCircle(Pen pen, int x_center, int y_center, int radius)
204+
{
205+
if (pen == null)
206+
throw new ArgumentNullException(nameof(pen));
207+
ThrowIfCoordNotValid(x_center + radius, y_center);
208+
ThrowIfCoordNotValid(x_center - radius, y_center);
209+
ThrowIfCoordNotValid(x_center, y_center + radius);
210+
ThrowIfCoordNotValid(x_center, y_center - radius);
211+
int x = radius;
212+
int y = 0;
213+
int e = 0;
214+
215+
while(x>=y)
216+
{
217+
DrawPoint(pen, x_center + x, y_center + y);
218+
DrawPoint(pen, x_center + y, y_center + x);
219+
DrawPoint(pen, x_center - y, y_center + x);
220+
DrawPoint(pen, x_center - x, y_center + y);
221+
DrawPoint(pen, x_center - x, y_center - y);
222+
DrawPoint(pen, x_center - y, y_center - x);
223+
DrawPoint(pen, x_center + y, y_center - x);
224+
DrawPoint(pen, x_center + x, y_center - y);
225+
226+
y++;
227+
if(e<=0)
228+
{
229+
e += 2 * y + 1;
230+
}
231+
if(e>0)
232+
{
233+
x--;
234+
e -= 2 * x + 1;
235+
}
236+
}
237+
}
238+
239+
//http://members.chello.at/~easyfilter/bresenham.html
240+
public virtual void DrawEllipse(Pen pen, int x_center, int y_center, int x_radius, int y_radius)
241+
{
242+
if (pen == null)
243+
throw new ArgumentNullException(nameof(pen));
244+
ThrowIfCoordNotValid(x_center + x_radius, y_center);
245+
ThrowIfCoordNotValid(x_center - x_radius, y_center);
246+
ThrowIfCoordNotValid(x_center, y_center + y_radius);
247+
ThrowIfCoordNotValid(x_center, y_center - y_radius);
248+
int a = 2 * x_radius;
249+
int b = 2 * y_radius;
250+
int b1 = b & 1;
251+
int dx = 4 * (1 - a) * b * b;
252+
int dy = 4 * (b1 + 1) * a * a;
253+
int err = dx + dy + b1 * a * a;
254+
int e2;
255+
int y = 0;
256+
int x = x_radius;
257+
a *= 8 * a;
258+
b1 = 8 * b * b;
259+
260+
while (x>=0)
261+
{
262+
DrawPoint(pen, x_center + x, y_center + y);
263+
DrawPoint(pen, x_center - x, y_center + y);
264+
DrawPoint(pen, x_center - x, y_center - y);
265+
DrawPoint(pen, x_center + x, y_center - y);
266+
e2 = 2 * err;
267+
if (e2 <= dy) { y++; err += dy += a; }
268+
if (e2 >= dx || 2 * err > dy) { x--; err += dx += b1; }
269+
}
270+
}
271+
272+
public virtual void DrawPolygon(Pen pen, params Point[] points)
273+
{
274+
if (points.Length < 3)
275+
throw new ArgumentException("A polygon requires more than 3 points.");
276+
if (pen == null)
277+
throw new ArgumentNullException(nameof(pen));
278+
279+
for (int i = 0; i < points.Length - 1; i++)
280+
{
281+
DrawLine(pen, points[i], points[i + 1]);
282+
}
283+
DrawLine(pen, points[0], points[points.Length - 1]);
284+
}
285+
193286
public void DrawRectangle(Pen pen, int x, int y, int width, int height)
194287
{
195288
/*
@@ -303,4 +396,25 @@ protected void ThrowIfCoordNotValid(int x, int y)
303396
}
304397
}
305398
}
399+
400+
public class Point
401+
{
402+
public Point(int x, int y)
403+
{
404+
this.X = x;
405+
this.Y = y;
406+
}
407+
int x;
408+
public int X
409+
{
410+
get { return x; }
411+
set { x = value; }
412+
}
413+
int y;
414+
public int Y
415+
{
416+
get { return y; }
417+
set { y = value; }
418+
}
419+
}
306420
}

0 commit comments

Comments
 (0)