#include "Triangle.h" #include 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; }