Skip to content

Commit d04c41a

Browse files
paramatSmallJoker
authored andcommittedDec 21, 2017
Vector functions: Fix vector.direction() function, improve documentation (#6801)
vector.direction() now returns a normalised vector with direction p1 to p2.
1 parent 18b9210 commit d04c41a

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed
 

Diff for: ‎builtin/common/vector.lua

+5-26
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,13 @@ function vector.distance(a, b)
6363
end
6464

6565
function vector.direction(pos1, pos2)
66-
local x_raw = pos2.x - pos1.x
67-
local y_raw = pos2.y - pos1.y
68-
local z_raw = pos2.z - pos1.z
69-
local x_abs = math.abs(x_raw)
70-
local y_abs = math.abs(y_raw)
71-
local z_abs = math.abs(z_raw)
72-
if x_abs >= y_abs and
73-
x_abs >= z_abs then
74-
y_raw = y_raw * (1 / x_abs)
75-
z_raw = z_raw * (1 / x_abs)
76-
x_raw = x_raw / x_abs
77-
end
78-
if y_abs >= x_abs and
79-
y_abs >= z_abs then
80-
x_raw = x_raw * (1 / y_abs)
81-
z_raw = z_raw * (1 / y_abs)
82-
y_raw = y_raw / y_abs
83-
end
84-
if z_abs >= y_abs and
85-
z_abs >= x_abs then
86-
x_raw = x_raw * (1 / z_abs)
87-
y_raw = y_raw * (1 / z_abs)
88-
z_raw = z_raw / z_abs
89-
end
90-
return {x=x_raw, y=y_raw, z=z_raw}
66+
return vector.normalize({
67+
x = pos2.x - pos1.x,
68+
y = pos2.y - pos1.y,
69+
z = pos2.z - pos1.z
70+
})
9171
end
9272

93-
9473
function vector.add(a, b)
9574
if type(b) == "table" then
9675
return {x = a.x + b.x,

Diff for: ‎doc/lua_api.txt

+33-15
Original file line numberDiff line numberDiff line change
@@ -2248,25 +2248,43 @@ The following functions provide escape sequences:
22482248

22492249
Spatial Vectors
22502250
---------------
2251-
* `vector.new(a[, b, c])`: returns a vector:
2251+
For the following functions, `v`, `v1`, `v2` are vectors, `p1`, `p2` are positions:
2252+
2253+
* `vector.new(a[, b, c])`:
2254+
* Returns a vector.
22522255
* A copy of `a` if `a` is a vector.
2253-
* `{x = a, y = b, z = c}`, if all `a, b, c` are defined
2254-
* `vector.direction(p1, p2)`: returns a vector
2255-
* `vector.distance(p1, p2)`: returns a number
2256-
* `vector.length(v)`: returns a number
2257-
* `vector.normalize(v)`: returns a vector
2258-
* `vector.floor(v)`: returns a vector, each dimension rounded down
2259-
* `vector.round(v)`: returns a vector, each dimension rounded to nearest int
2260-
* `vector.apply(v, func)`: returns a vector
2261-
* `vector.equals(v1, v2)`: returns a boolean
2262-
* `vector.sort(v1, v2)`: returns minp, maxp vectors of the cuboid defined by v1 and v2
2256+
* `{x = a, y = b, z = c}`, if all of `a`, `b`, `c` are defined numbers.
2257+
* `vector.direction(p1, p2)`:
2258+
* Returns a vector of length 1 with direction `p1` to `p2`.
2259+
* If `p1` and `p2` are identical, returns `{x = 0, y = 0, z = 0}`.
2260+
* `vector.distance(p1, p2)`:
2261+
* Returns zero or a positive number, the distance between `p1` and `p2`.
2262+
* `vector.length(v)`:
2263+
* Returns zero or a positive number, the length of vector `v`.
2264+
* `vector.normalize(v)`:
2265+
* Returns a vector of length 1 with direction of vector `v`.
2266+
* If `v` has zero length, returns `{x = 0, y = 0, z = 0}`.
2267+
* `vector.floor(v)`:
2268+
* Returns a vector, each dimension rounded down.
2269+
* `vector.round(v)`:
2270+
* Returns a vector, each dimension rounded to nearest integer.
2271+
* `vector.apply(v, func)`:
2272+
* Returns a vector where the function `func` has been applied to each component.
2273+
* `vector.equals(v1, v2)`:
2274+
* Returns a boolean, `true` if the vectors are identical.
2275+
* `vector.sort(v1, v2)`:
2276+
* Returns in order minp, maxp vectors of the cuboid defined by `v1`, `v2`.
22632277

22642278
For the following functions `x` can be either a vector or a number:
22652279

2266-
* `vector.add(v, x)`: returns a vector
2267-
* `vector.subtract(v, x)`: returns a vector
2268-
* `vector.multiply(v, x)`: returns a scaled vector or Schur product
2269-
* `vector.divide(v, x)`: returns a scaled vector or Schur quotient
2280+
* `vector.add(v, x)`:
2281+
* Returns a vector.
2282+
* `vector.subtract(v, x)`:
2283+
* Returns a vector.
2284+
* `vector.multiply(v, x)`:
2285+
* Returns a scaled vector or Schur product.
2286+
* `vector.divide(v, x)`:
2287+
* Returns a scaled vector or Schur quotient.
22702288

22712289
Helper functions
22722290
----------------

3 commit comments

Comments
 (3)

paramat commented on Dec 22, 2017

@paramat
ContributorAuthor

@rubenwardy I was trained by hmmmm to always add a very concise summary phrase at the start of the first line of the commit message. It's useful because you can then scan your eye down the start of the commit messages and see the subject, without reading the whole line.
Looking at commit messages you can see most devs do this.

I can see in this case it's borderline redundant, however 'Vector functions' is still more useful to the eye than the rest of the line, so i think this is fairly reasonable.

rubenwardy commented on Dec 22, 2017

@rubenwardy
Contributor

I disagree on that as you end up with pointless labels and inconsistency, but whatever.

The commit message is still bad as it is vague and overflows. At least remove the duplication, and maybe shorten to "docs".

Builtin Vectors: Fix direction() function, improve documentation

paramat commented on Dec 22, 2017

@paramat
ContributorAuthor

Yes it was a bit duplicated and redundant, and too long, not great. Seems to overflow at 69 characters, it's a pain to count so mistakes happen. 'builtin vectors' is better, but my version is similar to yours so can't be particularly vague =)
Anyway i'll keep your request in mind.

Please sign in to comment.