Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

IVec2

struct IVec2 { x: int, y: int }

A 2D vector with int components x and y, for things counted in whole steps such as pixels and grid cells. Create one with ivec2(x, y), IVec2::new(x, y) or a struct literal such as IVec2 { x = 1, y = 2 }.

Vec2.to_ivec2 and IVec2.to_vec2 convert between the two.

Implementations

new

fn new(x: int, y: int) -> IVec2

Creates a vector from its x and y components. A struct literal such as IVec2 { x = 3, y = 4 } does the same.

use std::math::IVec2;

let v = IVec2::new(3, 4);
let y = v.y; // 4

zero

fn zero() -> IVec2

Creates the vector whose components are both 0.

use std::math::IVec2;

let origin = IVec2::zero();
let x = origin.x; // 0

from_index

fn from_index(index: int, width: int) -> IVec2

Creates the position of cell index in a grid that is width cells wide, counting across each row and then down. It’s ivec2(index % width, index ~/ width), and to_index goes the other way.

A width below 1 is a runtime error.

use std::math::IVec2;

let cell = IVec2::from_index(7, 3);
let x = cell.x; // 1
let y = cell.y; // 2

add

fn add(self, other: IVec2) -> IVec2

Returns the sum of the two vectors, adding them component by component. Like int math, a result too large for an int is a runtime error, and that holds for every IVec2 and IVec3 method.

use std::math::IVec2;

let v = IVec2::new(3, 4).add(IVec2::new(1, 1));
let x = v.x; // 4

sub

fn sub(self, other: IVec2) -> IVec2

Returns this vector minus other, component by component. b.sub(a) is the vector that points from a to b.

use std::math::IVec2;

let player = IVec2::new(1, 1);
let enemy = IVec2::new(4, 5);
let offset = enemy.sub(player);
let x = offset.x; // 3

scale

fn scale(self, by: int) -> IVec2

Returns the vector with both components multiplied by by.

use std::math::IVec2;

let cell = IVec2::new(3, 4).scale(8);
let x = cell.x; // 24

dot

fn dot(self, other: IVec2) -> int

Returns the dot product, x * other.x + y * other.y. A result of 0 means the two vectors are perpendicular.

use std::math::IVec2;

let a = IVec2::new(3, 4).dot(IVec2::new(1, 0)); // 3

length_squared

fn length_squared(self) -> int

Returns the vector’s length multiplied by itself. An IVec2 has no length, since that’s rarely a whole number, and comparing squared lengths tells which of two is longer.

use std::math::IVec2;

let n = IVec2::new(3, 4).length_squared(); // 25

distance_squared

fn distance_squared(self, other: IVec2) -> int

Returns the distance between the two points multiplied by itself.

use std::math::IVec2;

let player = IVec2::new(0, 0);
let enemy = IVec2::new(3, 4);
let close = player.distance_squared(enemy) < 6 * 6; // true

to_vec2

fn to_vec2(self) -> Vec2

Returns the vector as a Vec2. Vec2.to_ivec2 goes the other way.

use std::math::IVec2;

let half = IVec2::new(3, 4).to_vec2().scale(0.5);
let x = half.x; // 1.5

to_index

fn to_index(self, width: int) -> int

Returns the number of this cell in a grid that is width cells wide, counting across each row and then down. It’s y * width + x, which lets a grid be one flat array, or a position be kept as a single int. IVec2::from_index goes the other way.

use std::math::ivec2;

let cells = [0, 0, 0, 0, 0, 0, 0, 0, 0];
cells[ivec2(1, 2).to_index(3)] = 5; // cells[7]