Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. public class Point{
  2.  
  3.     // doubles are floating point numbers too. Specification merely states that
  4.     // the inputs are floating point numbers, hence double is chosen
  5.     // =======================================================================
  6.     double x, y;
  7.     // constructor
  8.     Point(double x,double y){
  9.       this.x = x;
  10.       this.y = y;  
  11.     }
  12.  
  13.     // midPoint method takes in a point relative to current point,
  14.     //  and returns midpoint of the line b/w the two:
  15.     public Point midPoint(Point otherPoint){
  16.         double newX = (this.x + otherPoint.x) / 2;
  17.         double newY = (this.y + otherPoint.y) / 2;
  18.         return new Point(newX, newY);
  19.     }
  20.  
  21.     public double distanceTo(Point otherPoint){
  22.         double dx = -(this.x - otherPoint.x);
  23.         double dy = -(this.y - otherPoint.y);
  24.         return Math.sqrt((dx * dx) + (dy * dy));
  25.     }
  26.     public double angleTo(Point otherPoint){
  27.         // the - factor is to ensure left-side of PQ
  28.         double dx =  otherPoint.x - this.x;
  29.         double dy =  otherPoint.y - this.y;
  30.         return Math.atan2(dy, dx);
  31.     }
  32.  
  33.  
  34.     public Point moveTo(double angle, double d){
  35.         return new Point(this.x + (d * Math.cos(angle)),
  36.                          this.y + (d * Math.sin(angle)));
  37.     }
  38.    @Override
  39.     public boolean equals(Object obj) {
  40.         if(this== obj) {
  41.             return true;
  42.         } else if (obj instanceof Point) {
  43.         Point p = (Point) obj;
  44.         return Math.abs(this.x - p.x) < 1E-15 &&
  45.                 Math.abs(this.y - p.y) < 1E-15;
  46.         } else{
  47.             return false;
  48.         }
  49.     }
  50.  
  51.  
  52.     // toString method:
  53.     @Override
  54.     public String toString(){
  55.         return "point (" + String.format("%.3f", this.x)
  56.                           + ", " + String.format("%.3f", this.y) + ")";
  57.     }
  58.  
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement