aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/abigail/bash
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-25 20:25:18 +0100
committerAbigail <abigail@abigail.be>2021-02-25 20:25:18 +0100
commit1ee5a3b374b07f10366df37b11cc9b6abe754fdd (patch)
tree721b622198f71527200afd581f38f507887635a6 /challenge-101/abigail/bash
parent3132e59ee2717acb05178211f25ff0427a7c726d (diff)
downloadperlweeklychallenge-club-1ee5a3b374b07f10366df37b11cc9b6abe754fdd.tar.gz
perlweeklychallenge-club-1ee5a3b374b07f10366df37b11cc9b6abe754fdd.tar.bz2
perlweeklychallenge-club-1ee5a3b374b07f10366df37b11cc9b6abe754fdd.zip
Bash solution for week 101, part 1
Diffstat (limited to 'challenge-101/abigail/bash')
-rw-r--r--challenge-101/abigail/bash/ch-2.sh47
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-101/abigail/bash/ch-2.sh b/challenge-101/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..12b159c5c3
--- /dev/null
+++ b/challenge-101/abigail/bash/ch-2.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < 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 calc_side () {
+ #
+ # $1: x1; $2: y1; $3 = x2; $4 = y2
+ #
+ side=$((($4 - $2) * $3 - ($3 - $1) * $4))
+}
+
+
+while read x1 y1 x2 y2 x3 y3
+do #
+ # 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).
+ #
+ calc_side x2 y2 x3 y3; s1=$side
+ calc_side x3 y3 x1 y1; s2=$side
+ calc_side x1 y1 x2 y2; s3=$side
+
+ #
+ # 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.
+ #
+ echo $(( $s1 <= 0 && $s2 <= 0 && $s3 <= 0 ||
+ $s1 >= 0 && $s2 >= 0 && $s3 >= 0 ? 1 : 0 ))
+done