hump.vector-light¶

vector = require "hump.vector-light"


An table-free version of hump.vector. Instead of a vector type, hump.vector-light provides functions that operate on numbers.

Note

Using this module instead of hump.vector may result in faster code, but does so at the expense of speed of development and code readability. Unless you are absolutely sure that your code is significantly slowed down by hump.vector, I recommend using it instead.

Example:

function player:update(dt)
local dx,dy = 0,0
if love.keyboard.isDown('left') then
dx = -1
elseif love.keyboard.isDown('right') then
dx =  1
end
if love.keyboard.isDown('up') then
dy = -1
elseif love.keyboard.isDown('down') then
dy =  1
end
dx,dy = vector.normalize(dx, dy)

vector.mul(dy, dx, dy))

if vector.len(player.velx, player.vely) > player.max_velocity then
player.velx, player.vely = vector.mul(player.max_velocity,
vector.normalize(player.velx, player.vely)
end

player.x = player.x + dt * player.velx
player.y = player.y + dt * player.vely
end


Function Reference¶

vector.str(x, y)
Arguments: x,y (numbers) – The vector. The string representation.

Produce a human-readable string of the form (x,y). Useful for debugging.

Example:

print(vector.str(love.mouse.getPosition()))

vector.fromPolar(angle, radius)
Arguments: angle (number) – Angle of the vector in radians. radius (number) – Length of the vector (optional, default = 1). x, y: The vector in cartesian coordinates.

Convert polar coordinates to cartesian coordinates. The angle is measured against the vector (1,0), i.e., the x axis.

Examples:

x,y = vector.polar(math.pi,10)

vector.toPolar(x, y)
Arguments: x,y (numbers) – A vector. angle, radius: The vector in polar coordinates.

Convert the vector to polar coordinates, i.e., the angle and the radius/lenth.

Example:

-- complex multiplication
phase1, abs1 = vector.toPolar(re1, im1)
phase2, abs2 = vector.toPolar(re2, im2)

vector.fromPolar(phase1+phase2, abs1*abs2)

vector.randomDirection(len_min, len_max)
Arguments: len_min (number) – Minimum length of the vector (optional, default = 1). len_max (number) – Maximum length of the vector (optional, default = len_min). x, y: A vector pointing in a random direction with a random length between len_min and len_max.

Sample a vector with random direction and (optional) length.

Examples:

x,y = vector.randomDirection()    -- length is 1
x,y = vector.randomDirection(1,5) -- length is a random value between 1 and 5
x,y = vector.randomDirection(100) -- length is 100

vector.mul(s, x, y)
Arguments: s (number) – A scalar. x,y (numbers) – A vector. x*s, y*s.

Computes x*s,y*s. The order of arguments is chosen so that it’s possible to chain operations (see example).

Example:

velx,vely = vec.mul(dt, vec.add(velx,vely, accx,accy))

vector.div(s, x, y)
Arguments: s (number) – A scalar. x,y (numbers) – A vector. x/s, y/s.

Computes x/s,y/s. The order of arguments is chosen so that it’s possible to chain operations (see example).

Example:

x,y = vec.div(self.zoom, vec.sub(x,y, w/2,h/2))
x,y = vec.div(self.zoom, x-w/2, y-h/2)

vector.idiv(s, x, y)
Arguments: s (number) – A scalar. x,y (numbers) – A vector. x//s, y//s.

Computes integer division x//s,y//s (only Lua 5.3 and up). The order of arguments is chosen so that it’s possible to chain operations (see example).

Example:

i,k = vec.idiv(grid.cellsize, x,y)
i,k = vec.idiv(grid.cellsize, love.mouse.getPosition())

vector.add(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1+x2, x1+x2.

Computes the sum $$(x1+x2, y1+y2)$$ of two vectors. Meant to be used in conjunction with other functions like vector.mul().

Example:

player.x,player.y = vector.add(player.x,player.y, vector.mul(dt, dx,dy))

vector.sub(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1-x2, x1-x2.

Computes the difference $$(x1-x2, y1-y2)$$ of two vectors. Meant to be used in conjunction with other functions like vector.mul().

Example:

dx,dy = vector.sub(400,300, love.mouse.getPosition())

vector.permul(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1*x2, y1*y2.

Component-wise multiplication, i.e.: x1*x2, y1*y2.

Example:

x,y = vector.permul(x,y, 1,1.5)

vector.dot(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1*x2 + y1*y2.

Computes the dot product of two vectors: x1*x2 + y1*y2.

Example:

cosphi = vector.dot(rx,ry, vx,vy)

vector.cross(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1*y2 - y1*x2.

Computes the cross product of two vectors: x1*y2 - y1*x2.

Example:

parallelogram_area = vector.cross(ax,ay, bx,by)

vector.vector.det(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1*y2 - y1*x2.

Alias to vector.cross().

Example:

parallelogram_area = vector.det(ax,ay, bx,by)

vector.eq(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1 == x2 and y1 == y2

Test for equality.

Example:

if vector.eq(x1,y1, x2,y2) then be.happy() end

vector.le(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1 <= x2 and y1 <= y2.

Test for partial lexicographical order, <=.

Example:

if vector.le(x1,y1, x2,y2) then be.happy() end

vector.lt(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. x1 < x2 or (x1 == x2) and y1 <= y2.

Test for strict lexicographical order, <.

Example:

if vector.lt(x1,y1, x2,y2) then be.happy() end

vector.len(x, y)
Arguments: x,y (numbers) – The vector. Length of the vector.

Get length of a vector, i.e. math.sqrt(x*x + y*y).

Example:

distance = vector.len(love.mouse.getPosition())

vector.len2(x, y)
Arguments: x,y (numbers) – The vector. Squared length of the vector.

Get squared length of a vector, i.e. x*x + y*y.

Example:

-- get closest vertex to a given vector
closest, dsq = vertices, vector.len2(px-vertices.x, py-vertices.y)
for i = 2,#vertices do
local temp = vector.len2(px-vertices[i].x, py-vertices[i].y)
if temp < dsq then
closest, dsq = vertices[i], temp
end
end

vector.dist(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. The distance of the points.

Get distance of two points. The same as vector.len(x1-x2, y1-y2).

Example:

-- get closest vertex to a given vector
-- slightly slower than the example using len2()
closest, dist = vertices, vector.dist(px,py, vertices.x,vertices.y)
for i = 2,#vertices do
local temp = vector.dist(px,py, vertices[i].x,vertices[i].y)
if temp < dist then
closest, dist = vertices[i], temp
end
end

vector.dist2(x1, y1, x2, y2)
Arguments: x1,y1 (numbers) – First vector. x2,y2 (numbers) – Second vector. The squared distance of two points.

Get squared distance of two points. The same as vector.len2(x1-x2, y1-y2).

Example:

-- get closest vertex to a given vector
closest, dsq = vertices, vector.dist2(px,py, vertices.x,vertices.y)
for i = 2,#vertices do
local temp = vector.dist2(px,py, vertices[i].x,vertices[i].y)
if temp < dsq then
closest, dsq = vertices[i], temp
end
end

vector.normalize(x, y)
Arguments: x,y (numbers) – The vector. Vector with same direction as the input vector, but length 1.

Get normalized vector, i.e. a vector with the same direction as the input vector, but with length 1.

Example:

dx,dy = vector.normalize(vx,vy)

vector.rotate(phi, x, y)
Arguments: phi (number) – Rotation angle in radians. x,y (numbers) – The vector. The rotated vector

Get a rotated vector.

Example:

-- approximate a circle
circle = {}
for i = 1,30 do
local phi = 2 * math.pi * i / 30
circle[i*2-1], circle[i*2] = vector.rotate(phi, 0,1)
end

vector.perpendicular(x, y)
Arguments: x,y (numbers) – The vector. A vector perpendicular to the input vector

Quick rotation by 90°. The same (but faster) as vector.rotate(math.pi/2, x,y).

Example:

nx,ny = vector.normalize(vector.perpendicular(bx-ax, by-ay))

vector.project(x, y, u, v)
Arguments: x,y (numbers) – The vector to project. u,v (numbers) – The vector to project onto. The projected vector.

Project vector onto another vector.

Example:

vx_p,vy_p = vector.project(vx,vy, ax,ay)

vector.mirror(x, y, u, v)
Arguments: x,y (numbers) – The vector to mirror. u,v (numbers) – The vector defining the axis. The mirrored vector.

Mirrors vector on the axis defined by the other vector.

Example:

vx,vy = vector.mirror(vx,vy, surface.x,surface.y)

vector.angleTo(ox, y, u, v)
Arguments: x,y (numbers) – Vector to measure the angle. u,v (optional) (numbers) – Reference vector. Angle in radians.

Measures the angle between two vectors. u and v default to 0 if omitted, i.e. the function returns the angle to the coordinate system.

Example:

lean = vector.angleTo(self.upx, self.upy, 0,1)
if lean > .1 then self:fallOver() end

vector.trim(max_length, x, y)
Arguments: max_length (number) – Maximum allowed length of the vector. x,y (numbers) – Vector to trim. The trimmed vector.

Trim the vector to max_length, i.e. return a vector that points in the same direction as the source vector, but has a magnitude smaller or equal to max_length.

Example:

vel_x, vel_y = vector.trim(299792458,