/** Sub clase Rectangulo * @author Nicolas Montoya - Eduardo Toro */ import java.awt.*; import java.lang.Math.*; public class Rectangulo extends Forma { /** * ancho, alto y angulo del Rectangulo */ private double ancho, alto, angulo; /** * Puntos que sirven para ubicar centro del Rectangulo */ private Point a, b ,c; /** * Constructor. Inicializa un nuevo Rectangulo * @param ancho ancho del Rectangulo * @param alto alto del Rectangulo */ public Rectangulo(double ancho, double alto) { this.ancho = ancho; this.alto = alto; c = new Point(); a = new Point(); a.setLocation(ancho/2,-alto/2); b = new Point(); b.setLocation(-ancho/2,-alto/2); this.angulo = Math.atan((this.a.getY()-this.b.getY())/(this.a.getX()-this.b.getX()))*180/Math.PI; if (angulo<0) angulo=180+angulo; } // definicion de metodos public void escalar(Point centro, double factor){ this.c.setLocation(centro.getX()*(1-factor)+factor*c.getX(), centro.getY()*(1-factor)+factor*c.getY()); this.a.setLocation(centro.getX()*(1-factor)+factor*a.getX(), centro.getY()*(1-factor)+factor*a.getY()); this.b.setLocation(centro.getX()*(1-factor)+factor*b.getX(), centro.getY()*(1-factor)+factor*b.getY()); this.ancho=this.ancho*factor; this.alto=this.alto*factor; } 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()); } double k3; double theta3 = Math.atan((c.getY()-centro.getY())/(c.getX()-centro.getX())); if ((c.getX()-centro.getX())>0) k3 = 0; else k3 = 1; if (!(c.equals(centro))){ double d3 = Math.hypot((c.getX()-centro.getX()),(c.getY()-centro.getY())); c.setLocation(d3*Math.cos(theta3 + Math.PI*k3 + deltaAngulo)+centro.getX(), d3*Math.sin(theta3 + Math.PI*k3 + deltaAngulo)+centro.getY()); } angulo = Math.atan((this.a.getY()-this.b.getY())/(this.a.getX()-this.b.getX()))*180/Math.PI; if (angulo<0) angulo=180+angulo; } public void trasladar(double deltaX, double deltaY){ this.c.setLocation(c.getX() + deltaX, c.getY() + deltaY); this.a.setLocation(a.getX() + deltaX, a.getY() + deltaY); this.b.setLocation(b.getX() + deltaX, 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 Rectangulo Rectangulo other = (Rectangulo)otherObject; return c.equals(other.c) && ancho == other.ancho && alto == other.alto && a.equals(other.a) && b.equals(other.b); } /** Clona un Rectangulo @return la referencia de un Rectangulo clonado */ public Rectangulo clone(){ Rectangulo rect = new Rectangulo(this.ancho, this.alto); rect.c.setLocation(this.c.getX(), this.c.getY()); rect.a.setLocation(this.a.getX(), this.a.getY()); rect.b.setLocation(this.b.getX(), this.b.getY()); return rect; } /** Imprime los atributos del Rectangulo @return los atributos del Rectangulo */ public String toString() { return "Este es un Rectangulo: Centro("+ c.getX()+ "," + c.getY()+ "); Ancho=" + this.ancho + "; Alto=" + this.alto + "; Inclinacion = " + angulo + " grados."; } } // fin de la clase Rectangulo