aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/paulo-custodio/lua/ch-2.lua
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
committer冯昶 <seaker@qq.com>2021-03-15 18:13:51 +0800
commit8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch)
treeae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-101/paulo-custodio/lua/ch-2.lua
parent865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff)
parentc9aec2da6bcb04b488183f09ca94bee488557aff (diff)
downloadperlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.gz
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.bz2
perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.zip
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
Diffstat (limited to 'challenge-101/paulo-custodio/lua/ch-2.lua')
-rw-r--r--challenge-101/paulo-custodio/lua/ch-2.lua46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-101/paulo-custodio/lua/ch-2.lua b/challenge-101/paulo-custodio/lua/ch-2.lua
new file mode 100644
index 0000000000..47a12b657e
--- /dev/null
+++ b/challenge-101/paulo-custodio/lua/ch-2.lua
@@ -0,0 +1,46 @@
+#!/usr/bin/env lua
+
+--[[
+Challenge 101
+
+TASK #2 › Origin-containing Triangle
+Submitted by: Stuart Little
+You are given three points in the plane, as a list of six co-ordinates:
+A=(x1,y1), B=(x2,y2) and C=(x3,y3).
+
+Write a script to find out if the triangle formed by the given three
+co-ordinates contain origin (0,0).
+
+Print 1 if found otherwise 0.
+--]]
+
+function sign(x1,y1,x2,y2,x3,y3)
+ return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)
+end
+
+function point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3)
+ local d1 = sign(xp,yp, x1,y1, x2,y2)
+ local d2 = sign(xp,yp, x2,y2, x3,y3)
+ local d3 = sign(xp,yp, x3,y3, x1,y1)
+
+ local has_neg
+ if (d1 < 0) or (d2 < 0) or (d3 < 0) then
+ has_neg = true
+ else
+ has_neg = false
+ end
+
+ local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0)
+ local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0)
+
+ if has_neg and has_pos then
+ return 0
+ else
+ return 1
+ end
+end
+
+io.write(point_in_triangle(0,0,
+ tonumber(arg[1]), tonumber(arg[2]),
+ tonumber(arg[3]), tonumber(arg[4]),
+ tonumber(arg[5]), tonumber(arg[6])), "\n")