Logo

Programming-Idioms

  • C#
  • Rust

Idiom #179 Get center of a rectangle

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

struct Rectangle {
    x1: f64,
    y1: f64,
    x2: f64,
    y2: f64,
}

impl Rectangle {
    pub fn center(&self) -> (f64, f64) {
	    ((self.x1 + self.x2) / 2.0, (self.y1 + self.y2) / 2.0)
    }
}
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