5
5
using System . Text ;
6
6
using System . Threading . Tasks ;
7
7
using System . Drawing ;
8
+ using System . Collections ;
9
+ using System . Collections . ObjectModel ;
8
10
9
11
namespace Cosmos . System . Graphics
10
12
{
@@ -84,6 +86,8 @@ public virtual void Clear(Color color)
84
86
}
85
87
}
86
88
89
+
90
+
87
91
public abstract void DrawPoint ( Pen pen , int x , int y ) ;
88
92
89
93
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)
96
100
DrawPoint ( pen , x1 + i , y1 ) ;
97
101
}
98
102
99
- private void DrawVerthicalLine ( Pen pen , int dy , int x1 , int y1 )
103
+ private void DrawVerticalLine ( Pen pen , int dy , int x1 , int y1 )
100
104
{
101
105
int i ;
102
106
@@ -155,7 +159,7 @@ private void DrawDiagonalLine(Pen pen, int dx, int dy, int x1, int y1)
155
159
* DrawLine throw if the line goes out of the boundary of the Canvas, probably will be better to draw only the part
156
160
* of line visibile. This is too "smart" to do here better do it in a future Window Manager.
157
161
*/
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 )
159
163
{
160
164
if ( pen == null )
161
165
throw new ArgumentOutOfRangeException ( nameof ( pen ) ) ;
@@ -177,20 +181,109 @@ public void DrawLine(Pen pen, int x1, int y1, int x2, int y2)
177
181
178
182
if ( dx == 0 ) /* the line is vertical */
179
183
{
180
- DrawVerthicalLine ( pen , dy , x1 , y1 ) ;
184
+ DrawVerticalLine ( pen , dy , x1 , y1 ) ;
181
185
return ;
182
186
}
183
187
184
188
/* the line is neither horizontal neither vertical, is diagonal then! */
185
189
DrawDiagonalLine ( pen , dx , dy , x1 , y1 ) ;
186
190
}
187
191
192
+ public void DrawLine ( Pen pen , Point p1 , Point p2 )
193
+ {
194
+ DrawLine ( pen , p1 . X , p1 . Y , p2 . X , p2 . Y ) ;
195
+ }
196
+
188
197
public void DrawLine ( Pen pen , float x1 , float y1 , float x2 , float y2 )
189
198
{
190
199
throw new NotImplementedException ( ) ;
191
200
}
192
201
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 )
194
287
{
195
288
/*
196
289
* 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)
230
323
DrawLine ( pen , xc , yc , xd , yd ) ;
231
324
}
232
325
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
+
233
342
public void DrawRectangle ( Pen pen , float x_start , float y_start , float width , float height )
234
343
{
235
344
throw new NotImplementedException ( ) ;
@@ -304,4 +413,25 @@ protected void ThrowIfCoordNotValid(int x, int y)
304
413
}
305
414
}
306
415
}
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
+ }
307
437
}
0 commit comments