-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cs
119 lines (88 loc) · 2.43 KB
/
Vector.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
118
119
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace PolygonIntersection {
public struct Vector {
public float X;
public float Y;
static public Vector FromPoint(Point p) {
return Vector.FromPoint(p.X, p.Y);
}
static public Vector FromPoint(int x, int y) {
return new Vector((float)x, (float)y);
}
public Vector(float x, float y) {
this.X = x;
this.Y = y;
}
public float Magnitude {
get { return (float)Math.Sqrt(X * X + Y * Y); }
}
public void Normalize() {
float magnitude = Magnitude;
X = X / magnitude;
Y = Y / magnitude;
}
public Vector GetNormalized() {
float magnitude = Magnitude;
return new Vector(X / magnitude, Y / magnitude);
}
public float DotProduct(Vector vector) {
return this.X * vector.X + this.Y * vector.Y;
}
public float DistanceTo(Vector vector) {
return (float)Math.Sqrt(Math.Pow(vector.X - this.X, 2) + Math.Pow(vector.Y - this.Y, 2));
}
public static implicit operator Point(Vector p) {
return new Point((int)p.X, (int)p.Y);
}
public static implicit operator PointF(Vector p) {
return new PointF(p.X, p.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) {
return new Vector(-a.X, -a.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, int b) {
return new Vector(a.X * b, a.Y * b);
}
public static Vector operator *(Vector a, double b) {
return new Vector((float)(a.X * b), (float)(a.Y * b));
}
public override bool Equals(object obj) {
Vector v = (Vector)obj;
return X == v.X && Y == v.Y;
}
public bool Equals(Vector v) {
return X == v.X && Y == v.Y;
}
public override int GetHashCode() {
return X.GetHashCode() ^ Y.GetHashCode();
}
public static bool operator ==(Vector a, Vector b) {
return a.X == b.X && a.Y == b.Y;
}
public static bool operator !=(Vector a, Vector b) {
return a.X != b.X || a.Y != b.Y;
}
public override string ToString() {
return X + ", " + Y;
}
public string ToString(bool rounded) {
if (rounded) {
return (int)Math.Round(X) + ", " + (int)Math.Round(Y);
} else {
return ToString();
}
}
}
}