This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- Clojure
- C++
- C#
- D
- Dart
- Fortran
- Go
- Haskell
- JS
- JS
- Java
- Java
- Java
- Kotlin
- Kotlin
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
- Smalltalk
(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
);
}
}
auto c = tuple((x1+x2)/2, (y1+y2)/2);
Using a tuple, its elements type is infered from the type of its arguments.
var r = MutableRectangle.fromPoints(Point(x1, y1), Point(x2, y2));
var center = Point(r.width / 2, r.height / 2);
double w = x2 - x1, h = y2 - y1;
Rectangle2D r = new Rectangle2D.Double(x1, y1, w, h);
double c[] = { r.getCenterX(), r.getCenterY() };
int w = x2 - x1, h = y2 - y1;
Rectangle r = new Rectangle(x1, y1, w, h);
double c[] = { r.getCenterX(), r.getCenterY() };
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
);
}
}
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)
}
}