diff --git a/VectSharp/Graphics.cs b/VectSharp/Graphics.cs index 6b45e24e..bc73032d 100644 --- a/VectSharp/Graphics.cs +++ b/VectSharp/Graphics.cs @@ -775,19 +775,78 @@ public Segment[] ToBezierSegments() { List tbr = new List(); - if (EndAngle - StartAngle <= Math.PI / 2) + if (EndAngle > StartAngle) { - tbr.AddRange(GetBezierSegment(Points[0].X, Points[0].Y, Radius, StartAngle, EndAngle, true)); + if (EndAngle - StartAngle <= Math.PI / 2) + { + tbr.AddRange(GetBezierSegment(Points[0].X, Points[0].Y, Radius, StartAngle, EndAngle, true)); + } + else + { + int count = (int)Math.Ceiling(2 * (EndAngle - StartAngle) / Math.PI); + double angle = StartAngle; + + for (int i = 0; i < count; i++) + { + tbr.AddRange(GetBezierSegment(Points[0].X, Points[0].Y, Radius, angle, angle + (EndAngle - StartAngle) / count, i == 0)); + angle += (EndAngle - StartAngle) / count; + } + } } - else + else if (EndAngle < StartAngle) { - int count = (int)Math.Ceiling(2 * (EndAngle - StartAngle) / Math.PI); - double angle = StartAngle; + Point startPoint = new Point(Points[0].X + Radius * Math.Cos(EndAngle), Points[0].Y + Radius * Math.Sin(EndAngle)); + if (StartAngle - EndAngle <= Math.PI / 2) + { + tbr.AddRange(GetBezierSegment(Points[0].X, Points[0].Y, Radius, EndAngle, StartAngle, true)); + } + else + { + int count = (int)Math.Ceiling(2 * (StartAngle - EndAngle) / Math.PI); + double angle = EndAngle; - for (int i = 0; i < count; i++) + for (int i = 0; i < count; i++) + { + tbr.AddRange(GetBezierSegment(Points[0].X, Points[0].Y, Radius, angle, angle + (StartAngle - EndAngle) / count, i == 0)); + angle += (StartAngle - EndAngle) / count; + } + } + + return ReverseSegments(tbr, startPoint).ToArray(); + } + + return tbr.ToArray(); + } + + private static Segment[] ReverseSegments(IReadOnlyList originalSegments, Point startPoint) + { + List tbr = new List(originalSegments.Count); + + for (int i = originalSegments.Count - 1; i >= 0; i--) + { + switch (originalSegments[i].Type) { - tbr.AddRange(GetBezierSegment(Points[0].X, Points[0].Y, Radius, angle, angle + (EndAngle - StartAngle) / count, i == 0)); - angle += (EndAngle - StartAngle) / count; + case SegmentType.Line: + if (i > 0) + { + tbr.Add(new LineSegment(originalSegments[i - 1].Point)); + } + else + { + tbr.Add(new LineSegment(startPoint)); + } + break; + case SegmentType.CubicBezier: + CubicBezierSegment originalSegment = (CubicBezierSegment)originalSegments[i]; + if (i > 0) + { + tbr.Add(new CubicBezierSegment(originalSegment.Points[1], originalSegment.Points[0], originalSegments[i - 1].Point)); + } + else + { + tbr.Add(new CubicBezierSegment(originalSegment.Points[1], originalSegment.Points[0], startPoint)); + } + break; } } diff --git a/VectSharp/VectSharp.csproj b/VectSharp/VectSharp.csproj index 69daf002..d55f94d3 100644 --- a/VectSharp/VectSharp.csproj +++ b/VectSharp/VectSharp.csproj @@ -5,9 +5,9 @@ Giorgio Bianchini University of Bristol A library to produce vector graphics (including text) without too many dependencies. Extensible to support multiple output formats. This is the base package, and does not provide any output formats. For more information, see https://github.com/arklumpus/VectSharp - 1.2.0 - 1.2.0.0 - 1.2.0.0 + 1.2.1 + 1.2.1.0 + 1.2.1.0