Browse Source

Added DrawCircle and DrawEllipse commands.

improved_timing
Ian Burgmyer 7 years ago
parent
commit
e5a3a7f872
  1. 83
      DotSDL/Graphics/Canvas.cs
  2. 15
      Samples/Sample.BasicPixels/Window.cs

83
DotSDL/Graphics/Canvas.cs

@ -30,6 +30,89 @@ namespace DotSDL.Graphics {
SetSize(width, height);
}
/// <summary>
/// Draws a circle onto the <see cref="Canvas"/>.
/// </summary>
/// <param name="color">The color of the circle.</param>
/// <param name="center">A <see cref="Point"/> indicating the center of the circle.</param>
/// <param name="radius">The radius of the drawn circle, in pixels.</param>
public void DrawCircle(Color color, Point center, int radius) {
var x = radius;
var y = 0;
var err = 0;
while(x >= y) {
PlotMirroredPoints(color, center, x, y);
PlotMirroredPoints(color, center, y, x);
y += 1;
if(err <= 0) {
err += 2 * y + 1;
}
if(err > 0) {
x -= 1;
err -= 2 * x + 1;
}
}
}
/// <summary>
/// Draws an ellipse onto the <see cref="Canvas"/>.
/// </summary>
/// <param name="color">The color of the ellipse.</param>
/// <param name="center">A <see cref="Point"/> indicating the center of the ellipse.</param>
/// <param name="width">The radius when measured horizontally, in pixels.</param>
/// <param name="height">The radius when measured vertically, in pixels.</param>
public void DrawEllipse(Color color, Point center, int width, int height) {
var rxSq = width * width;
var rySq = height * height;
var x = 0;
var y = height;
int p;
var px = 0;
var py = 2 * rxSq * y;
PlotMirroredPoints(color, center, x, y);
// Region 1
p = (int)(rySq - (rxSq * height) + (0.25 * rxSq));
while(px < py) {
x++;
px = px + 2 * rySq;
if(p < 0) {
p = p + rySq + px;
} else {
y--;
py = py - 2 * rxSq;
p = p + rySq + px - py;
}
PlotMirroredPoints(color, center, x, y);
}
// Region 2
p = (int)(rySq * (x + 0.5) * (x + 0.5) + rxSq * (y - 1) * (y - 1) - rxSq * rySq);
while(y > 0) {
y--;
py = py - 2 * rxSq;
if(p > 0) {
p = p + rxSq - py;
} else {
x++;
px = px + 2 * rySq;
p = p + rxSq - py + px;
}
PlotMirroredPoints(color, center, x, y);
}
}
private void PlotMirroredPoints(Color color, Point center, int rX, int rY) {
Pixels[GetIndex(center.X + rX, center.Y + rY)] = color;
Pixels[GetIndex(center.X + rX, center.Y - rY)] = color;
Pixels[GetIndex(center.X - rX, center.Y + rY)] = color;
Pixels[GetIndex(center.X - rX, center.Y - rY)] = color;
}
/// <summary>
/// Plots a line on the <see cref="Canvas"/>.
/// </summary>

15
Samples/Sample.BasicPixels/Window.cs

@ -55,17 +55,10 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 48, Y = offsetY + 80 }, End = new Point { X = offsetX + 32, Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 32, Y = offsetY + 96 }, End = new Point { X = offsetX, Y = offsetY + 96 } }
);
// o
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 64, Y = offsetY + 88}, End = new Point { X = offsetX + 64, Y = offsetY + 56 } },
new Line { Start = new Point { X = offsetX + 64, Y = offsetY + 56}, End = new Point { X = offsetX + 72, Y = offsetY + 48 } },
new Line { Start = new Point { X = offsetX + 72, Y = offsetY + 48}, End = new Point { X = offsetX + 80, Y = offsetY + 48 } },
new Line { Start = new Point { X = offsetX + 80, Y = offsetY + 48}, End = new Point { X = offsetX + 88, Y = offsetY + 56 } },
new Line { Start = new Point { X = offsetX + 88, Y = offsetY + 56}, End = new Point { X = offsetX + 88, Y = offsetY + 88 } },
new Line { Start = new Point { X = offsetX + 88, Y = offsetY + 88}, End = new Point { X = offsetX + 80, Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 80, Y = offsetY + 96}, End = new Point { X = offsetX + 72, Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 72, Y = offsetY + 96}, End = new Point { X = offsetX + 64, Y = offsetY + 88 } }
);
canvas.DrawEllipse(RandomColor(min, max), new Point { X = offsetX + 76, Y = offsetY + 72 }, 12, 24);
// t
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 104 + 12, Y = offsetY + 24}, End = new Point { X = offsetX + 104 + 12, Y = offsetY + 96 } },
@ -80,6 +73,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 144 + 48, Y = offsetY + 48 }, End = new Point { X = offsetX + 144 + 48 , Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 144 + 48, Y = offsetY + 96 }, End = new Point { X = offsetX + 144, Y = offsetY + 96 } }
);
// D
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 208, Y = offsetY + 96 }, End = new Point { X = offsetX + 208, Y = offsetY } },
@ -89,6 +83,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 208 + 48, Y = offsetY + 80 }, End = new Point { X = offsetX + 208 + 32, Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 208 + 32, Y = offsetY + 96 }, End = new Point { X = offsetX + 208, Y = offsetY + 96 } }
);
// L
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 272, Y = offsetY }, End = new Point { X = offsetX + 272, Y = offsetY + 96} },

Loading…
Cancel
Save