Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Php

Idiom #179 Get center of a rectangle

Return the center c of the rectangle with coördinates(x1,y1,x2,y2)

class Point
{
	public $x;
	public $y;

	public function __construct(float x, float y)
	{
		$this->x = x;
		$this->y = y;
	}
}

class Rectangle
{
	private $point1;
	private $point2;

	public function __construct(Point $point1, Point $point2)
	{
		$this->point1 = $point1;
		$this->point2 = $point2;
	}

	public function getCenter(): Point
	{
		return new Point(
			($this->point1->x + $this->point2->x) / 2,
			($this->point1->y + $this->point2->y) / 2
		);
	}
}
(defn c 
  "calculates the center c of a rectangle of coordinates(x1, y1, x2, y2)" 
  [x1 y1 x2 y2]
  {:x (/ (+ x1 x2) 2)
   :y (/ (+ y1 y2) 2)})

New implementation...
< >
Bart