-
Notifications
You must be signed in to change notification settings - Fork 54
/
SmallRect.java
80 lines (70 loc) · 2.17 KB
/
SmallRect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/******************************************************************************
* Copyright (c) 2011, 2016 Stephan Schwiebert and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stephan Schwiebert - initial API and implementation
*
*******************************************************************************/
package org.eclipse.gef4.cloudio.internal.ui.util;
import org.eclipse.swt.SWT;
/**
* A custom variation of a rectangle, which stores the required values as short
* instead of int, thus saving some space.
*
* @author sschwieb
*
*/
// TODO: replace with org.eclipse.gef4.geometry.planar.Rectangle
public class SmallRect {
final short x, y, width, height;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + height;
result = prime * result + width;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SmallRect other = (SmallRect) obj;
if (height != other.height)
return false;
if (width != other.width)
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public SmallRect(int x, int y, int width, int height) {
this.x = (short) x;
this.y = (short) y;
this.width = (short) width;
this.height = (short) height;
}
public boolean intersects(SmallRect rect) {
if (rect == null)
SWT.error(SWT.ERROR_NULL_ARGUMENT);
return rect == this || intersects(rect.x, rect.y, rect.width, rect.height);
}
public boolean intersects(final int x, final int y, final int width, final int height) {
return (x < this.x + this.width) && (y < this.y + this.height) && (x + width > this.x) && (y + height > this.y);
}
public String toString() {
return "Rectangle {" + x + ", " + y + ", " + width + ", " + height + "}";
}
}