/** Sub clase Linea * @author Nicolas Montoya - Eduardo Toro */ import java.awt.*; import java.lang.Math.*; public class Linea extends Forma { /** * Puntos que sirven para establecer una Linea */ private Point a, b; /** * Pendiente y angulo de la Linea */ private double pendiente, angulo; /** * Constructor. Inicializa una nueva Linea * @param a Punto inicial de la Linea * @param b Punto final de la Linea */ public Linea(Point a, Point b) { this.a = new Point(); this.b = new Point(); this.a.setLocation(a.getX(),a.getY()); this.b.setLocation(b.getX(),b.getY()); pendiente= (this.b.getY()-this.a.getY())/(this.b.getX()-this.a.getX()); angulo = Math.atan(pendiente)*180/Math.PI; if (angulo<0) angulo=180+angulo; } // definicion de metodos public void escalar(Point centro, double factor){ this.a.setLocation(centro.getX()*(1-factor)+factor*this.a.getX(), centro.getY()*(1-factor)+factor*this.a.getY()); this.b.setLocation(centro.getX()*(1-factor)+factor*this.b.getX(), centro.getY()*(1-factor)+factor*this.b.getY()); } public void rotar(Point centro, double deltaAngulo){ double k1; double theta1 = Math.atan((a.getY()-centro.getY())/(a.getX()-centro.getX())); if ((a.getX()-centro.getX())>0) k1 = 0; else k1 = 1; if (!(a.equals(centro))){ double d1 = Math.hypot((a.getX()-centro.getX()),(a.getY()-centro.getY())); a.setLocation(d1*Math.cos(theta1 + Math.PI*k1 + deltaAngulo)+centro.getX(), d1*Math.sin(theta1 + Math.PI*k1 + deltaAngulo)+centro.getY()); } double k2; double theta2 = Math.atan((b.getY()-centro.getY())/(b.getX()-centro.getX())); if ((b.getX()-centro.getX())>0) k2 = 0; else k2 = 1; if (!(b.equals(centro))){ double d2 = Math.hypot((b.getX()-centro.getX()),(b.getY()-centro.getY())); b.setLocation(d2*Math.cos(theta2 + Math.PI*k2 + deltaAngulo)+centro.getX(), d2*Math.sin(theta2 + Math.PI*k2 + deltaAngulo)+centro.getY()); } pendiente= (this.b.getY()-this.a.getY())/(this.b.getX()-this.a.getX()); angulo = Math.atan(pendiente)*180/Math.PI; if (angulo<0) angulo=180+angulo; } public void trasladar(double deltaX, double deltaY){ this.a.setLocation(this.a.getX() + deltaX, this.a.getY() + deltaY); this.b.setLocation(this.b.getX() + deltaX, this.b.getY() + deltaY); } /** Compara contenidos @param otherObject otro objeto Forma @return true si el contenido de las formas son iguales y un false si no lo son */ public boolean equals(Object otherObject) { // un test rapido para ver si los objetos son identicos if (this == otherObject) return true; // debe retornar NULL si el parametro explicito es nulo if (otherObject == null) return false; // si no son de la misma clase no pueden ser iguales if (getClass() != otherObject.getClass()) return false; // ahora sabemos que el oterObjet es non-null Linea Linea other = (Linea)otherObject; // return (a.equals(other.a)||a.equals(other.b)) && (b.equals(other.a)||b.equals(other.b)); } /** Clona una Linea @return la referencia de una Linea clonada */ public Linea clone(){ Linea lin = new Linea(this.a, this.b); return lin; } /** Imprime los atributos de la Linea @return los atributos de la Linea */ public String toString() { return "Este es una Linea entre los puntos: A=("+ this.a.getX()+ "," + this.a.getY()+ ") y B=("+ this.b.getX()+ "," + this.b.getY()+ ") e Inclinacion = " + angulo + " grados."; } } // fin de la clase Linea