Skip to content

Instantly share code, notes, and snippets.

@dcrystalj
Created May 7, 2014 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcrystalj/72bf57b67bf6ff3f3903 to your computer and use it in GitHub Desktop.
Save dcrystalj/72bf57b67bf6ff3f3903 to your computer and use it in GitHub Desktop.
Point
///**
// * 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