1<?php
2
3/**
4 * Immutable class representing a 2D point in canvas coordinates
5 */
6class Point {
7    public function __construct($x, $y) {
8        $this->x = $x;
9        $this->y = $y;
10    }
11
12    public function getX() {
13        return $this->x;
14    }
15
16    public function getY() {
17        return $this->y;
18    }
19
20    public function distanceFrom(Point $other) {
21        return sqrt(
22            (($other->x - $this->x) * ($other->x - $this->x))
23                + (($other->y - $this->y) * ($other->y - $this->y))
24        );
25    }
26
27    /**
28     * Return a new Point instance that has been incremented by the
29     * specified X and Y values
30     */
31    public function addIncrement($x, $y) {
32        return new Point($this->x + $x, $this->Y + $y);
33    }
34
35    public function __toString() {
36        return sprintf("Point<%d, %d>", $this->x, $this->y);
37    }
38
39    private $x;
40    private $y;
41}