/** Sub clase Triangulo * @author Nicolas Montoya - Eduardo Toro */ import java.awt.*; import java.lang.Math.*; public class Triangulo extends Forma { /** * Puntos que definen los vertices del Triangulo */ private Point a, b, c; /** * Constructor. Inicializa un nuevo Triangulo * @param a vertice del Triangulo * @param b vertice del Triangulo * @param c vertice del Triangulo */ public Triangulo(Point a, Point b, Point c) { this.a = new Point(); this.a.setLocation(a.getX(),a.getY()); this.b = new Point(); this.b.setLocation(b.getX(),b.getY());; this.c = new Point(); this.c.setLocation(c.getX(),c.getY());; } //definicion de motodos 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()); this.c.setLocation(centro.getX()*(1-factor)+factor*this.c.getX(), centro.getY()*(1-factor)+factor*this.c.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()); } 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()); } } 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); this.c.setLocation(this.c.getX() + deltaX, this.c.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 Triangulo Triangulo other = (Triangulo)otherObject; return (a.equals(other.a)||a.equals(other.b)||a.equals(other.c)) && (b.equals(other.a)||b.equals(other.b)||b.equals(other.c)) && (c.equals(other.a)||c.equals(other.b)||c.equals(other.c)); } /** Clona un Circulo @return la referencia de un Triangulo clonado */ public Triangulo clone(){ Triangulo tria = new Triangulo(this.a, this.b, this.c); return tria; } /** Imprime los atributos del Triangulo @return los atributos del Triangulo */ public String toString() { return "Este es un Triangulo con vertices: A=("+ this.a.getX()+ "," + this.a.getY()+ ")" + "B=("+ this.b.getX()+ "," + this.b.getY()+ ")" + "C=("+ this.c.getX()+ "," + this.c.getY()+ ")."; } } // fin de la clase Triangulo