Skip to content

Commit 354e913

Browse files
committed
Merge branch 'master' of https://github.com/CosmosOS/Cosmos into netcore
2 parents ecc8ae3 + c7286fe commit 354e913

File tree

4 files changed

+160
-7
lines changed

4 files changed

+160
-7
lines changed

Demos/Cosmos Graphic Subsystem/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/Console.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public void NewLine() {
6363
mX = 0;
6464
if (mY == mText.Rows) {
6565
mText.ScrollUp();
66-
mY = mText.Rows - 1;
67-
mX = 0;
66+
mY--;
6867
}
6968
UpdateCursor();
7069
}
@@ -93,8 +92,14 @@ public void Write(string aText) {
9392
if (aText[i] == '\n') {
9493
NewLine();
9594
} else if (aText[i] == '\r') {
95+
mX = 0;
96+
UpdateCursor();
9697
} else if (aText[i] == '\t') {
97-
Write(" ");
98+
//Write(" ");
99+
WriteChar(' ');
100+
WriteChar(' ');
101+
WriteChar(' ');
102+
WriteChar(' ');
98103
} else {
99104
WriteChar(aText[i]);
100105
}

source/Cosmos.System/Graphics/Canvas.cs

Lines changed: 134 additions & 4 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);
@@ -96,7 +100,7 @@ private void DrawHorizontalLine(Pen pen, int dx, int x1, int y1)
96100
DrawPoint(pen, x1 + i, y1);
97101
}
98102

99-
private void DrawVerthicalLine(Pen pen, int dy, int x1, int y1)
103+
private void DrawVerticalLine(Pen pen, int dy, int x1, int y1)
100104
{
101105
int i;
102106

@@ -155,7 +159,7 @@ private void DrawDiagonalLine(Pen pen, int dx, int dy, int x1, int y1)
155159
* DrawLine throw if the line goes out of the boundary of the Canvas, probably will be better to draw only the part
156160
* of line visibile. This is too "smart" to do here better do it in a future Window Manager.
157161
*/
158-
public void DrawLine(Pen pen, int x1, int y1, int x2, int y2)
162+
public virtual void DrawLine(Pen pen, int x1, int y1, int x2, int y2)
159163
{
160164
if (pen == null)
161165
throw new ArgumentOutOfRangeException(nameof(pen));
@@ -177,20 +181,109 @@ public void DrawLine(Pen pen, int x1, int y1, int x2, int y2)
177181

178182
if (dx == 0) /* the line is vertical */
179183
{
180-
DrawVerthicalLine(pen, dy, x1, y1);
184+
DrawVerticalLine(pen, dy, x1, y1);
181185
return;
182186
}
183187

184188
/* the line is neither horizontal neither vertical, is diagonal then! */
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

193-
public void DrawRectangle(Pen pen, int x, int y, int width, int height)
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+
286+
public virtual void DrawRectangle(Pen pen, int x, int y, int width, int height)
194287
{
195288
/*
196289
* we must draw four lines connecting any vertex of our rectangle to do this we first obtain the position of these
@@ -230,6 +323,22 @@ public void DrawRectangle(Pen pen, int x, int y, int width, int height)
230323
DrawLine(pen, xc, yc, xd, yd);
231324
}
232325

326+
public virtual void DrawFilledRectangle(Pen pen, int x_start, int y_start, int width, int height)
327+
{
328+
for (int i = 0; i != width; i++)
329+
{
330+
DrawLine(pen, x_start, y_start, x_start + height, y_start);
331+
y_start++;
332+
}
333+
}
334+
335+
public virtual void DrawTriangle(Pen pen, int v1x, int v1y, int v2x, int v2y, int v3x, int v3y)
336+
{
337+
DrawLine(pen, v1x, v1y, v2x, v2y);
338+
DrawLine(pen, v1x, v1y, v3x, v3y);
339+
DrawLine(pen, v2x, v2y, v3x, v3y);
340+
}
341+
233342
public void DrawRectangle(Pen pen, float x_start, float y_start, float width, float height)
234343
{
235344
throw new NotImplementedException();
@@ -304,4 +413,25 @@ protected void ThrowIfCoordNotValid(int x, int y)
304413
}
305414
}
306415
}
416+
417+
public class Point
418+
{
419+
public Point(int x, int y)
420+
{
421+
this.X = x;
422+
this.Y = y;
423+
}
424+
int x;
425+
public int X
426+
{
427+
get { return x; }
428+
set { x = value; }
429+
}
430+
int y;
431+
public int Y
432+
{
433+
get { return y; }
434+
set { y = value; }
435+
}
436+
}
307437
}

0 commit comments

Comments
 (0)