-
Notifications
You must be signed in to change notification settings - Fork 8
/
FlatEllipse.cs
70 lines (56 loc) · 2.01 KB
/
FlatEllipse.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
using System;
using Microsoft.Xna.Framework;
namespace Flat
{
public readonly struct FlatEllipse
{
public readonly Vector2 Center;
public readonly Vector2 Radius;
public FlatEllipse(Vector2 center, Vector2 radius)
{
this.Center = center;
this.Radius = radius;
}
public void GetExtents(out FlatBox box)
{
Vector2 min = new Vector2(this.Center.X - Radius.X, this.Center.Y - this.Radius.Y);
Vector2 max = new Vector2(this.Center.X + Radius.X, this.Center.Y + this.Radius.Y);
box = new FlatBox(min, max);
}
public bool Contains(Vector2 v, out float d)
{
// https://math.stackexchange.com/questions/76457/check-if-a-point-is-within-an-ellipse
float dx = v.X - this.Center.X;
float dy = v.Y - this.Center.Y;
float num1 = dx * dx;
float den1 = this.Radius.X * this.Radius.X;
float num2 = dy * dy;
float den2 = this.Radius.Y * this.Radius.Y;
d = num1 / den1 + num2 / den2;
// todo: Should this be less than OR EQUAL TO instead of just less than?
return d < 1f;
}
public bool Equals(FlatEllipse other)
{
return this.Center == other.Center && this.Radius == other.Radius;
}
public override bool Equals(object obj)
{
if(obj is FlatEllipse other)
{
return this.Equals(other);
}
return false;
}
public override int GetHashCode()
{
int result = new { this.Center, this.Radius }.GetHashCode();
return result;
}
public override string ToString()
{
string result = string.Format("Center: {0}, Radius(X): {1}, Radius(Y): {2}", this.Center, this.Radius.X, this.Radius.Y);
return result;
}
}
}