Logo

Programming-Idioms

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

Idiom #179 Get center of a rectangle

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

template <typename T> struct Point {
  T x = {};
  T y = {};
};
template <typename T> struct Rectangle {
  Point<T> upper_left;
  Point<T> lower_right;
};
template <typename T>
Point<T> get_center(const Rectangle<T>& rect) {
  return {
    .x = (rect.upper_left.x + rect.lower_right.x) / 2,
    .y = (rect.upper_left.y + rect.lower_right.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