binibiningtinamoran

Line

Oct 8th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public class Line {
  2.  
  3.     private Point p1, p2;
  4.  
  5.     public Line(Point p1, Point p2) {
  6.         this.p1 = p1;
  7.         this.p2 = p2;
  8.     }
  9.     public Line(Line aLine) {
  10.         this.p1 = aLine.p1;
  11.         this.p2 = aLine.p2;
  12.     }
  13.     public Line() {
  14.         this.p1 = new Point(0,0);
  15.         this.p2 = new Point(0,0);
  16.     }
  17.     public Point getP1() {
  18.         return p1;
  19.     }
  20.     public Point getP2() {
  21.         return p2;
  22.     }
  23.     public void set(Point p1, Point p2) {
  24.         this.p1 = p1;
  25.         this.p2 = p2;
  26.     }
  27.     public double getDistance() {
  28.         double xCoord = Math.pow((p1.getX() - p2.getX()),2);
  29.         double yCoord = Math.pow((p1.getY() - p2.getY()), 2);
  30.  
  31.         return Math.sqrt(xCoord + yCoord);
  32.     }
  33.     public String toString() {
  34.         return String.format("Line ( " + p1.toString() + ", " + p2.toString() + ", distance = %," +
  35.                 ".4f)\n", this.getDistance());
  36.     }
  37.  
  38. }
Add Comment
Please, Sign In to add comment