forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
23 lines (19 loc) · 677 Bytes
/
Complex.java
File metadata and controls
23 lines (19 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.awt.Rectangle;
/**
* Example from "My method doesn't return what I expect."
*/
public class Complex {
public static Rectangle intersection(Rectangle a, Rectangle b) {
int x1 = Math.min(a.x, b.x);
int y1 = Math.min(a.y, b.y);
int x2 = Math.max(a.x + a.width, b.x + b.width);
int y2 = Math.max(a.y + a.height, b.y + b.height);
Rectangle rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
return rect;
}
public static void main(String[] args) {
Rectangle x = new Rectangle(0, 0, 10, 10);
Rectangle y = new Rectangle(-5, -5, -5, -5);
System.out.println(intersection(x, y));
}
}