Logo

Programming-Idioms

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

Idiom #179 Get center of a rectangle

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

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)
const center = ({x1, y1, x2, y2}) => ({x: (x1 + x2) / 2, y: (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)})
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,
  };
}
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
		);
	}
}
import std.typecons;
auto c = tuple((x1+x2)/2, (y1+y2)/2);
import "dart:math";
var r = MutableRectangle.fromPoints(Point(x1, y1), Point(x2, y2));
var center = Point(r.width / 2, r.height / 2);
real :: center(2)
center = [(x1 + x2)/2, (y1 + y2)/2]
import "image"
c := image.Pt((x1+x2)/2, (y1+y2)/2)
rectCenter :: Num a => (a, a) -> (a, a) -> (a,a)
rectCenter (x1,y1) (x2,y2) = ((x1+x2)/2,(y1+y2)/2)
double[] c = {(x1+x2)/2,(y1+y2)/2};
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)
class Point
{
	public $x;
	public $y;

	public function __construct(float x, float y)
	{
		$this->x = x;
		$this->y = y;
	}
}

class Rectangle
{
	private $point1;
	private $point2;

	public function __construct(Point $point1, Point $point2)
	{
		$this->point1 = $point1;
		$this->point2 = $point2;
	}

	public function getCenter(): Point
	{
		return new Point(
			($this->point1->x + $this->point2->x) / 2,
			($this->point1->y + $this->point2->y) / 2
		);
	}
}
uses Types;
c := CenterPoint(Rect(x1,y1,x2,y2));
my @c = (($x1 + $x2) / 2, ($y1 + $y2) / 2);
center = ((x1+x2)/2, (y1+y2)/2)
from collections import namedtuple
Point = namedtuple('Point', 'x y')
center = Point((x1+x2)/2, (y1+y2)/2)
Point = Struct.new(:x, :y)

Rect  = Struct.new(:x1, :y1, :x2, :y2) do
  def center  
    Point.new((x1+x2)/2.0, (y1+y2)/2.0)
  end
end

c = Rect.new(0,0,2,5).center
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)
    }
}
c := (x1@y1 corner: x1@y2) center.

New implementation...
< >
Bart