-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2D.cs
114 lines (99 loc) · 3.07 KB
/
Vector2D.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
using System;
using System.Numerics;
namespace GeometrySharp
{
public class Vector2D
{
public Vector2D(Point2D p)
{
X = p.X;
Y = p.Y;
Length = Point2D.Zero.DistanceTo(p);
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
public Vector2D(Point2D p1, Point2D p2)
{
X = p2.X - p1.X;
Y = p2.Y - p1.Y;
Length = p1.DistanceTo(p2);
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
public Vector2D(double x1, double y1, double x2, double y2)
{
X = x2 - x1;
Y = y2 - y1;
Length = new Point2D(x1, y1).DistanceTo(new Point2D(x2, y2));
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
public Vector2D(double x, double y)
{
X = x;
Y = y;
Length = Point2D.Zero.DistanceTo(new Point2D(x, y));
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
public Vector2D(Segment2D seg)
{
X = seg.EndPoint.X - seg.StartPoint.X;
Y = seg.EndPoint.Y - seg.StartPoint.Y;
Length = seg.Length;
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
private double x;
private double y;
public double X
{
get { return x; }
set
{
x = value;
Length = Point2D.Zero.DistanceTo(new Point2D(x, Y));
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
}
public double Y
{
get { return y; }
set
{
y = value;
Length = Point2D.Zero.DistanceTo(new Point2D(X, y));
if (Length == 0) IsPoint = true;
else IsPoint = false;
}
}
public bool IsPoint { get; private set; }
public double Length { get; private set; }
public static Vector2D Zero = new Vector2D(0, 0);
public static Vector2D Normalize(Vector2D vec)
{
return new Vector2D(vec.X / vec.Length, vec.Y / vec.Length);
}
public static double Dot(Vector2D v1, Vector2D v2)
{
return v1.X * v2.X + v1.Y * v2.Y;
}
public static bool AreParallel(Vector2D v1, Vector2D v2)
{
double ratio1 = v1.X / v1.Y;
double ratio2 = v2.X / v2.Y;
return Math.Abs(ratio1 - ratio2) <= double.Epsilon;
}
public static double AngleBetweenVectors(Vector2D v1, Vector2D v2)
{
double dotProduct = Dot(v1, v2);
double magnitudesProduct = v1.Length * v2.Length;
return Math.Acos(dotProduct / magnitudesProduct);
}
public static Vector2D operator *(Vector2D vector, double value)
{
return new Vector2D(vector.X * value, vector.Y * value);
}
}
}