|
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);
|
@@ -185,11 +189,100 @@ public void DrawLine(Pen pen, int x1, int y1, int x2, int y2)
|
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 |
|
| 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 | + |
193 | 286 | public void DrawRectangle(Pen pen, int x, int y, int width, int height)
|
194 | 287 | {
|
195 | 288 | /*
|
@@ -303,4 +396,25 @@ protected void ThrowIfCoordNotValid(int x, int y)
|
303 | 396 | }
|
304 | 397 | }
|
305 | 398 | }
|
| 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 | + } |
306 | 420 | }
|
0 commit comments