-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathVecMath.java
54 lines (40 loc) · 1.2 KB
/
VecMath.java
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
package cadcore;
import java.awt.geom.Point2D;
public class VecMath {
//Utility vector functions
public static double getVecLength(double x0, double y0, double x1, double y1)
{
double dx = x0-x1;
double dy = y0-y1;
return Math.sqrt((dx*dx)+(dy*dy));
}
static double getVecLength(Point2D.Double p0, Point2D.Double p1)
{
return getVecLength(p0.x, p0.y, p1.x, p1.y);
}
public static double getVecLength(Point2D.Double p0)
{
return getVecLength(p0.x, p0.y, 0, 0);
}
public static void subVector(Point2D.Double p0, Point2D.Double p1, Point2D.Double ret)
{
ret.setLocation(p1.x-p0.x, p1.y-p0.y);
}
public static void addVector(Point2D.Double p0, Point2D.Double p1, Point2D.Double ret)
{
ret.setLocation(p1.x+p0.x, p1.y+p0.y);
}
public static void scaleVector(Point2D.Double p0, double v)
{
p0.setLocation(p0.x*v,p0.y*v);
}
static double getVecDot(Point2D.Double p0, Point2D.Double p1)
{
return (p0.x*p1.x + p0.y*p1.y);
}
public static double getVecAngle(Point2D.Double p0, Point2D.Double p1)
{
double angle = Math.acos(getVecDot(p0,p1)/(getVecLength(p0)*getVecLength(p1)));
return Double.isNaN(angle)?0:angle;
}
}