Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Segment2.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
4 public struct Segment2 {
5 public Vector2 Start;
6
7 public Vector2 End;
8
9 public Segment2(Vector2 start, Vector2 end) {
10 Start = start;
11 End = end;
12 }
13
14 public override string ToString() => $"{Start.X}, {Start.Y}, {End.X}, {End.Y}";
15
16 public static float Distance(Segment2 s, Vector2 p) {
17 Vector2 v = s.End - s.Start;
18 Vector2 v2 = s.Start - p;
19 Vector2 v3 = s.End - p;
20 float num = Vector2.Dot(v2, v);
21 if (num * Vector2.Dot(v3, v) <= 0f) {
22 float num2 = v.LengthSquared();
23 if (num2 == 0f) {
24 return Vector2.Distance(p, s.Start);
25 }
26 return MathF.Abs(Vector2.Cross(p - s.Start, v)) / MathF.Sqrt(num2);
27 }
28 if (!(num > 0f)) {
29 return v3.Length();
30 }
31 return v2.Length();
32 }
33
34 public static bool Intersection(Segment2 s1, Segment2 s2, out Vector2 result) {
35 Vector2 v = s1.End - s1.Start;
36 Vector2 v2 = s2.End - s2.Start;
37 float num = Vector2.Cross(v, v2);
38 if (num == 0f) {
39 result = Vector2.Zero;
40 return false;
41 }
42 float num2 = 1f / num;
43 float num3 = (v2.X * (s1.Start.Y - s2.Start.Y) - v2.Y * (s1.Start.X - s2.Start.X)) * num2;
44 float num4 = (v.X * (s1.Start.Y - s2.Start.Y) - v.Y * (s1.Start.X - s2.Start.X)) * num2;
45 if (num3 < 0f
46 || num3 > 1f
47 || num4 < 0f
48 || num4 > 1f) {
49 result = Vector2.Zero;
50 return false;
51 }
52 result = new Vector2(s1.Start.X + num3 * v.X, s1.Start.Y + num3 * v.Y);
53 return true;
54 }
55
56 public static Vector2 NearestPoint(Segment2 s, Vector2 p) {
57 Vector2 v = s.End - s.Start;
58 Vector2 v2 = s.Start - p;
59 Vector2 v3 = s.End - p;
60 float num = Vector2.Dot(v2, v);
61 if (num * Vector2.Dot(v3, v) <= 0f) {
62 float num2 = v.Length();
63 if (num2 == 0f) {
64 return s.Start;
65 }
66 float num3 = MathF.Sqrt(v2.LengthSquared() - MathUtils.Sqr(MathF.Abs(Vector2.Cross(p - s.Start, v)) / num2));
67 return Vector2.Lerp(s.Start, s.End, num3 / num2);
68 }
69 if (!(num > 0f)) {
70 return s.End;
71 }
72 return s.Start;
73 }
74 }
75}
static int Sqr(int x)
static float Dot(Vector2 v1, Vector2 v2)
static float Cross(Vector2 v1, Vector2 v2)
static readonly Vector2 Zero
static Vector2 Lerp(Vector2 v1, Vector2 v2, float f)
float LengthSquared()
static float Distance(Vector2 v1, Vector2 v2)
static bool Intersection(Segment2 s1, Segment2 s2, out Vector2 result)
override string ToString()
Segment2(Vector2 start, Vector2 end)
static float Distance(Segment2 s, Vector2 p)
static Vector2 NearestPoint(Segment2 s, Vector2 p)