/** * Sets the height of this rectangle to a given value. The rectangle will expand or contract, * equally on both sides to reach the given height. * * @param height A value, greater than or equal to 0, to which this rectangle's height will be * set. * @throws IllegalArgumentException If <code>height</code> is less than 0. */ public void setHeight(double height) { // negative height? if (height < 0) throw new IllegalArgumentException("Invalid rectangle height: Cannot " + "be less than 0."); // store all vertices for easy access Vector2D v1 = getVertex(0); Vector2D v2 = getVertex(1); Vector2D v3 = getVertex(2); Vector2D v4 = getVertex(3); // compute the change in height double delta = height - this.height; // find the direction of v1 to v2 Vector2D v1v2 = v2.subtract(v1); // determine changes for vertices Vector2D delta2And3 = new Vector2D(v1v2, delta / 2); Vector2D delta1And4 = delta2And3.inverse(); // apply changes v1 = v1.add(delta1And4); v2 = v2.add(delta2And3); v3 = v3.add(delta2And3); v4 = v4.add(delta1And4); // set new vertices LinkedList<Vector2D> verts = new LinkedList<Vector2D>(); verts.add(v1); verts.add(v2); verts.add(v3); verts.add(v4); setVertices(verts); }
/** * Sets the width of this rectangle to a given value. The rectangle will expand or contract, * equally on both sides to reach the given width. * * @param width A value, greater than or equal to 0, to which this rectangle's width will be set. * @throws IllegalArgumentException If <code>width</code> is less than 0. */ public void setWidth(double width) { // negative width? if (width < 0) throw new IllegalArgumentException("Invalid rectangle width: Cannot " + "be less than 0."); // store all vertices for easy access Vector2D v1 = getVertex(0); Vector2D v2 = getVertex(1); Vector2D v3 = getVertex(2); Vector2D v4 = getVertex(3); // compute the change in width double delta = width - this.width; // find the direction of v2 to v3 Vector2D v2v3 = v3.subtract(v2); // determine changes for vertices Vector2D delta3And4 = new Vector2D(v2v3, delta / 2); Vector2D delta1And2 = delta3And4.inverse(); // apply changes v1 = v1.add(delta1And2); v2 = v2.add(delta1And2); v3 = v3.add(delta3And4); v4 = v4.add(delta3And4); // set new vertices LinkedList<Vector2D> verts = new LinkedList<Vector2D>(); verts.add(v1); verts.add(v2); verts.add(v3); verts.add(v4); setVertices(verts); }