-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.java
More file actions
154 lines (124 loc) · 2.64 KB
/
Vector.java
File metadata and controls
154 lines (124 loc) · 2.64 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
public final class Vector//here a 2D vector
{
public double x;
public double y;
public Vector()
{
this(0,0);
}
public Vector(double x, double y)
{
this.x = x;
this.y = y;
}
public Vector(Vector other)
{
x = other.x;
y = other.y;
}
public final void reset()
{
x = 0.0;
y = 0.0;
}
public final void assign(Vector other)
{
x = other.x;
y = other.y;
}
public final double length()
{
return Math.sqrt(x*x + y*y);
}
public final double lengthquad()
{
return x*x + y*y;
}
public final void normalize()
{
double len = length();
//if(len == 0.0) throw new RuntimeException("div by zero");
if(len == 0.0) return;
x /= len;
y /= len;
}
public final String toString()
{
return "("+x+", "+y+")";
}
public final boolean equals(Vector other)
{
return (x == other.x) && (y == other.y);
}
public static final void add(Vector dest, final Vector a, final Vector b)
{
dest.x = a.x + b.x;
dest.y = a.y + b.y;
}
public static final void sub(Vector dest, final Vector a, final Vector b)
{
dest.x = a.x - b.x;
dest.y = a.y - b.y;
}
public static final double dot(final Vector a, final Vector b)
{
return a.x * b.x + a.y * b.y;
}
public static final void mul(Vector dest, final Vector a, final Vector b)
{
dest.x = a.x * b.x;
dest.y = a.y * b.y;
}
public static final void div(Vector dest, final Vector a, final Vector b)
{
if(b.x == 0.0 || b.y == 0.0) throw new RuntimeException("div by zero");
dest.x = a.x / b.x;
dest.y = a.y / b.y;
}
public static final void add_num(Vector dest, final Vector a, final double b)
{
dest.x = a.x + b;
dest.y = a.y + b;
}
public static final void sub_num(Vector dest, final Vector a, final double b)
{
dest.x = a.x - b;
dest.y = a.y - b;
}
public static final void mul_num(Vector dest, final Vector a, final double b)
{
dest.x = a.x * b;
dest.y = a.y * b;
}
public static final void div_num(Vector dest, final Vector a, final double b)
{
//if(b == 0.0) throw new RuntimeException("div by zero");
dest.x = a.x / b;
dest.y = a.y / b;
}
public static final void muladd(Vector dest, final Vector a, final Vector b)
{
dest.x += a.x * b.x;
dest.y += a.y * b.y;
}
public static final void muladd_num(Vector dest, final Vector a, final double b)
{
dest.x += a.x * b;
dest.y += a.y * b;
}
public static final void mulsub(Vector dest, final Vector a, final Vector b)
{
dest.x -= a.x * b.x;
dest.y -= a.y * b.y;
}
public static final void mulsub_num(Vector dest, final Vector a, final double b)
{
dest.x -= a.x * b;
dest.y -= a.y * b;
}
public Vector copy()
{
Vector out = new Vector(this);
return out;
}
}