Logo

Programming-Idioms

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

Idiom #179 Get center of a rectangle

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

data class Point(val x: Double, val y: Double)
data class Rectangle(val x1: Double, val y1: Double, val x2: Double, val y2: Double){
  fun center() = Point( x = (x1 + x2)/2, y =(y1 + y2)/2)
}
fun center(x1: int, y1: int, x2: int, y2: int) = Pair((x1 + x2)/2, (y1 + y2)/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