Tipo Punto


TIPO PUNTO

public enum TipoCuadrante {
       PRIMER, SEGUNDO, TERCER, CUARTO, EJES
}

public enum TipoDiana {
       INTERIOR, MEDIO, EXTERIOR
}
 
public interface Punto extends Comparable<Punto>{
       Double getX();
       Double getY();
       void setX(Double nx);
       void setY(Double ny);
       Double getDistanciaOtroPunto(Punto q);
       Double getDistanciaAlOrigen();
       TipoCuadrante getCuadrante();
       TipoDiana getPosicionDiana();
       void moverDireccion(Double nx, Double ny);
}

 
public class PuntoImpl implements Punto {
Double x;
Double y;
static final Punto ORIGEN = new PuntoImpl(0.,0.);
 
public PuntoImpl(Double nx, Double ny){
   x=nx;
   y=ny;
}
public PuntoImpl(String s){
   List<String> lisat = Arrays.asList(s.split(","));
   x=new Double(lisat.get(0));
   y=new Double(lisat.get(1));
}
public Double getX() {
   return x;
}
public Double getY() {
   return y;
}
public void setX(Double nx) {
   x=nx;
}
public void setY(Double ny) {
   y=ny;
}
public String toString(){
   String cadena;
   cadena="("+getX()+","+getY()+")";
   return cadena;
}
public Double   getDistanciaOtroPunto(Punto q){
   Double dx = this.getX()-q.getX();
   Double dy = this.getY()-q.getY();
   return Math.sqrt(dx*dx+dy*dy);
}
public Double getDistanciaAlOrigen(){
   return this.getDistanciaOtroPunto(ORIGEN);
} 
public boolean equals(Object obj){
   boolean res=false;
  
   if (obj instanceof Punto){
          Punto p = (Punto) obj;
           res= this.getX().equals(p.getX()) &&
                this.getY().equals(p.getY());
   }
   return res;
}
public TipoCuadrante getCuadrante(){
   TipoCuadrante t=TipoCuadrante.EJES;
   if (this.getX()>0. && this.getY()>0.){
      t=TipoCuadrante.PRIMER;
   }else if (this.getX()<0. && this.getY()>0.){
            t=TipoCuadrante.SEGUNDO;
   }else if (this.getX()<0. && this.getY()<0.){
            t=TipoCuadrante.TERCER;
   }else if (this.getX()>0. && this.getY()<0.){
            t=TipoCuadrante.CUARTO;
   }
   return t;
}
public TipoDiana getPosicionDiana(){
   TipoDiana t=TipoDiana.EXTERIOR;
        
   if (this.getDistanciaAlOrigen()<4.0){
          t=TipoDiana.INTERIOR;
   }
   else if (this.getDistanciaAlOrigen()<8.0){
          t=TipoDiana.MEDIO;
   }
   return t;
}
public int compareTo(Punto p){
   int res;
 
   res=this.getX().compareTo(p.getX());
   if (res==0){
         res=this.getY().compareTo(p.getY());
   }
   return res;
}
public void moverDireccion(Double nx, Double ny){
   this.setX(this.getX()+nx);
   this.setY(this.getY()+ny);
} 
}

No hay comentarios:

Publicar un comentario