aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/paulo-custodio/cpp/ch-2.cpp
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/cpp/ch-2.cpp
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/cpp/ch-2.cpp')
-rw-r--r--challenge-101/paulo-custodio/cpp/ch-2.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-101/paulo-custodio/cpp/ch-2.cpp b/challenge-101/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..0f1ce5bceb
--- /dev/null
+++ b/challenge-101/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,54 @@
+/*
+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.
+*/
+
+#include <iostream>
+
+typedef struct Point {
+ double x, y;
+} Point;
+
+double sign(Point p1, Point p2, Point p3) {
+ return (p1.x - p3.x) * (p2.y - p3.y)
+ - (p2.x - p3.x) * (p1.y - p3.y);
+}
+
+int point_in_triangle(Point p0, Point p1, Point p2, Point p3) {
+ double d1 = sign(p0, p1, p2);
+ double d2 = sign(p0, p2, p3);
+ double d3 = sign(p0, p3, p1);
+
+ bool has_neg = (d1 < 0.0) || (d2 < 0.0) || (d3 < 0.0);
+ bool has_pos = (d1 > 0.0) || (d2 > 0.0) || (d3 > 0.0);
+
+ if (!(has_neg && has_pos))
+ return 1;
+ else
+ return 0;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 7) {
+ std::cerr << "Usage: ch-2 x1 y1 x2 y2 x3 y3" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+ else {
+ Point p[3], p0;
+ for (int i = 0; i < 3; i++) {
+ p[i].x = atof(argv[i * 2 + 1]);
+ p[i].y = atof(argv[i * 2 + 2]);
+ }
+ p0.x = p0.y = 0.0;
+ std::cout << point_in_triangle(p0, p[0], p[1], p[2]) << std::endl;
+ }
+}