-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToText.java
56 lines (44 loc) · 1.4 KB
/
ToText.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
import tester.*;
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
boolean belowLeftOf(Point p) { return this.x < p.x && this.y < p.y; }
boolean aboveRightOf(Point p) { return this.x > p.x && this.y > p.y; }
double distance(Point p) {
int dx = p.x - this.x;
int dy = p.y - this.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
interface Region {
boolean contains(Point p);
}
class RectRegion implements Region {
Point lowerLeft, upperRight;
RectRegion(Point lowerL, Point upperR) {
this.lowerLeft = lowerL;
this.upperRight = upperR;
}
public boolean contains(Point p) { return this.lowerLeft.belowLeftOf(p) && this.upperRight.aboveRightOf(p); }
}
class CircleRegion implements Region {
Point center;
int radius;
CircleRegion(Point center, int radius) { this.center = center; this.radius = radius; }
public boolean contains(Point p) { return this.center.distance(p) < this.radius; }
}
class UnionRegion implements Region {
Region r1, r2;
UnionRegion(Region r1, Region r2) { this.r1 = r1; this.r2 = r2; }
public boolean contains(Point p) { return this.r1.contains(p) || this.r2.contains(p); }
}
class IntersectRegion implements Region {
Region r1, r2;
IntersectRegion(Region r1, Region r2) {
this.r1 = r1;
this.r2 = r2;
}
public boolean contains(Point p) {
return this.r1.contains(p) && this.r2.contains(p);
}
}