Guest User

lavieri

a guest
Mar 12th, 2009
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.util.Collection;
  2. import java.util.Iterator;
  3.  
  4. /**
  5.  *
  6.  * @author Tomaz
  7.  */
  8. public class GeometricVector {
  9.  
  10.     public static GeometricVector valueOf(Point3D start, Point3D end) {
  11.         return new GeometricVector(start, end);
  12.     }
  13.  
  14.     public static double length(Collection<Point3D> pontos) {
  15.         if (pontos.size() < 2)
  16.             return 0D;
  17.         double distancia = 0D;
  18.         Iterator<Point3D> itr = pontos.iterator();
  19.         Point3D start = itr.next();
  20.         Point3D end;
  21.         for (; itr.hasNext() ;) {
  22.             end = itr.next();
  23.             distancia += valueOf(start, end).length();
  24.             start = end;
  25.         }
  26.         return distancia;
  27.     }
  28.  
  29.     private final Point3D start;
  30.     private final Point3D end;
  31.  
  32.     public GeometricVector(Point3D start, Point3D end) {
  33.         this.start = start;
  34.         this.end = end;
  35.     }
  36.  
  37.     public Point3D getStart() {
  38.         return start;
  39.     }
  40.  
  41.     public Point3D getEnd() {
  42.         return end;
  43.     }
  44.  
  45.     public double length() {
  46.         double xDeltaPow2 = Math.pow(
  47.                 end.getX().doubleValue()-start.getX().doubleValue(), 2);
  48.         double yDeltaPow2 = Math.pow(
  49.                 end.getY().doubleValue()-start.getY().doubleValue(), 2);
  50.         double zDeltaPow2 = Math.pow(
  51.                 end.getZ().doubleValue()-start.getZ().doubleValue(), 2);
  52.        
  53.         return Math.sqrt(xDeltaPow2 + yDeltaPow2 + zDeltaPow2);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment