-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathBezierLine.cs
153 lines (133 loc) · 4.67 KB
/
BezierLine.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace GeminiGraphEditor
{
public class BezierLine : Shape
{
private const FrameworkPropertyMetadataOptions MetadataOptions =
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender;
public static readonly DependencyProperty X1Property = DependencyProperty.Register("X1",
typeof(double),
typeof(BezierLine),
new FrameworkPropertyMetadata(0.0, MetadataOptions));
public static readonly DependencyProperty X2Property = DependencyProperty.Register("X2",
typeof(double),
typeof(BezierLine),
new FrameworkPropertyMetadata(0.0, MetadataOptions));
public static readonly DependencyProperty Y1Property = DependencyProperty.Register("Y1",
typeof(double),
typeof(BezierLine),
new FrameworkPropertyMetadata(0.0, MetadataOptions));
public static readonly DependencyProperty Y2Property = DependencyProperty.Register("Y2",
typeof(double),
typeof(BezierLine),
new FrameworkPropertyMetadata(0.0, MetadataOptions));
private Geometry geometry;
public double X1
{
get => (double) GetValue(X1Property);
set => SetValue(X1Property, value);
}
public double X2
{
get => (double) GetValue(X2Property);
set => SetValue(X2Property, value);
}
public double Y1
{
get => (double) GetValue(Y1Property);
set => SetValue(Y1Property, value);
}
public double Y2
{
get => (double) GetValue(Y2Property);
set => SetValue(Y2Property, value);
}
protected override Geometry DefiningGeometry => geometry;
protected virtual PathSegmentCollection GetSegments()
{
PathSegmentCollection segments;
double midX = X1 + (X2 - X1) / 2;
double midY = Y1 + (Y2 - Y1) / 2;
if (X1 > X2 - 30)
{
double diff = Math.Max((X1 - X2) / 3, 70);
segments = new PathSegmentCollection
{
new BezierSegment
{
Point1 = new Point(X1 + diff, Y1),
Point2 = new Point(midX, midY),
Point3 = new Point(midX, midY),
IsStroked = true
},
new BezierSegment
{
Point1 = new Point(midX, midY),
Point2 = new Point(X2 - diff, Y2),
Point3 = new Point(X1, Y2),
IsStroked = true
}
};
}
else
{
segments = new PathSegmentCollection
{
new BezierSegment
{
Point1 = new Point(midX, Y1),
Point2 = new Point(midX, Y2),
Point3 = new Point(X2, Y2),
IsStroked = true
}
};
}
return segments;
}
protected override Size MeasureOverride(Size constraint)
{
PathSegmentCollection segments = GetSegments();
geometry = new PathGeometry
{
Figures =
{
new PathFigure
{
IsFilled = false,
StartPoint = new Point(X1, Y1),
Segments = segments
}
}
};
return base.MeasureOverride(constraint);
}
}
public class VerticalBezierLine : BezierLine
{
protected override PathSegmentCollection GetSegments()
{
double midX = X1 + (X2 - X1) / 2;
double midY = Y1 + (Y2 - Y1) / 2;
return new PathSegmentCollection
{
new BezierSegment
{
Point1 = new Point(X1, Y1 + 30),
Point2 = new Point(midX, midY),
Point3 = new Point(midX, midY),
IsStroked = true
},
new BezierSegment
{
Point1 = new Point(midX, midY),
Point2 = new Point(X2, Y2 - 30),
Point3 = new Point(X2, Y2),
IsStroked = true
}
};
}
}
}