Tanoda
Bezier.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10
12
13 public struct Bezier {
14
15 public Vector3 a;
16 public Vector3 b;
17 public Vector3 c;
18
19 public Bezier(Vector3 a, Vector3 b, Vector3 c) {
20 this.a = a; this.b = b; this.c = c;
21 }
22
23 public BezierPointEnumerator Points(int numPoints) {
24 return new BezierPointEnumerator(this, numPoints);
25 }
26
27 public BezierLineEnumerator Lines(int numLines) {
28 return new BezierLineEnumerator(this, numLines);
29 }
30
31 public struct BezierPointEnumerator {
34 this.bez = bez;
35 this.numPoints = Mathf.Max(2, numPoints);
36 this.idx = -1;
37 }
38 public Vector3 Current { get {
39 var t = Mathf.Max(0, idx) / (float)numPoints;
40 return Vector3.Lerp(
41 Vector3.Lerp(bez.a, bez.b, t),
42 Vector3.Lerp(bez.b, bez.c, t),
43 t
44 );
45 }}
46 public bool MoveNext() {
47 if (idx == numPoints) { return false; }
48 idx += 1;
49 return true;
50 }
51 public BezierPointEnumerator GetEnumerator() { return this; }
52 }
53
54 public struct BezierLineEnumerator {
56 Vector3? lastPoint;
57 public BezierLineEnumerator(Bezier bez, int numLines) {
58 numLines = Mathf.Max(1, numLines);
59 this.points = new BezierPointEnumerator(bez, numLines + 1);
60 lastPoint = null;
61 }
62 public LocalSegment3 Current { get {
63 if (!lastPoint.HasValue) {
65 }
66 else {
67 return new LocalSegment3(lastPoint.Value, points.Current);
68 }
69 }}
70 public bool MoveNext() {
71 if (!lastPoint.HasValue) {
72 if (!points.MoveNext()) { return false; }
73 }
75 if (!points.MoveNext()) { return false; }
76 return true;
77 }
78 public BezierLineEnumerator GetEnumerator() { return this; }
79 }
80
81 }
82
83}
BezierLineEnumerator(Bezier bez, int numLines)
Definition: Bezier.cs:57
BezierPointEnumerator(Bezier bez, int numPoints)
Definition: Bezier.cs:33
Bezier(Vector3 a, Vector3 b, Vector3 c)
Definition: Bezier.cs:19
BezierPointEnumerator Points(int numPoints)
Definition: Bezier.cs:23
BezierLineEnumerator Lines(int numLines)
Definition: Bezier.cs:27