Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
added function math.line_intersect
Browse files Browse the repository at this point in the history
  • Loading branch information
Youka committed Nov 13, 2014
1 parent 6bdf5e0 commit 8ed745d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ <h1>Yutils<span>An ASS typeset utilities library</span></h1>
Calculates length of given vector.
</div>
<div class="function">
x, y = math.line_intersect(x0, y0, x1, y1, x2, y2, x3, y3, strict)<br>
Calculates intersection point of two lines.<br>
<span class="function_parameters">x0, y0, x1, y1</span> are both points of line 1, <span class="function_parameters">x2, y2, x3, y3</span> are points of line 2. <span class="function_parameters">strict</span> is a flag, determining the intersection has to be located on the lines.<br>
<span class="function_return">x, y</span> can be the intersection point. If both lines are parallel, <span class="function_return">x</span> is <b>nil</b>. If <span class="function_parameters">strict</span> is <b>true</b> and there's no intersection on the strict length lines, <span class="function_return">x</span> is <b>inf</b> (1/0).
</div>
<div class="function">
rx, ry, rz = math.ortho(x1, y1, z1, x2, y2, z2)<br>
Calculates the orthogonal vector to vectors <span class="function_parameters">x1|y1|z1</span> and <span class="function_parameters">x2|y2|z3</span>.
</div>
Expand Down
32 changes: 31 additions & 1 deletion src/Yutils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------------------------------------------
Version: 4th November 2014, 04:00 (GMT+1)
Version: 13th November 2014, 22:20 (GMT+1)
Yutils
table
Expand All @@ -44,6 +44,7 @@
transform(x, y, z[, w]) -> number, number, number, number
degree(x1, y1, z1, x2, y2, z2) -> number
distance(x, y[, z]) -> number
line_intersect(x0, y0, x1, y1, x2, y2, x3, y3, strict) -> number, number|nil|inf
ortho(x1, y1, z1, x2, y2, z2) -> number, number, number
randomsteps(min, max, step) -> number
round(x[, dec]) -> number
Expand Down Expand Up @@ -960,6 +961,35 @@ Yutils = {
-- Calculate length
return z and math.sqrt(x*x + y*y + z*z) or math.sqrt(x*x + y*y)
end,
line_intersect = function(x0, y0, x1, y1, x2, y2, x3, y3, strict)
-- Check arguments
if type(x0) ~= "number" or type(y0) ~= "number" or type(x1) ~= "number" or type(y1) ~= "number" or
type(x2) ~= "number" or type(y2) ~= "number" or type(x3) ~= "number" or type(y3) ~= "number" or
strict ~= nil and type(strict) ~= "boolean" then
error("two lines and optional strictness flag expected", 2)
end
-- Get line vectors & check valid lengths
local x10, y10, x32, y32 = x0 - x1, y0 - y1, x2 - x3, y2 - y3
if x10 == 0 and y10 == 0 or x32 == 0 and y32 == 0 then
error("lines mustn't have zero length", 2)
end
-- Calculate determinant and check for parallel lines
local det = x10 * y32 - y10 * x32
if det ~= 0 then
-- Calculate line intersection (endless line lengths)
local pre, post = (x0 * y1 - y0 * x1), (x2 * y3 - y2 * x3)
local ix, iy = (pre * x32 - x10 * post) / det, (pre * y32 - y10 * post) / det
-- Check for line intersection with given line lengths
if strict then
local s, t = x10 ~= 0 and (ix - x1) / x10 or (iy - y1) / y10, x32 ~= 0 and (ix - x3) / x32 or (iy - y3) / y32
if s < 0 or s > 1 or t < 0 or t > 1 then
return 1/0 -- inf
end
end
-- Return intersection point
return ix, iy
end
end,
-- Get orthogonal vector of 2 given vectors
ortho = function(x1, y1, z1, x2, y2, z2)
-- Check arguments
Expand Down
1 change: 1 addition & 0 deletions tests/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ print("Arc curve: ", Yutils.math.arc_curve(100, 0, 0, 0, 95))
print("Curve point: ", Yutils.math.bezier(0.5, {{0,0},{4,-10},{8,2},{12,0}}))
print("Distance: ", Yutils.math.distance(10, -5, -3))
print("Degree: ", Yutils.math.degree(0,-1,0, -0.1,1,0))
print("Lines intersection: ", Yutils.math.line_intersect(0, 0, 10, 10, 1, 0, 9, 10, true))
print("Orthogonal: ", Yutils.math.ortho(1,0,0, 0,0,1))
local mat = Yutils.math.create_matrix().rotate("z", 90).scale(0.5, 2, 2).translate(10, 20, -5)
print("Transformed point: ", mat.transform(0, 0, 0))
Expand Down

0 comments on commit 8ed745d

Please sign in to comment.