forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle2.cs
141 lines (119 loc) · 4.26 KB
/
Circle2.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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Representation of a 2D circle
/// </summary>
[Serializable]
public struct Circle2 : IEquatable<Circle2>, IFormattable
{
public Vector2 center;
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 Circle2 unit => new Circle2(Vector2.zero, 1);
public Circle2(float radius) : this(Vector2.zero, radius)
{
}
public Circle2(Vector2 center, float radius)
{
this.center = center;
this.radius = radius;
}
/// <summary>
/// Returns a point on the circle at the given <paramref name="angle"/>
/// </summary>
/// <param name="angle">Angle in degrees</param>
public Vector2 GetPoint(float angle)
{
return Geometry.PointOnCircle2(center, radius, angle);
}
/// <summary>
/// Returns a list of evenly distributed points on the circle
/// </summary>
/// <param name="count">Number of points</param>
public List<Vector2> GetPoints(int count)
{
return Geometry.PointsOnCircle2(center, radius, count);
}
/// <summary>
/// Returns true if the point intersects the circle
/// </summary>
public bool Contains(Vector2 point)
{
return Intersect.PointCircle(point, center, radius);
}
/// <summary>
/// Linearly interpolates between two circles
/// </summary>
public static Circle2 Lerp(Circle2 a, Circle2 b, float t)
{
t = Mathf.Clamp01(t);
return new Circle2(a.center + (b.center - a.center)*t, a.radius + (b.radius - a.radius)*t);
}
/// <summary>
/// Linearly interpolates between two circles without clamping the interpolant
/// </summary>
public static Circle2 LerpUnclamped(Circle2 a, Circle2 b, float t)
{
return new Circle2(a.center + (b.center - a.center)*t, a.radius + (b.radius - a.radius)*t);
}
public static explicit operator Sphere(Circle2 circle)
{
return new Sphere((Vector3) circle.center, circle.radius);
}
public static explicit operator Circle3(Circle2 circle)
{
return new Circle3((Vector3) circle.center, Vector3.back, circle.radius);
}
public static Circle2 operator +(Circle2 circle, Vector2 vector)
{
return new Circle2(circle.center + vector, circle.radius);
}
public static Circle2 operator -(Circle2 circle, Vector2 vector)
{
return new Circle2(circle.center - vector, circle.radius);
}
public static bool operator ==(Circle2 a, Circle2 b)
{
return a.center == b.center && a.radius == b.radius;
}
public static bool operator !=(Circle2 a, Circle2 b)
{
return !(a == b);
}
public override int GetHashCode()
{
return center.GetHashCode() ^ (radius.GetHashCode() << 2);
}
public override bool Equals(object other)
{
return other is Circle2 && Equals((Circle2) other);
}
public bool Equals(Circle2 other)
{
return center.Equals(other.center) && radius.Equals(other.radius);
}
public override string ToString()
{
return string.Format("Circle2(center: {0}, radius: {1})", center, radius);
}
public string ToString(string format)
{
return string.Format("Circle2(center: {0}, radius: {1})", center.ToString(format), radius.ToString(format));
}
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format("Circle2(center: {0}, radius: {1})", center.ToString(format, formatProvider),
radius.ToString(format, formatProvider));
}
}
}