This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maths.cs
117 lines (115 loc) · 3.31 KB
/
Maths.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
using System;
namespace Engine;
public struct Point {
public int X { get; set; }
public int Y { get; set; }
public const float Rad2Deg = 180f / (float)Math.PI;
public const float Deg2Rad = (float)Math.PI / 180f;
/// <summary> new Point(0, 0); </summary>
public static Point Zero { get; private set; } = new Point(0, 0);
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public Vector ToVector() => new Vector((float)X, (float)Y);
public override string ToString() => String.Format("({0}, {1})", X, Y);
public static Point operator +(Point a, Point b) {
return new Point(a.X + b.X, a.Y + b.Y);
}
public static Point operator -(Point a, Point b) {
return new Point(a.X - b.X, a.Y - b.Y);
}
public static Point operator +(Point a, int b)
{
return new Point(a.X + b, a.Y + b);
}
public static Point operator -(Point a, int b)
{
return new Point(a.X - b, a.Y - b);
}
public static Point operator /(Point a, float b) {
return new Point((int)(a.X / b), (int)(a.Y / b));
}
public static Point operator *(Point a, float b) {
return new Point((int)(a.X * b), (int)(a.Y * b));
}
public static bool operator ==(Point a, Point b)
{
return a.X == b.X && a.Y == b.Y;
}
public static bool operator !=(Point a, Point b)
{
return a.X != b.X || a.Y != b.Y;
}
public static bool operator <=(Point a, Point b)
{
return a.X <= b.X && a.Y <= b.Y;
}
public static bool operator >=(Point a, Point b)
{
return a.X >= b.X && a.Y >= b.Y;
}
public static bool operator <(Point a, Point b)
{
return a.X < b.X && a.Y < b.Y;
}
public static bool operator >(Point a, Point b)
{
return a.X > b.X && a.Y > b.Y;
}
public override bool Equals(object obj) {
return Equals(obj);
}
public override int GetHashCode() {
return GetHashCode();
}
/// <summary> Calculates distance between two points. </summary>
/// <param name="a">Point A</param>
/// <param name="b">Point B</param>
/// <returns>Distance between A and B</returns>
public static float Distance(Point a, Point b) {
Point dV = b - a;
float d = (float)Math.Sqrt(Math.Pow(dV.X, 2) + Math.Pow(dV.Y, 2));
return d;
}
public void Clamp(Point min, Point max) {
X = (X > max.X) ? max.X : X;
X = (X < min.X) ? min.X : X;
Y = (Y > max.Y) ? max.Y : Y;
Y = (Y < min.Y) ? min.Y : Y;
}
}
public struct Vector {
public float X { get; set; }
public float Y { get; set; }
public static Vector Zero { get; private set; } = new Vector(0, 0);
public Vector(float x, float y) {
this.X = x;
this.Y = y;
}
public Point ToPoint => new Point((int)Math.Round(X, 0), (int)Math.Round(Y, 0));
public void Rotate(float a) {
Vector n = Vector.Zero;
n.X = (float)(X * Math.Cos(a / 57.3f) - Y * Math.Sin(a / 57.3f));
n.Y = (float)(X * Math.Sin(a / 57.3f) + Y * Math.Cos(a / 57.3f));
X = n.X;
Y = n.Y;
}
public static Vector operator + (Vector a, Vector b) {
return new Vector(a.X + b.X, a.Y + b.Y);
}
public static Vector operator - (Vector a, Vector b) {
return new Vector(a.X - b.X, a.Y - b.Y);
}
public static Vector operator / (Vector a, float b) {
return new Vector((a.X / b), (a.Y / b));
}
public static Vector operator * (Vector a, float b) {
return new Vector((a.X * b), (a.Y * b));
}
public static float Distance(Vector a, Vector b) {
Vector dV = b - a;
float d = (float)Math.Sqrt(Math.Pow(dV.X, 2) + Math.Pow(dV.Y, 2));
return d;
}
}