Guest User

lavieri

a guest
Mar 12th, 2009
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.Collection;
  2. import java.util.Collections;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5.  
  6. /**
  7.  *
  8.  * @author Tomaz Lavieri
  9.  */
  10. public class Trilha {
  11.     private final LinkedList<Point3D> pontos = new LinkedList<Point3D>();
  12.  
  13.     public Trilha() {}
  14.  
  15.     public Trilha(Collection<Point3D> pontos) {
  16.         pontos.addAll(pontos);
  17.     }
  18.  
  19.     public double addPonto(long x, long y, long z) {
  20.         return addPonto(new Point3D(x,y,z));
  21.     }
  22.  
  23.     public double addPonto(Point3D point) {
  24.         Point3D start = (pontos.size() > 0) ? pontos.getLast() : point;
  25.         GeometricVector nextDistance = new GeometricVector(start, point);
  26.         pontos.add(point);
  27.         return nextDistance.size();
  28.     }
  29.  
  30.     public double addPontos(Trilha trilha) {
  31.         this.pontos.addAll(trilha.pontos);
  32.         return trilha.distancia();
  33.     }
  34.  
  35.     public double addPontos(Collection<Point3D> pontos) {
  36.         return addPontos(new Trilha(pontos));
  37.     }
  38.    
  39.     public boolean removePrimeiraOcorrencia(Point3D ponto) {
  40.         return pontos.removeFirstOccurrence(ponto);
  41.     }
  42.    
  43.     public boolean removeUltimaOcorrencia(Point3D ponto) {
  44.         return pontos.removeLastOccurrence(ponto);
  45.     }
  46.    
  47.     public boolean removeOcorrenciaN(Point3D ponto, int n) {
  48.         boolean modifie = false;
  49.         if ( n <= Collections.frequency(pontos, ponto) ) {
  50.             synchronized(pontos) {
  51.                 int count = 0;
  52.                 for(Iterator<Point3D> itr = pontos.iterator(); itr.hasNext();) {
  53.                     Point3D p = itr.next();
  54.                     if (ponto.equals(p))
  55.                         count++;
  56.                     if (count == n) {
  57.                         itr.remove();
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         return modifie;
  64.     }
  65.    
  66.     public synchronized boolean removeSubTrilha(Trilha trilha) {
  67.         int start = Collections.indexOfSubList(pontos, trilha.pontos);
  68.         if (start == -1)
  69.             return false;
  70.        
  71.         for (int i = start; i < trilha.pontos.size(); i++)
  72.             pontos.remove(i);
  73.         return true;
  74.     }
  75.  
  76.     public double distancia() {
  77.         if (pontos.size() < 2)
  78.             return 0D;
  79.         double distancia = 0D;
  80.         Point3D start = pontos.getFirst();
  81.         Point3D end;
  82.         for (Point3D p : pontos) {
  83.             end = p;
  84.             distancia += (new GeometricVector(start, end)).size();
  85.             start = p;
  86.         }
  87.         return distancia;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment