-
-
Save dcrystalj/72bf57b67bf6ff3f3903 to your computer and use it in GitHub Desktop.
Point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///** | |
// * Created by dcrystalj on 28.4.2014. | |
// */ | |
public class Point { | |
int x,y; | |
public int dot(Point p) { | |
return this.x * p.x + this.y * p.y; | |
} | |
public double length() { | |
return Math.sqrt(x*x + y*y); | |
} | |
public double distance(Point p) { | |
return this.minus(p).length(); | |
} | |
public double angle (Point p, Point prev) { | |
Point start = prev.minus(this).negate(); | |
Point end = p.minus(this); | |
double length = start.length() * end.length(); | |
if (length == 0) | |
return 0; | |
return Math.acos( | |
Math.round( ((double) start.dot(end) / length) *100 )/100.0 | |
); // * 57.29577951308232; //multiply by 180/Math.PI == 57.29577951308232 for easier testing; | |
} | |
private Point negate () { | |
return new Point(this.x * -1, this.y * -1); | |
} | |
private Point minus (Point point) { | |
return new Point(this.x - point.x, this.y-point.y); | |
} | |
/** | |
* Constructor | |
* | |
* @param x | |
* @param y | |
*/ | |
public Point(int x, int y){ | |
this.x = x; | |
this.y = y; | |
} | |
public String toString() { | |
return "x: "+x + " y: "+y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment