Logo

Programming-Idioms

  • VB
  • C#

Idiom #179 Get center of a rectangle

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

class Point
{
	public float X { get; }
	public float Y { get; }

	public Point(float x, float y)
	{
		X = x;
		Y = y;
	}
}

class Rectangle
{
	Point Point1;
	Point Point2;

	public Rectangle(Point point1, Point point2)
	{
		Point1 = point1;
		Point2 = point2;
	}

	public Point GetCenter()
	{
		return new Point(
		(Point1.X + Point2.X) / 2,
		(Point1.Y + 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