Advertisement
Lusien_Lashans

Segment

Dec 1st, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package azaza;
  2.  
  3. public class Segment {
  4.     private IVector start, end;
  5.  
  6.     public Segment(IVector start, IVector end) {
  7.         this.start = start;
  8.         this.end = end;
  9.     }
  10.  
  11.     public IVector getStart() {return start;}
  12.  
  13.     public IVector getEnd() {return end;}
  14.  
  15.     public double length() {return this.end.sub(this.start).length();}
  16.  
  17.     public double distanceTo(IVector point) {
  18.         IVector mainSideA = end.sub(start);
  19.         IVector sideB = point.sub(start);
  20.         IVector sideC = point.sub(end);
  21.  
  22.         double mainA = mainSideA.length();
  23.         double B = sideB.length();
  24.         double C = sideC.length();
  25.  
  26.         double p = (mainA+B+C)/2;
  27.  
  28.         double S = Math.sqrt(p*(p-mainA)*(p-B)*(p-C));
  29.  
  30.         double distance = 2*S/mainA;
  31.  
  32.         return distance;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement