Logo

Programming-Idioms

  • Pascal
  • Js

Idiom #179 Get center of a rectangle

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

const center = ({x1, y1, x2, y2}) => ({x: (x1 + x2) / 2, y: (y1 + y2) / 2})
class Point {
  constructor (x, y) {
    this.x = x
    this.y = y
  }
}
const center = ({x1, y1, x2, y2}) => new Point ((x1 + x2) / 2, (y1 + y2) / 2)
uses Types;
c := CenterPoint(Rect(x1,y1,x2,y2));
(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