Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
BoundingRectangle.cs
浏览该文件的文档.
1namespace Engine {
2 public struct BoundingRectangle : IEquatable<BoundingRectangle> {
3 public Vector2 Min;
4
5 public Vector2 Max;
6
7 public IEnumerable<Vector2> Corners {
8 get {
9 yield return new Vector2(Min.X, Min.Y);
10 yield return new Vector2(Max.X, Min.Y);
11 yield return new Vector2(Min.X, Max.Y);
12 yield return new Vector2(Max.X, Max.Y);
13 }
14 }
15
16 public BoundingRectangle(float x1, float y1, float x2, float y2) {
17 Min = new Vector2(x1, y1);
18 Max = new Vector2(x2, y2);
19 }
20
22 Min = min;
23 Max = max;
24 }
25
26 public BoundingRectangle(IEnumerable<Vector2> points) {
27 ArgumentNullException.ThrowIfNull(points);
28 Min = new Vector2(float.PositiveInfinity);
29 Max = new Vector2(float.NegativeInfinity);
30 foreach (Vector2 point in points) {
31 Min.X = MathUtils.Min(Min.X, point.X);
32 Min.Y = MathUtils.Min(Min.Y, point.Y);
33 Max.X = MathUtils.Max(Max.X, point.X);
34 Max.Y = MathUtils.Max(Max.Y, point.Y);
35 }
36 if (Min.X == float.PositiveInfinity) {
37 throw new ArgumentException("points");
38 }
39 }
40
41 public static implicit operator BoundingRectangle((float X1, float Y1, float X2, float Y2) v) => new(v.X1, v.Y1, v.X2, v.Y2);
42
43 public override bool Equals(object obj) => obj is BoundingRectangle rectangle && Equals(rectangle);
44
45 public override int GetHashCode() => Min.GetHashCode() + Max.GetHashCode();
46
47 public override string ToString() => $"{Min},{Max}";
48
49 public bool Equals(BoundingRectangle other) => Min == other.Min && Max == other.Max;
50
51 public Vector2 Center() => new(0.5f * (Min.X + Max.X), 0.5f * (Min.Y + Max.Y));
52
53 public Vector2 Size() => Max - Min;
54
55 public float Area() {
56 Vector2 vector = Size();
57 return vector.X * vector.Y;
58 }
59
60 public bool Contains(Vector2 p) => p.X >= Min.X && p.X <= Max.X && p.Y >= Min.Y && p.Y <= Max.Y;
61
62 public bool Intersection(BoundingRectangle r) => r.Max.X >= Min.X && r.Min.X <= Max.X && r.Max.Y >= Min.Y && r.Min.Y <= Max.Y;
63
64 public bool Intersection(BoundingCircle circle) {
65 float num = circle.Center.X - Math.Clamp(circle.Center.X, Min.X, Max.X);
66 float num2 = circle.Center.Y - Math.Clamp(circle.Center.Y, Min.Y, Max.Y);
67 return num * num + num2 * num2 <= circle.Radius * circle.Radius;
68 }
69
71 Vector2 min = Vector2.Max(r1.Min, r2.Min);
72 Vector2 max = Vector2.Min(r1.Max, r2.Max);
73 return !(max.X > min.X) || !(max.Y > min.Y) ? default : new BoundingRectangle(min, max);
74 }
75
77 Vector2 min = Vector2.Min(r1.Min, r2.Min);
78 Vector2 max = Vector2.Max(r1.Max, r2.Max);
79 return new BoundingRectangle(min, max);
80 }
81
83 Vector2 min = Vector2.Min(r.Min, p);
84 Vector2 max = Vector2.Max(r.Max, p);
85 return new BoundingRectangle(min, max);
86 }
87
88 public static float Distance(BoundingRectangle r, Vector2 p) {
89 float num = MathUtils.Max(r.Min.X - p.X, 0f, p.X - r.Max.X);
90 float num2 = MathUtils.Max(r.Min.Y - p.Y, 0f, p.Y - r.Max.Y);
91 return MathF.Sqrt(num * num + num2 * num2);
92 }
93
94 public static bool operator ==(BoundingRectangle a, BoundingRectangle b) => a.Equals(b);
95
96 public static bool operator !=(BoundingRectangle a, BoundingRectangle b) => !a.Equals(b);
97 }
98}
static int Min(int x1, int x2)
static int Max(int x1, int x2)
BoundingRectangle(Vector2 min, Vector2 max)
static float Distance(BoundingRectangle r, Vector2 p)
static BoundingRectangle Union(BoundingRectangle r, Vector2 p)
override bool Equals(object obj)
BoundingRectangle(IEnumerable< Vector2 > points)
bool Equals(BoundingRectangle other)
bool Intersection(BoundingCircle circle)
static BoundingRectangle Intersection(BoundingRectangle r1, BoundingRectangle r2)
bool Intersection(BoundingRectangle r)
static bool operator!=(BoundingRectangle a, BoundingRectangle b)
IEnumerable< Vector2 > Corners
static bool operator==(BoundingRectangle a, BoundingRectangle b)
BoundingRectangle(float x1, float y1, float x2, float y2)
static BoundingRectangle Union(BoundingRectangle r1, BoundingRectangle r2)
static Vector2 Max(Vector2 v, float f)
static Vector2 Min(Vector2 v, float f)