Skip to content

Commit

Permalink
Fixed bug in Arc when EndAngle is smaller than StartAngle.
Browse files Browse the repository at this point in the history
  • Loading branch information
arklumpus committed May 28, 2020
1 parent e750230 commit aae74c9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
75 changes: 67 additions & 8 deletions VectSharp/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,19 +775,78 @@ public Segment[] ToBezierSegments()
{
List<Segment> tbr = new List<Segment>();

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<Segment> originalSegments, Point startPoint)
{
List<Segment> tbr = new List<Segment>(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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions VectSharp/VectSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<Authors>Giorgio Bianchini</Authors>
<Company>University of Bristol</Company>
<Description>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</Description>
<Version>1.2.0</Version>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
<Version>1.2.1</Version>
<AssemblyVersion>1.2.1.0</AssemblyVersion>
<FileVersion>1.2.1.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down

0 comments on commit aae74c9

Please sign in to comment.