Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Rectangle.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.geometry;
6
7 import static java.lang.Math.abs;
8 import static java.lang.Math.min;
9
10 /**
11  * Rectangle class.
12  */
13 public class Rectangle {
14
15     /**
16      * Rectangle points.
17      */
18     public Point2D p1, p2;
19
20     /**
21      * Creates new rectangle with given size.
22      * The rectangle will be centered at the origin.
23      * The rectangle will be square.
24      *
25      * @param size The size of the rectangle.
26      */
27     public Rectangle(final double size) {
28         p2 = new Point2D(size / 2, size / 2);
29         p1 = p2.clone().invert();
30     }
31
32     /**
33      * @param p1 The first point of the rectangle.
34      * @param p2 The second point of the rectangle.
35      */
36     public Rectangle(final Point2D p1, final Point2D p2) {
37         this.p1 = p1;
38         this.p2 = p2;
39     }
40
41     public double getHeight() {
42         return abs(p1.y - p2.y);
43     }
44
45     /**
46      * @return The leftmost x coordinate of the rectangle.
47      */
48     public double getLowerX() {
49         return min(p1.x, p2.x);
50     }
51
52     public double getLowerY() {
53         return min(p1.y, p2.y);
54     }
55
56     /**
57      * @return rectangle width.
58      */
59     public double getWidth() {
60         return abs(p1.x - p2.x);
61     }
62
63 }