Logo

Programming-Idioms

Declare a container type for two floating-point numbers x and y
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

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

Other implementations
type Point is
   record
      X : Float;
      Y : Float;
   end record;
typedef struct {
  double x;
  double y;
} Point;
type point = {
	x: float;
	y: float;
}
(vector-of :double x y)
[x y]
namespace point {
    double x, y;
}
template <typename T>
struct point { T x, y; };
template <typename T>
class point {
    public:
        T x, y;
        point(T x, T y) {
            this->x = x;
            this->y = y;
        }
};
struct Point{
  double x;
  double y;
};
(double x, double y)
record Point(double x, double y);
struct Point
{
    public double x;
    public double y;
};
struct Point{
    double x;
    double y;
}

    
class Point
{
    float x;
    float y;
}
class Point {
  double x, y;
}
class Point {
  double x, y;
  Point(this.x, this.y);
}
{x, y}
defmodule Point do
  defstruct x: 0.0, y: 0.0
end
p = [ x: 1.122, y: 7.45 ]
-module(points).
-export([new/2, x/1, y/1]).

-opaque point() :: #{x => float(), y => float()}.
-export_type([point/0]).

-spec new(float(), float()) -> point().
new(X, Y) -> #{x => X, y => Y}.

-spec x(point()) -> float().
x(#{x := X}) -> X.

-spec y(point()) -> float().
y(#{y := Y}) -> Y.
PointAsAMap = #{x => X, y => Y}.
PointAsATuple = {X, Y}.
module x
  type point
  real :: x, y
  end type point
end module x
type point
  real :: x
  real :: y
end type point
type Point struct {
    x, y float64
}
class Point{
  double x
  double y
}
data Point = Point
  { x :: Double
  , y :: Double
  } deriving (Eq, Ord, Show, Read)
data Point = Point 
  { x :: Double
  , y :: Double 
  }
const point = { x: 1, y: 2 };
var p = { x: 1.122, y: 7.45 };
class Point{
  double x;
  double y;
}
class Point { double x, y; }
public record Point(double x, double y) { }
class Point<T extends Number> {
    T x, y;
    Point(T x, T y) {
        this.x = x;
        this.y = y;
    }
}
data class Point(val x: Float, val y: Float)
(defstruct point
  (x 0.0d0 :type double-float)
  (y 0.0d0 :type double-float))
point = { x = 123, y = 456 }
typedef struct {
  double x,y;
} NSPoint;
class Point
{
    /** @var float */
    public $x = 0.0;

    /** @var float */
    public $y = 0.0;
}
Type
  TPoint = Record
    x : Double;
    y : Double;
  End;
my $point = [ 1.5, 6.3 ];
my $point = { x => 1, y => 2 };
from decimal import Decimal
from typing import Literal
type X = Literal['x', 'X']
type Y = Literal['y', 'Y']
type K = X | Y
type V = float | Decimal
type Point = dict[K, V]
p: Point = {
    'x': 1.2,
    'y': Decimal('3.4')
}
from collections import namedtuple
Point = namedtuple("Point", "x y")
from typing import TypedDict
class P[T: float](TypedDict):
    x: T
    y: T
point: P = {
    'x': 1.2,
    'y': 3.4
}
from typing import NamedTuple
class Point[T: float](NamedTuple):
    x: T
    y: T
p = Point(1.2, 3.4)
from dataclasses import dataclass
@dataclass
class Point:
    x: float
    y: float
point = {'x': 1.2, 'y': 3.4}
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
point = dict(x=1.2, y=3.4)
from ctypes import c_double, Structure
class Point(Structure):
    _fields_ = [
        ('x', c_double),
        ('y', c_double)
    ]
p = Point(1.2, 3.4)
class Point(dict):
    def __init__(self, *args):
        m = zip('xy', args)
        super().__init__(m)
p = Point(1.2, 3.4)
class Point(tuple):
    def __new__(cls, *args):
        m = zip('xy', args)
        return super().__new__(cls, m)
p = Point(1.2, 3.4)
class Point[T: float]:
    def __init__(self, *args: T):
        self.x, self.y = args
p = Point(1.2, 3.4)
Point = Struct.new(:x, :y)
struct Point {
    x: f64,
    y: f64,
}
struct Point(f64, f64);
case class Point(x: Float, y: Float)
(define (make-point x y)
  (cons x y))
(define (point-x p)
  (car p))
(define (point-y p)
  (cdr p))
Object subclass: #Point
	instanceVariableNames: 'x y'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Primitives'
Structure Point
    Public x As Double
    Public y As Double
End Structure