-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathVisualizer.cs
115 lines (96 loc) · 3.72 KB
/
Visualizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Numerics;
namespace BiArcTutorial
{
public class Visualizer : BindableObject, IDrawable
{
public static readonly BindableProperty BezierProperty =
BindableProperty.Create(nameof(Bezier), typeof(CubicBezier), typeof(Visualizer), null);
public static readonly BindableProperty ApproxProperty =
BindableProperty.Create(nameof(Approx), typeof(List<Approx>), typeof(Visualizer), null);
public static readonly BindableProperty SelectedArcIndexProperty =
BindableProperty.Create(nameof(SelectedArcIndex), typeof(int), typeof(Visualizer), null);
public CubicBezier Bezier {
get => (CubicBezier)GetValue(BezierProperty);
set => SetValue(BezierProperty, value);
}
public List<Approx> Approx
{
get => (List<Approx>)GetValue(ApproxProperty);
set => SetValue(ApproxProperty, value);
}
public int SelectedArcIndex
{
get => (int)GetValue(SelectedArcIndexProperty);
set => SetValue(SelectedArcIndexProperty, value);
}
public void Draw(ICanvas canvas, RectF dirtyRect)
{
int currArcIdx = 1;
foreach (var approx in Approx)
{
DrawApproxCircle(canvas, currArcIdx++, approx.BiArc.A1);
DrawApproxCircle(canvas, currArcIdx++, approx.BiArc.A2);
}
canvas.StrokeColor = Colors.Black;
canvas.StrokeSize = 3;
PathF bezierPath = new PathF();
bezierPath.MoveTo(Bezier.P1);
bezierPath.CurveTo(Bezier.C1, Bezier.C2, Bezier.P2);
canvas.DrawPath(bezierPath);
currArcIdx = 1;
foreach (var approx in Approx)
{
DrawApproxArc(canvas, currArcIdx++, approx.BiArc.A1);
DrawApproxArc(canvas, currArcIdx++, approx.BiArc.A2);
}
canvas.StrokeSize = 1;
foreach (var approx in Approx)
{
foreach (var (p1,p2,color) in approx.DebugLines)
{
canvas.StrokeColor = color;
canvas.DrawLine(p1, p2);
}
}
}
private void DrawApproxCircle(ICanvas canvas, int currArcIdx, Arc arc)
{
if (arc.r != 0.0)
{
if (currArcIdx == SelectedArcIndex)
{
canvas.StrokeColor = Colors.LightGreen;
canvas.StrokeSize = 3;
}
else
{
canvas.StrokeColor = Colors.Green;
canvas.StrokeSize = 1;
}
canvas.DrawEllipse(arc.C.X - arc.r, arc.C.Y - arc.r, 2 * arc.r, 2 * arc.r);
}
}
private void DrawApproxArc(ICanvas canvas, int currArcIdx, Arc arc)
{
if (arc.r != 0.0)
{
if (currArcIdx == SelectedArcIndex)
{
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 3;
}
else
{
canvas.StrokeColor = Colors.Blue;
canvas.StrokeSize = 3;
}
// MAUI seemingly has opposite angle direction than System.Drawing
var startAngle = -1 * arc.startAngle * 180.0f / (float)Math.PI;
var endAngle = startAngle - arc.sweepAngle * 180.0f / (float)Math.PI;
canvas.DrawArc(arc.C.X - arc.r, arc.C.Y - arc.r, 2 * arc.r, 2 * arc.r,
startAngle, endAngle, arc.IsClockwise, false);
}
}
}
}