1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10: require_once 'exceptions.php';
11:
12: 13: 14:
15: class Point {
16:
17: 18: 19: 20: 21:
22: private $x;
23:
24: 25: 26: 27: 28:
29: private $y;
30:
31: 32: 33: 34: 35:
36: public function __construct($x, $y) {
37: $this->setX($x);
38: $this->setY($y);
39: }
40:
41: 42: 43: 44: 45: 46: 47:
48: public function offset(Point $p) {
49: $this->setX($this->getX() + $p->getX());
50: $this->setY($this->getY() + $p->getY());
51: return $this;
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61: 62:
63: public static function add(Point $p, $obj) {
64: if ($obj instanceof Point) {
65: return new Point($p->getX() + $obj->getX(), $p->getY() + $obj->getY());
66: } else if ($obj instanceof Size) {
67: return new Point($p->getX() + $obj->getWidth(), $p->getY() + $obj->getHeight());
68: }
69: throw new IllegalArgumentException();
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80:
81: public static function subtract(Point $p, $obj) {
82: if ($obj instanceof Point) {
83: return self::add($p, new Point(-$obj->getX(), -$obj->getY()));
84: } else if ($obj instanceof Size) {
85: return self::add($p, new Point(-$obj->getWidth(), -$obj->getHeight()));
86: }
87: throw new IllegalArgumentException();
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function equals(Point $p) {
97: return $this->getX() == $p->getX() && $this->getY() == $p->getY();
98: }
99:
100: public function getX() {
101: return $this->x;
102: }
103:
104: public function getY() {
105: return $this->y;
106: }
107:
108: 109: 110: 111:
112:
113: public function setX($x) {
114: if (is_integer($x)) {
115: $this->x = $x;
116: } else {
117: throw new IllegalArgumentException();
118: }
119: }
120:
121: 122: 123: 124:
125:
126: public function setY($y) {
127: if (is_integer($y)) {
128: $this->y = $y;
129: } else {
130: throw new IllegalArgumentException();
131: }
132: }
133:
134: public function __toString() {
135: return "{x: {$this->x}, y: {$this->y}}";
136: }
137:
138: }
139:
140: ?>