forked from FreyaHolmer/Mathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle.cs
58 lines (43 loc) · 1.83 KB
/
Circle.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
// Collected and expanded upon to by Freya Holmér (https://github.com/FreyaHolmer/Mathfs)
using UnityEngine;
using static Freya.Mathfs;
namespace Freya {
// Circle math
public struct Circle {
public Vector2 center;
public float radius;
public Circle( Vector2 center, float radius ) {
this.center = center;
this.radius = radius;
}
float Area => RadiusToArea( radius );
float Circumference => RadiusToCircumference( radius );
public static float RadiusToArea( float r ) => r * r * ( 0.5f * TAU );
public static float AreaToRadius( float area ) => Sqrt( 2 * area / TAU );
public static float AreaToCircumference( float area ) => Sqrt( 2 * area / TAU ) * TAU;
public static float CircumferenceToArea( float c ) => c * c / ( 2 * TAU );
public static float RadiusToCircumference( float r ) => r * TAU;
public static float CircumferenceToRadius( float c ) => c / TAU;
public static Circle FromTwoPoints( Vector2 a, Vector2 b ) => new Circle( ( a + b ) / 2f, Vector2.Distance( a, b ) / 2f );
public static bool FromPointTangentPoint( Vector2 startPt, Vector2 startTangent, Vector2 endPt, out Circle circle ) {
Line2D lineA = new Line2D( startPt, startTangent.Rotate90CW() );
Line2D lineB = LineSegment2D.GetBisectorFast( startPt, endPt );
if( Intersect.Lines( lineA, lineB, out Vector2 pt ) ) {
circle = new Circle( pt, Vector2.Distance( pt, startPt ) );
return true;
}
circle = default;
return false;
}
public static bool FromThreePoints( Vector2 a, Vector2 b, Vector2 c, out Circle circle ) {
Line2D lineA = LineSegment2D.GetBisectorFast( a, b );
Line2D lineB = LineSegment2D.GetBisectorFast( b, c );
if( Intersect.Lines( lineA, lineB, out circle.center ) ) {
circle.radius = Vector2.Distance( circle.center, a );
return true;
}
circle = default;
return false;
}
}
}