summaryrefslogtreecommitdiffstats
path: root/Triangle.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Triangle.cpp')
-rw-r--r--Triangle.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/Triangle.cpp b/Triangle.cpp
new file mode 100644
index 0000000..e367629
--- /dev/null
+++ b/Triangle.cpp
@@ -0,0 +1,36 @@
+#include "Triangle.h"
+#include <math.h>
+
+
+Triangle::Direction Triangle::getDirection() const {
+ double c = (va.getX()-vb.getX())*(vc.getY()-vb.getY()) - (vc.getX()-vb.getX())*(va.getY()-vb.getY());
+
+ return (c < 0) ? CW : (c > 0) ? CCW : Unknown;
+}
+
+double Triangle::area() const {
+ double a = vb.distanceSq(vc);
+ double b = vc.distanceSq(va);
+ double c = va.distanceSq(vb);
+
+ return sqrt((a+b+c)*a+b+c - 2*(a*a+b*b+c*c))/4;
+}
+
+double Triangle::perimeter() const {
+ return va.distance(vb) + vb.distance(vc) + vc.distance(va);
+}
+
+bool Triangle::contains(const Vertex &v) const {
+ double a = (v.getX()-vb.getX())*(vc.getY()-vb.getY()) - (vc.getX()-vb.getX())*(v.getY()-vb.getY());
+ double b = (v.getX()-vc.getX())*(va.getY()-vc.getY()) - (va.getX()-vc.getX())*(v.getY()-vc.getY());
+ double c = (v.getX()-va.getX())*(vb.getY()-va.getY()) - (vb.getX()-va.getX())*(v.getY()-va.getY());
+
+ switch(getDirection()) {
+ case CW:
+ return ((a < 0) && (b < 0) && (c < 0));
+ case CCW:
+ return ((a > 0) && (b > 0) && (c > 0));
+ }
+
+ return false;
+}