-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Circle3.cs
129 lines (109 loc) · 4.21 KB
/
Circle3.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
using System;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Representation of a 3D circle
/// </summary>
[Serializable]
public struct Circle3 : IEquatable<Circle3>, IFormattable
{
public Vector3 center;
public Vector3 normal;
public float radius;
/// <summary>
/// Returns the perimeter of the circle
/// </summary>
public float perimeter => 2*Mathf.PI*radius;
/// <summary>
/// Returns the area of the circle
/// </summary>
public float area => Mathf.PI*radius*radius;
public static Circle3 unitXY => new Circle3(Vector3.zero, Vector3.back, 1);
public static Circle3 unitXZ => new Circle3(Vector3.zero, Vector3.up, 1);
public static Circle3 unitYZ => new Circle3(Vector3.zero, Vector3.left, 1);
public Circle3(float radius) : this(Vector3.zero, Vector3.back, radius)
{
}
public Circle3(Vector3 center, float radius) : this(center, Vector3.back, radius)
{
}
public Circle3(Vector3 center, Vector3 normal, float radius)
{
this.center = center;
this.normal = normal;
this.radius = radius;
}
/// <summary>
/// Linearly interpolates between two circles
/// </summary>
public static Circle3 Lerp(Circle3 a, Circle3 b, float t)
{
t = Mathf.Clamp01(t);
return new Circle3(
center: a.center + (b.center - a.center)*t,
normal: Vector3.LerpUnclamped(a.normal, b.normal, t),
radius: a.radius + (b.radius - a.radius)*t);
}
/// <summary>
/// Linearly interpolates between two circles without clamping the interpolant
/// </summary>
public static Circle3 LerpUnclamped(Circle3 a, Circle3 b, float t)
{
return new Circle3(
center: a.center + (b.center - a.center)*t,
normal: Vector3.LerpUnclamped(a.normal, b.normal, t),
radius: a.radius + (b.radius - a.radius)*t);
}
public static explicit operator Sphere(Circle3 circle)
{
return new Sphere(circle.center, circle.radius);
}
public static explicit operator Circle2(Circle3 circle)
{
return new Circle2((Vector2) circle.center, circle.radius);
}
public static Circle3 operator +(Circle3 circle, Vector3 vector)
{
return new Circle3(circle.center + vector, circle.normal, circle.radius);
}
public static Circle3 operator -(Circle3 circle, Vector3 vector)
{
return new Circle3(circle.center - vector, circle.normal, circle.radius);
}
public static bool operator ==(Circle3 a, Circle3 b)
{
return a.center == b.center && a.normal == b.normal && a.radius == b.radius;
}
public static bool operator !=(Circle3 a, Circle3 b)
{
return !(a == b);
}
public override int GetHashCode()
{
return center.GetHashCode() ^ (normal.GetHashCode() << 2) ^ (radius.GetHashCode() >> 2);
}
public override bool Equals(object other)
{
return other is Circle3 && Equals((Circle3) other);
}
public bool Equals(Circle3 other)
{
return center.Equals(other.center) && normal.Equals(other.normal) && radius.Equals(other.radius);
}
public override string ToString()
{
return string.Format("Circle3(center: {0}, normal: {1}, radius: {2})", center, normal, radius);
}
public string ToString(string format)
{
return string.Format("Circle3(center: {0}, normal: {1}, radius: {2})",
center.ToString(format), normal.ToString(format), radius.ToString(format));
}
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format("Circle3(center: {0}, normal: {1}, radius: {2})",
center.ToString(format, formatProvider), normal.ToString(format, formatProvider), radius.ToString(format, formatProvider));
}
}
}