checkpoint
[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(double x1, double y1, double x2, double y2) {
11         this.x1 = x1;
12         this.x2 = x2;
13         this.y1 = y1;
14         this.y2 = y2;
15     }
16
17     public double getWidth() { return Math.abs(x2-x1); }
18     public double getHeight() { return Math.abs(y2-y1); }
19     public double minx() { return Math.min(x1, x2); }
20     public double miny() { return Math.min(y1, y2); }
21     public double maxx() { return Math.max(x1, x2); }
22     public double maxy() { return Math.max(y1, y2); }
23     public double cx() { return (x1+x2)/2; }
24     public double cy() { return (y1+y2)/2; }
25     public double width() { return Math.abs(x2-x1); }
26     public double height() { return Math.abs(y2-y1); }
27
28     public void fill(G g) {
29         g.g.fill(new Rectangle2D.Double(minx(), miny(), width(), height()));
30     }
31     public void draw(G g) {
32         g.line(x1, y1, x1, y2);
33         g.line(x1, y2, x2, y2);
34         g.line(x2, y2, x2, y1);
35         g.line(x2, y1, x1, y1);
36     }
37
38     public boolean contains(P p) {
39         return p.x >= minx() && p.x < maxx() && p.y >= miny() && p.y < maxy();
40     }
41
42     public R plus(double minxplus, double minyplus, double maxxplus, double maxyplus) {
43         return new R(minx()+minxplus,
44                      miny()+minyplus,
45                      maxx()+maxxplus,
46                      maxy()+maxyplus);
47     }
48
49     public P corner(int dir) {
50         switch (dir) {
51             case SW: return new P(minx(), miny());
52             case SE: return new P(maxx(), miny());
53             case NW: return new P(minx(), maxy());
54             case NE: return new P(maxx(), maxy());
55             case NORTH: return new P(cx(), maxy());
56             case SOUTH: return new P(cx(), miny());
57             case WEST:  return new P(minx(), cy());
58             case EAST:  return new P(maxx(), cy());
59             default: return null;
60         }
61     }
62 }