Logo

Programming-Idioms

New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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);
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)
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})
double[] c = {(x1+x2)/2,(y1+y2)/2};
center = function(x1, y1, x2, y2) return {x = (x1 + x2)/2, y = (y1 
+ y2)/2 end
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
		);
	}
}
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)
    }
}