-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cs
86 lines (72 loc) · 1.76 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
using System;
using System.Drawing;
namespace func_rocket
{
public class Vector
{
public Vector(double x, double y)
{
X = x;
Y = y;
}
public readonly double X;
public readonly double Y;
public double Length { get { return Math.Sqrt(X * X + Y * Y); } }
public double Angle { get { return Math.Atan2(Y, X); } }
public static Vector Zero = new Vector(0, 0);
public override string ToString()
{
return string.Format("X: {0}, Y: {1}", X, Y);
}
protected bool Equals(Vector other)
{
return X.Equals(other.X) && Y.Equals(other.Y);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Vector)obj);
}
public override int GetHashCode()
{
unchecked
{
return (X.GetHashCode() * 397) ^ Y.GetHashCode();
}
}
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, double k)
{
return new Vector(a.X * k, a.Y * k);
}
public static Vector operator /(Vector a, double k)
{
return new Vector(a.X / k, a.Y / k);
}
public static Vector operator *(double k, Vector a)
{
return a * k;
}
public static Vector operator +(Vector a, Vector b)
{
return new Vector(a.X + b.X, a.Y + b.Y);
}
public Vector Normalize()
{
return Length > 0 ? this*(1/Length) : this;
}
public Vector Rotate(double angle)
{
return new Vector(X*Math.Cos(angle) - Y*Math.Sin(angle), X*Math.Sin(angle) + Y*Math.Cos(angle));
}
public Vector BoundTo(Size size)
{
return new Vector(Math.Max(0, Math.Min(size.Width, X)), Math.Max(0, Math.Min(size.Height, Y)));
}
}
}