Skip to content

Commit 64479de

Browse files
committed
added ellipticalarc to PathBuilder, fixed Bugs in EllipticalArcLineSegment
1 parent 952fc98 commit 64479de

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/ImageSharp.Drawing/Shapes/EllipticalArcLineSegment.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Numerics;
77

8-
namespace SixLabors.ImageSharp.Drawing.Shapes
8+
namespace SixLabors.ImageSharp.Drawing
99
{
1010
/// <summary>
1111
/// Represents a line segment that contains radii and angles that will be rendered as a elliptical arc
@@ -69,6 +69,7 @@ public EllipticalArcLineSegment Transform(Matrix3x2 matrix)
6969
}
7070

7171
// TODO
72+
return this;
7273
}
7374

7475
/// <summary>
@@ -88,31 +89,40 @@ private PointF[] GetDrawingPoints()
8889
for (float i = this.startAngle; i < this.startAngle + this.sweepAngle; i++)
8990
{
9091
float end = i + 1;
91-
if (end <= this.startAngle + this.sweepAngle)
92+
if (end >= this.startAngle + this.sweepAngle)
9293
{
9394
end = this.startAngle + this.sweepAngle;
9495
}
9596

96-
points.AddRange(this.GetDrawingPoints(points[points.Count - 1], i, end));
97+
points.AddRange(this.GetDrawingPoints( i, end,0));
9798
}
9899

99100
return points.ToArray();
100101
}
101102

102-
private List<PointF> GetDrawingPoints(PointF start, float startAngle, float end)
103+
private List<PointF> GetDrawingPoints(float start, float end, int depth)
103104
{
105+
if (depth > 1000)
106+
{
107+
return new List<PointF>();
108+
}
109+
104110
var points = new List<PointF>();
111+
112+
float startX = (float)((this.firstRadius * Math.Sin(Math.PI * start / 180) * Math.Cos(Math.PI * this.rotation / 180)) - (this.secondRadius * Math.Cos(Math.PI * start / 180) * Math.Sin(Math.PI * this.rotation / 180)) + this.center.X);
113+
float startY = (float)((this.firstRadius * Math.Sin(Math.PI * start / 180) * Math.Sin(Math.PI * this.rotation / 180)) + (this.secondRadius * Math.Cos(Math.PI * start / 180) * Math.Cos(Math.PI * this.rotation / 180)) + this.center.Y);
114+
105115
float endX = (float)((this.firstRadius * Math.Sin(Math.PI * end / 180) * Math.Cos(Math.PI * this.rotation / 180)) - (this.secondRadius * Math.Cos(Math.PI * end / 180) * Math.Sin(Math.PI * this.rotation / 180)) + this.center.X);
106116
float endY = (float)((this.firstRadius * Math.Sin(Math.PI * end / 180) * Math.Sin(Math.PI * this.rotation / 180)) + (this.secondRadius * Math.Cos(Math.PI * end / 180) * Math.Cos(Math.PI * this.rotation / 180)) + this.center.Y);
107-
if ((new Vector2(endX, endY) - new Vector2(start.X, start.Y)).LengthSquared() < MinimumSqrDistance)
117+
if ((new Vector2(endX, endY) - new Vector2(startX, startY)).LengthSquared() < MinimumSqrDistance)
108118
{
109119
points.Add(new PointF(endX, endY));
110120
}
111121
else
112122
{
113-
float mid = startAngle + ((startAngle - end) / 2);
114-
points.AddRange(this.GetDrawingPoints(start, startAngle, mid));
115-
points.AddRange(this.GetDrawingPoints(points[points.Count - 1], mid, end));
123+
float mid = start + ((end - start) / 2);
124+
points.AddRange(this.GetDrawingPoints(start, mid, depth + 1));
125+
points.AddRange(this.GetDrawingPoints(mid, end, depth + 1));
116126
}
117127

118128
return points;

src/ImageSharp.Drawing/Shapes/PathBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF con
199199
return this;
200200
}
201201

202+
public PathBuilder AddEllipticalArc(PointF center, float firstRadius, float secondRadius, float rotation, float startAngle, float sweepAngle)
203+
{
204+
EllipticalArcLineSegment arc = new EllipticalArcLineSegment(center, firstRadius, secondRadius, rotation, startAngle, sweepAngle);
205+
arc.Transform(this.currentTransform);
206+
this.currentFigure.AddSegment(arc);
207+
return this;
208+
}
209+
202210
/// <summary>
203211
/// Starts a new figure but leaves the previous one open.
204212
/// </summary>

0 commit comments

Comments
 (0)