Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • 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)
    }
}
(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