about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/1/Ex4.c
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.1/labwork/1/Ex4.c')
-rw-r--r--usth/ICT2.1/labwork/1/Ex4.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/usth/ICT2.1/labwork/1/Ex4.c b/usth/ICT2.1/labwork/1/Ex4.c
new file mode 100644
index 0000000..000f73a
--- /dev/null
+++ b/usth/ICT2.1/labwork/1/Ex4.c
@@ -0,0 +1,46 @@
+/*
+ * Print the area of the rectangle constructed from a pair of 2D points
+ * and check if a point it within this rectangle
+ * This is free and unencumbered software released into the public domain.
+ */
+
+#include <math.h>
+#include <stdio.h>
+
+/* Writing a header for just a simple struct would be overkill. */
+struct point {
+	double x, y;
+};
+
+struct rect {
+	struct point u, v;
+};
+
+double area(struct rect r)
+{
+	return fabs((r.u.x - r.v.x) * (r.u.y - r.v.y));
+}
+
+int isin(struct point p, struct rect r)
+{
+	return ((p.x - r.u.x) * (p.x - r.v.x) < 0
+	        && (p.y - r.u.y) * (p.y - r.v.y) < 0);
+}
+
+int main()
+{
+	struct point m, n;
+	/* Don't bother logging errors, since it would pollute the pipe. */
+	do {
+		scanf("%lf %lf %lf %lf", &m.x, &m.y, &n.x, &n.y);
+	} while (m.x == n.x || m.y == n.y);
+
+	struct rect r = {m, n};
+	printf("%g\n", area(r));
+
+	struct point p;
+	scanf("%lf %lf", &p.x, &p.y);
+	printf("%d\n", isin(p, r));
+
+	return 0;
+}