Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Point{
- // doubles are floating point numbers too. Specification merely states that
- // the inputs are floating point numbers, hence double is chosen
- // =======================================================================
- double x, y;
- // constructor
- Point(double x,double y){
- this.x = x;
- this.y = y;
- }
- // midPoint method takes in a point relative to current point,
- // and returns midpoint of the line b/w the two:
- public Point midPoint(Point otherPoint){
- double newX = (this.x + otherPoint.x) / 2;
- double newY = (this.y + otherPoint.y) / 2;
- return new Point(newX, newY);
- }
- public double distanceTo(Point otherPoint){
- double dx = -(this.x - otherPoint.x);
- double dy = -(this.y - otherPoint.y);
- return Math.sqrt((dx * dx) + (dy * dy));
- }
- public double angleTo(Point otherPoint){
- // the - factor is to ensure left-side of PQ
- double dx = otherPoint.x - this.x;
- double dy = otherPoint.y - this.y;
- return Math.atan2(dy, dx);
- }
- public Point moveTo(double angle, double d){
- return new Point(this.x + (d * Math.cos(angle)),
- this.y + (d * Math.sin(angle)));
- }
- @Override
- public boolean equals(Object obj) {
- if(this== obj) {
- return true;
- } else if (obj instanceof Point) {
- Point p = (Point) obj;
- return Math.abs(this.x - p.x) < 1E-15 &&
- Math.abs(this.y - p.y) < 1E-15;
- } else{
- return false;
- }
- }
- // toString method:
- @Override
- public String toString(){
- return "point (" + String.format("%.3f", this.x)
- + ", " + String.format("%.3f", this.y) + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement