Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PathState.cs
1using System;
2using SkiaSharp;
3
5{
9 public class PathState
10 {
11 private readonly Path pathDef;
12 private readonly SKPath path;
13 private readonly bool calcStart;
14 private readonly bool calcEnd;
15 private float x0, y0;
16 private float x, y;
17 private float xPrev, yPrev;
18 private float? angle;
19 private bool drawingSpline = false;
20 private PathSpline currentSpline = null;
21 private bool nonSpline = false;
22
30 public PathState(Path PathDef, SKPath Path, bool CalcStart, bool CalcEnd)
31 {
32 this.pathDef = PathDef;
33 this.path = Path;
34 this.calcStart = CalcStart;
35 this.calcEnd = CalcEnd;
36 }
37
41 public float X => this.x;
42
46 public float Y => this.y;
47
51 public float XPrev => this.xPrev;
52
56 public float YPrev => this.yPrev;
57
61 public bool CalcStart => this.calcStart;
62
66 public bool CalcEnd => this.calcEnd;
67
71 public float Angle
72 {
73 get
74 {
75 if (!this.angle.HasValue)
76 {
77 float dx = this.x - this.xPrev;
78 float dy = this.y - this.yPrev;
79
80 if (dx == 0 && dy == 0)
81 this.angle = 0;
82 else
83 this.angle = (float)Math.Atan2(dy, dx);
84 }
85
86 return this.angle.Value;
87 }
88 }
89
95 public void Set0(float X, float Y)
96 {
97 this.Set(X, Y);
98 this.x0 = X;
99 this.y0 = Y;
100 this.nonSpline = false;
101 }
102
109 public SKPoint Add0(float DeltaX, float DeltaY)
110 {
111 SKPoint P = this.Add(DeltaX, DeltaY);
112
113 this.nonSpline = false;
114 this.x0 = P.X;
115 this.y0 = P.Y;
116
117 return P;
118 }
119
123 public void CloseLine()
124 {
125 this.Set(this.x0, this.y0);
126 this.path?.Close();
127 this.nonSpline = false;
128 }
129
133 public void CloseLoop()
134 {
135 if (this.drawingSpline)
136 {
137 if (this.nonSpline)
138 {
139 this.AddSplineVertex(this.x0, this.y0);
140 this.FlushSpline();
141 this.path?.Close();
142 this.nonSpline = false;
143 }
144 else
145 {
146 Loop.CreateLoop(this.path, this.currentSpline.ToArray());
147 this.currentSpline = null;
148 }
149
150 this.drawingSpline = false;
151 }
152 else
153 this.CloseLine();
154 }
155
161 public void Set(float X, float Y)
162 {
163 if (this.drawingSpline)
164 this.FlushSpline();
165
166 this.nonSpline = true;
167 if (X != this.x || Y != this.y)
168 {
169 this.xPrev = this.x;
170 this.x = X;
171
172 this.yPrev = this.y;
173 this.y = Y;
174
175 this.angle = null;
176
177 this.pathDef?.IncludePoint(X, Y);
178 }
179 }
180
185 public SKPoint Add(float DeltaX, float DeltaY)
186 {
187 DeltaX += this.x;
188 DeltaY += this.y;
189
190 this.Set(DeltaX, DeltaY);
191
192 return new SKPoint(DeltaX, DeltaY);
193 }
194
200 public SKPoint Forward(float Distance)
201 {
202 if (this.drawingSpline)
203 this.FlushSpline();
204
205 if (Distance != 0)
206 {
207 float Radians = this.Angle;
208 float Dx = (float)(Distance * Math.Cos(Radians));
209 float Dy = (float)(Distance * Math.Sin(Radians));
210
211 return this.Add(Dx, Dy);
212 }
213 else
214 return new SKPoint(this.x, this.y);
215 }
216
222 public SKPoint Backward(float Distance)
223 {
224 return this.Forward(-Distance);
225 }
226
231 public void TurnLeft(float DeltaDegrees)
232 {
233 if (this.drawingSpline)
234 this.FlushSpline();
235
236 this.angle = this.Angle - DeltaDegrees * LayoutElement.DegreesToRadians;
237 }
238
243 public void TurnRight(float DeltaDegrees)
244 {
245 if (this.drawingSpline)
246 this.FlushSpline();
247
248 this.angle = this.Angle + DeltaDegrees * LayoutElement.DegreesToRadians;
249 }
250
254 public void TurnTowards(float X, float Y)
255 {
256 this.TurnTowardsRel(X - this.x, Y - this.y);
257 }
258
262 public void TurnTowardsRel(float DeltaX, float DeltaY)
263 {
264 if (this.drawingSpline)
265 this.FlushSpline();
266
267 if (DeltaX == 0 && DeltaY == 0)
268 this.angle = 0;
269 else
270 this.angle = (float)Math.Atan2(DeltaY, DeltaX);
271 }
272
277 public void TurnTowards(float Angle)
278 {
279 if (this.drawingSpline)
280 this.FlushSpline();
281
282 this.angle = Angle;
283 }
284
288 public void FlushSpline()
289 {
290 if (this.drawingSpline)
291 {
292 this.drawingSpline = false;
293
294 Spline.CreateSpline(this.path, this.currentSpline.ToArray());
295 this.currentSpline = null;
296 this.nonSpline = false;
297 }
298 }
299
306 public PathSpline SetSplineVertex(float X, float Y)
307 {
308 if (this.path is null)
309 {
310 this.Set(X, Y);
311 return null;
312 }
313 else
314 {
315 if (this.currentSpline is null)
316 this.currentSpline = new PathSpline(this.x, this.y);
317
318 this.currentSpline.Add(X, Y);
319 this.drawingSpline = true;
320
321 if (X != this.x || Y != this.y)
322 {
323 this.xPrev = this.x;
324 this.x = X;
325
326 this.yPrev = this.y;
327 this.y = Y;
328
329 this.angle = null;
330
331 this.pathDef?.IncludePoint(X, Y);
332 }
333
334 return this.currentSpline;
335 }
336 }
337
344 public PathSpline AddSplineVertex(float DeltaX, float DeltaY)
345 {
346 return this.SetSplineVertex(this.x + DeltaX, this.y + DeltaY);
347 }
348 }
349}
Abstract base class for angle elements.
Definition: Angle.cs:11
Abstract base class for distance elements.
Definition: Distance.cs:11
static SKPath CreateLoop(params Vertex[] Points)
Creates a Loop (Closed Spline) path through a given set of points.
Definition: Loop.cs:44
Closes a path (i.e. returns to the origin), using a line.
Definition: CloseLine.cs:10
Draws a line to a point that lies a certain distance forward of the last point, in the current direct...
Definition: Forward.cs:11
SKPoint[] ToArray()
Returns an array of vertices of the spline curve.
Definition: PathSpline.cs:37
void TurnRight(float DeltaDegrees)
Turns the current direction right.
Definition: PathState.cs:243
void TurnLeft(float DeltaDegrees)
Turns the current direction left.
Definition: PathState.cs:231
void Set(float X, float Y)
Sets a new coordinate
Definition: PathState.cs:161
void TurnTowards(float Angle)
Turns the current direction towards a given direction.
Definition: PathState.cs:277
void TurnTowards(float X, float Y)
Turns the current direction towards a given point.
Definition: PathState.cs:254
SKPoint Backward(float Distance)
Moves forward
Definition: PathState.cs:222
SKPoint Forward(float Distance)
Moves forward
Definition: PathState.cs:200
void TurnTowardsRel(float DeltaX, float DeltaY)
Turns the current direction towards a given point, relative to the current point.
Definition: PathState.cs:262
void CloseLoop()
Closes the current contour, using a spline, creating a closed smooth loop.
Definition: PathState.cs:133
SKPoint Add0(float DeltaX, float DeltaY)
Sets the start coordinate of a new contour, relative to the last point.
Definition: PathState.cs:109
PathState(Path PathDef, SKPath Path, bool CalcStart, bool CalcEnd)
Current state tracing the path.
Definition: PathState.cs:30
PathSpline SetSplineVertex(float X, float Y)
Sets a new spline vertex
Definition: PathState.cs:306
void CloseLine()
Closes the current contour, using a line.
Definition: PathState.cs:123
bool CalcStart
If start position and angle are to be calculated.
Definition: PathState.cs:61
PathSpline AddSplineVertex(float DeltaX, float DeltaY)
Sets a new spline vertex, relative to the last coordinate
Definition: PathState.cs:344
bool CalcEnd
If end position and angle are to be calculated.
Definition: PathState.cs:66
SKPoint Add(float DeltaX, float DeltaY)
Sets a new coordinate
Definition: PathState.cs:185
void FlushSpline()
Closes an ongoing spline curve (if one open).
Definition: PathState.cs:288
void Set0(float X, float Y)
Sets the start coordinate of a new contour.
Definition: PathState.cs:95
Turns the current direction towards a point, relative to the current point.
static SKPath CreateSpline(params Vertex[] Points)
Creates a Spline path through a given set of points.
Definition: Spline.cs:102
Abstract base class for layout elements.