-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVect.java
More file actions
189 lines (167 loc) · 3.04 KB
/
Vect.java
File metadata and controls
189 lines (167 loc) · 3.04 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.util.*;
public class Vect
{
private double slope;
private double angle;
private double mag;
public double x;
public double y;
public Vect(double a, double b, double c, double d)
{
slope=((double)(d-b))/((double)(c-a));
angle=Math.PI+Math.atan2(d-b,c-a);
mag=Math.sqrt(Math.pow(c-a,2)+Math.pow(d-b,2));
x=Math.cos(angle)*mag;
y=Math.sin(angle)*mag;
}
public Vect(double a, double b)
{
angle=a;
mag=b;
x=Math.cos(angle)*mag;
y=Math.sin(angle)*mag;
}
public Vect(Point a, Point b)
{
x=b.x-a.x;
y=b.y-a.y;
mag=Math.sqrt(x*x+y*y);
angle=Math.atan2(y,x);
slope=y/x;
}
public Vect(Vect in)
{
x=in.x;
y=in.y;
}
public String toString()
{
return "{"+x+","+y+"}";
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getAngle()
{
angle=Math.atan2(y,x);
return angle;
}
public double getMag()
{
mag=Math.sqrt(x*x+y*y);
return mag;
}
public void setX(double a)
{
x=a;
slope=y/x;
angle=Math.atan(slope);
if((slope>0&&y>0)||(slope<0&&y<0))
angle=Math.PI-angle;
mag=Math.sqrt(x*x+y*y);
}
public void setY(double a)
{
y=a;
slope=y/x;
angle=Math.atan(slope);
if((slope>0&&y>0)||(slope<0&&y<0))
angle=Math.PI-angle;
mag=Math.sqrt(x*x+y*y);
}
public void setMag(double m)
{
double r=m/mag;
x=x*r;
y=y*r;
mag=m;
}
public void add(Vect v)
{
x=x+v.getX();
y=y+v.getY();
/* mag=Math.sqrt(x*x+y*y);
slope=x/y;
angle=Math.atan(slope);
if((slope>0&&y<0)||(slope<0&&y>0))
angle=Math.PI-angle;
mag=Math.sqrt(x*x+y*y);*/
}
public void subtract(Vect v)
{
x-=v.x;
y-=v.y;
}
public Vect plus(Vect a)
{
Vect out=this.cloneVect();
out.add(a);
return out;
}
public void multiply(double n)
{
y=y*n;
x=x*n;
mag=Math.sqrt(x*x+y*y);
angle=Math.atan(slope);
if((slope>0&&y<0)||(slope<0&&y>0))
angle=Math.PI-angle;
}
public double getSlope()
{
return slope;
}
public void reverse()
{
x=-x;
y=-y;
}
public double dotProduct(Vect v)
{
return x*v.x+y*v.y;
}
public Vect cloneVect()
{
return new Vect(angle,mag);
}
public Vect multipliedBy(double a)
{
Vect out = this.cloneVect();
out.multiply(a);
return out;
}
public Point getPointFrom(Point p)
{
return new Point(p.x+x,p.y+y);
}
public void normalize()
{
mag=Math.sqrt(x*x+y*y);
x/=mag;
y/=mag;
}
public void rotateVect(double ang)
{
double s=Math.sin(ang),c=Math.cos(ang);
double [][] mat = {{c,-s},{s,c}};
Matrix m1=new Matrix(mat),m2=new Matrix(this),out=m1.multiply(m2);
x=out.data[0][0];
y=out.data[1][0];
// System.out.println(m1+"\n\n"+m2+"\n\n"+out+"\n\n------------------");
}
public void rotateVect(Matrix rot)
{
Matrix out=rot.multiply(new Matrix(this));
x=out.data[0][0];
y=out.data[1][0];
}
}