Created
November 11, 2016 15:04
-
-
Save michaldo/f1a6736c44531dfedc9d9cd6cc6c55ef to your computer and use it in GitHub Desktop.
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
class Point { | |
final int x; | |
final int y; | |
public Point(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public Point scale(double scale) { | |
return new Point((int) (x * scale), (int) (y* scale)); | |
} | |
public Point normalizeVs(Point center) { | |
return new Point(x - center.x, y - center.y); | |
} | |
public int distance(Point to) { | |
return (int) sqrt((x - to.x) * (x -to.x) + (y - to.y) * (y - to.y)); | |
} | |
static int det(Point p1, Point p2) { | |
return p1.x * p2.y - p1.y * p2.x; | |
} | |
@Override | |
public String toString() { | |
return x + "-" + y; | |
} | |
public Point add(Point vector) { | |
return new Point(x + vector.x, y + vector.y); | |
} | |
/** | |
* @param p1 | |
* @param p2 | |
* @param q1 | |
* @param q2 | |
* @return angle in degrees between vector [p1->p2] and [q1->q2] in range (-180, 180) | |
*/ | |
public static float angle(Point p1, Point p2, Point q1, Point q2) { | |
Point w1 = p2.normalizeVs(p1); | |
Point w2 = q2.normalizeVs(q1); | |
float radian = (float) (atan2(w1.y, w1.x) - atan2(w2.y, w2.x)); | |
if (radian < - PI) radian += 2 * PI; | |
return (float) (radian / (PI/180)); | |
} | |
public static float angle(Point p1, Point p2, Point p3) { | |
return angle(p1, p2, p2, p3); | |
} | |
public static float angle(Point p1, Point p2) { | |
Point w1 = p2.normalizeVs(p1); | |
return angle(w1); | |
} | |
/** | |
* @param w1 | |
* @return angle in degrees between vector w1 and axis x in range (-180, 180) | |
*/ | |
public static float angle(Point w1) { | |
float radian = (float) atan2(w1.y, w1.x); | |
if (radian < - PI) radian += 2 * PI; | |
return (float) (radian / (PI/180)); | |
} | |
public static Point rotate(Point p1, Point p2, int angle) { | |
Point w1 = p2.normalizeVs(p1); | |
float radian = (float) toRadians(angle); | |
int x = (int) (w1.x * cos(radian) - w1.y * sin(radian)); | |
int y = (int)(w1.x * sin(radian) + w1.y * cos(radian)) ; | |
return p1.add(new Point(x, y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment