package uk.ac.liv.comp285.cw1.shapes;
public class Point {
public Point(float x, float y) {
super();
this.x = x;
this.y = y;
}
private float x,y;
/**
* @return the x
*/
public float getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(float x) {
this.x = x;
}
/**
* @return the y
*/
public float getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(float y) {
this.y = y;
}
/**
* Returns a point which is a subtraction of this Point away from another
* @param otherPoint
* @return
*/
Point subtract(Point otherPoint) {
return(new Point(x-otherPoint.x,y-otherPoint.y));
}
/**
* Returns a point which is the addition of this point and another point
* @param otherPoint
* @return
*/
Point add(Point otherPoint) {
return(new Point(x+otherPoint.x,y+otherPoint.y));
}
/**
* Rotates a point around the origin given by an angle in radians and returns the resulting Point
* @param p
* @param origin
* @param angle
* @return
*/
public Point rotate(Point origin,double angle) {
// Optimisations
if (origin==null) return(this); // origin not set so don’t rotate
if (angle==0) return(this); // don’t bother to rotate if angle = 0
// Find point relative to origin
Point relativePoint=subtract(origin);
//First convert point to polar co-ordinate form
// Calculate tangent
double tan=relativePoint.getY()/relativePoint.getX();
// Use arc tangent to calculate angle for polar co-ordinate
double pointAngle=Math.atan(tan);
// Use Pythagoras to calculate the radius
double r=Math.sqrt((Math.pow(relativePoint.getX(),2))+(Math.pow(relativePoint.getY(),2)));
// Add on the rotation angle
pointAngle+=angle;
return((new Point((float)(r*Math.cos(pointAngle)),(float)(r*Math.sin(pointAngle)))).add(origin));
}
}