Beatmup Java package
IntRectangle.java
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 package Beatmup.Geometry;
20 
21 /**
22  * Integer-valued rectangle
23  */
24 public class IntRectangle {
25  public int x1, y1, x2, y2;
26 
27  public IntRectangle() {
28  x1 = y1 = x2 = y2 = 0;
29  }
30 
31  public IntRectangle(int centerX, int centerY, int size) {
32  x1 = centerX - size/2;
33  y1 = centerY - size/2;
34  x2 = centerX + size/2;
35  y2 = centerY + size/2;
36  }
37 
38  public IntRectangle(int x1, int y1, int x2, int y2) {
39  this.x1 = x1;
40  this.y1 = y1;
41  this.x2 = x2;
42  this.y2 = y2;
43  }
44 
45  public int getWidth() {
46  return x2-x1;
47  }
48 
49  public int getHeight() {
50  return y2-y1;
51  }
52 
53  /**
54  * Expands the rectangle so that it have a given point inside or on the boundary
55  * @param x horizontal coordinate of the given point
56  * @param y horizontal coordinate of the given point
57  */
58  public void expandTo(int x, int y) {
59  if (x < x1)
60  x1 = x;
61  if (x2 < x)
62  x2 = x;
63  if (y < y1)
64  y1 = y;
65  if (y2 < y)
66  y2 = y;
67  }
68 
69 
70  /**
71  * Expands / contracts the rectangle
72  * @param border expansion size (if negative, contraction)
73  */
74  public void expand(int border) {
75  x1 -= border;
76  y1 -= border;
77  x2 += border;
78  y2 += border;
79  }
80 
81  public IntRectangle scale(int x, int y) {
82  return new IntRectangle(x1*x, y1*y, x2*x, y2*y);
83  }
84 
85  public Rectangle scale(float x, float y) {
86  return new Rectangle(x1*x, y1*y, x2*x, y2*y);
87  }
88 
89  /**
90  * Flips rectangle corners so that its size become positive
91  */
92  public void makeReal() {
93  if (x2 < x1) {
94  int swap = x1;
95  x1 = x2;
96  x2 = swap;
97  }
98  if (y2 < y1) {
99  int swap = y1;
100  y1 = y2;
101  y2 = swap;
102  }
103  }
104 
105 
106  public static float fit(int innerWidth, int innerHeight, int outerWidth, int outerHeight) {
107  return innerWidth * outerHeight > innerHeight * outerWidth ?
108  (float) outerWidth / innerWidth : (float) outerHeight / innerHeight;
109  }
110 
111 
112  @Override
113  public String toString() {
114  return "((" + Integer.toString(x1) + ", " + Integer.toString(y1) + "), (" + Integer.toString(x2) + ", " + Integer.toString(y2) + "))";
115  }
116 }
Integer-valued rectangle.
void expand(int border)
Expands / contracts the rectangle.
void makeReal()
Flips rectangle corners so that its size become positive.
void expandTo(int x, int y)
Expands the rectangle so that it have a given point inside or on the boundary.