added mpardemo
[slipway.git] / src / edu / berkeley / slipway / gui / R.java
1 package edu.berkeley.slipway.gui;
2
3 import com.atmel.fpslic.*;
4 import java.awt.geom.*;
5 import static com.atmel.fpslic.FpslicConstants.*;
6
7 public class R {
8
9     private double x1, x2, y1, y2;
10     public R(P p1, P p2) { this(p1.x, p1.y, p2.x, p2.y); }
11     public R(double x1, double y1, double x2, double y2) {
12         this.x1 = x1;
13         this.x2 = x2;
14         this.y1 = y1;
15         this.y2 = y2;
16     }
17
18     public double getWidth() { return Math.abs(x2-x1); }
19     public double getHeight() { return Math.abs(y2-y1); }
20     public double minx() { return Math.min(x1, x2); }
21     public double miny() { return Math.min(y1, y2); }
22     public double maxx() { return Math.max(x1, x2); }
23     public double maxy() { return Math.max(y1, y2); }
24     public double cx() { return (x1+x2)/2; }
25     public double cy() { return (y1+y2)/2; }
26     public double width() { return Math.abs(x2-x1); }
27     public double height() { return Math.abs(y2-y1); }
28
29     public void fill(G g) {
30         g.g.fill(new Rectangle2D.Double(minx(), miny(), width(), height()));
31     }
32     public void draw(G g) {
33         g.line(x1, y1, x1, y2);
34         g.line(x1, y2, x2, y2);
35         g.line(x2, y2, x2, y1);
36         g.line(x2, y1, x1, y1);
37     }
38
39     public boolean contains(P p) {
40         return p.x >= minx() && p.x < maxx() && p.y >= miny() && p.y < maxy();
41     }
42
43     public R plus(double minxplus, double minyplus, double maxxplus, double maxyplus) {
44         return new R(minx()+minxplus,
45                      miny()+minyplus,
46                      maxx()+maxxplus,
47                      maxy()+maxyplus);
48     }
49
50     public P corner(int dir) {
51         switch (dir) {
52             case SW: return new P(minx(), miny());
53             case SE: return new P(maxx(), miny());
54             case NW: return new P(minx(), maxy());
55             case NE: return new P(maxx(), maxy());
56             case NORTH: return new P(cx(), maxy());
57             case SOUTH: return new P(cx(), miny());
58             case WEST:  return new P(minx(), cy());
59             case EAST:  return new P(maxx(), cy());
60             default: return null;
61         }
62     }
63
64     public boolean within(R r) {
65         return minx() >= r.minx()
66             && miny() >= r.miny()
67             && maxx() <= r.maxx()
68             && maxy() <= r.maxy();
69     }
70 }