Math for Game Developers - Character Movement 3 (Vector Length)
package learn.mathforgame.VectorLength;
public class Main {
public static void main(String[] args) {
Point p = new Point(0, -1);
Point i = new Point(1, 1);
Vector v = new Vector().subtractPoints(p, i);
System.out.println("Result: "+v.length(v));
}
}
Point class:
package learn.mathforgame.VectorLength;
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Vector class:
package learn.mathforgame.VectorLength;
public class Vector {
private int x, y;
public Vector() {
}
public Vector(int x, int y) {
this.x = x;
this.y = y;
}
public Vector subtractPoints(Point p, Point i) {
return new Vector(p.getX() - i.getX(), p.getY()-i.getY());
}
public float length(Vector v) {
return (float) Math.sqrt((v.x *v.x) + (v.y*v.y));
}
}
No comments:
Post a Comment