struct SF::Vector2(T)

Overview

Utility generic struct for manipulating 2-dimensional vectors

SF::Vector2 is a simple struct that defines a mathematical vector with two coordinates (x and y). It can be used to represent anything that has two dimensions: a size, a point, a velocity, etc.

The generic parameter T is the type of the coordinates. It can be any type that supports arithmetic operations (+, -, /, *) and comparisons (==, !=), for example Int or Float.

You generally don't have to care about the generic form, the most common specializations have special aliases:

See also: SF.vector2f, SF.vector2i.

The SF::Vector2 struct has a small and simple interface, its #x and #y members can be accessed directly and it contains no mathematical function like dot product, cross product, length, etc.

Usage example:

v1 = SF.vector2f(16.5, 24)
v1.x = 18.2_f32
y = v1.y

v2 = v1 * 5
v3 = v1 + v2

different = (v2 != v3)

Note: for 3-dimensional vectors, see SF::Vector3.

Included Modules

Defined in:

system/system.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(x : T, y : T) #

Construct the vector from its coordinates.


[View source]
def self.new #

Default constructor: equivalent to .new(0, 0)


[View source]

Instance Method Detail

def *(n : Number) #

Memberwise multiplication by a scalar


[View source]
def *(other) #

Memberwise multiplication of two vectors


[View source]
def +(other) #

Memberwise addition of two vectors


[View source]
def -(other) #

Memberwise subtraction of two vectors


[View source]
def - #

Memberwise opposite of the vector


[View source]
def /(n : Number) #

Memberwise division by a scalar


[View source]
def ==(other : Vector2) #

Returns true if both corresponding coordinates of two vectors are equal


[View source]
def [](i : Int) : T #

Get a coordinate by its index: 0 is #x, 1 is #y.

Raises IndexError for other indices.


[View source]
def each(&) #

Yields #x, then #y


[View source]
def size : Int32 #

Returns 2


[View source]
def x : T #

The x coordinate


[View source]
def x=(x : T) #

The x coordinate


[View source]
def y : T #

The y coordinate


[View source]
def y=(y : T) #

The y coordinate


[View source]