aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/abigail/node
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-24 15:34:17 +0100
committerAbigail <abigail@abigail.be>2021-02-24 15:34:17 +0100
commit67e66456e31b2dcbec30d2b88b0fa297180b6780 (patch)
tree9a8c885cb882261d4e2aa41d8e182372b6c60f51 /challenge-101/abigail/node
parent4dbfa232abe9797f56c3ba7427dbae9fb6846943 (diff)
downloadperlweeklychallenge-club-67e66456e31b2dcbec30d2b88b0fa297180b6780.tar.gz
perlweeklychallenge-club-67e66456e31b2dcbec30d2b88b0fa297180b6780.tar.bz2
perlweeklychallenge-club-67e66456e31b2dcbec30d2b88b0fa297180b6780.zip
Node.js solution for week 101, part 2
Diffstat (limited to 'challenge-101/abigail/node')
-rw-r--r--challenge-101/abigail/node/ch-2.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-101/abigail/node/ch-2.js b/challenge-101/abigail/node/ch-2.js
new file mode 100644
index 0000000000..9f7ce81124
--- /dev/null
+++ b/challenge-101/abigail/node/ch-2.js
@@ -0,0 +1,51 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-2.js < input-file
+//
+
+//
+// See https://stackoverflow.com/questions/2049582/
+//
+
+//
+// This determines on which side of the line through (x1, y1) and
+// (x2, y2) the origin lies. If > 0, then the origin lies to the left
+// of the line, if < 0, the origin lies to the right of the line, if
+// = 0, the origin lies on the line.
+//
+function side (x1, y1, x2, y2) {
+ return (y2 - y1) * x2 - (x2 - x1) * y2
+}
+
+
+require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => {
+ //
+ // Parse input
+ //
+ let [x1, y1, x2, y2, x3, y3] = _ . split (/\s+/)
+
+ //
+ // Determine where the origin is relative to the three lines
+ // through the vertices of the triangle. Note we have to go
+ // in a specific order through the points. (Either clock wise,
+ // or counter clockwise, as long as we're consistent).
+ //
+ let s1 = side (x2, y2, x3, y3)
+ let s2 = side (x3, y3, x1, y1)
+ let s3 = side (x1, y1, x2, y2)
+
+ //
+ // If the origin either lies to the left (or on) each of the
+ // lines, or to the right (or on) each of the lines, the origin
+ // lies inside the triangle. If not, it does not.
+ //
+ console . log ((s1 <= 0 && s2 <= 0 && s3 <= 0) ||
+ (s1 >= 0 && s2 >= 0 && s3 >= 0) ? 1 : 0)
+});