#pragma once #ifndef UTILITY_H static float sign(float x, float y, float x1, float y1, float x2, float y2) { return (x - x2) * (y1 - y2) - (x1 - x2) * (y - y2); } static float sign (Point &p1, Point &p2, Point &p3) { return (p1.x() - p3.x()) * (p2.y() - p3.y()) - (p2.x() - p3.x()) * (p1.y() - p3.y()); } static bool IsPointInTriangle(Point &pt, Point &p1, Point &p2, Point &p3) { float d1 = sign(pt, p1, p2); float d2 = sign(pt, p2, p3); float d3 = sign(pt, p3, p1); bool has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0); bool has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0); return !(has_neg && has_pos); } static float pDistance(float x, float y, float x1, float y1, float x2, float y2) { float A = x - x1; float B = y - y1; float C = x2 - x1; float D = y2 - y1; float dot = A * C + B * D; float len_sq = C * C + D * D; float param = -1; if (len_sq != 0) // in case of 0 length line param = dot / len_sq; float xx, yy; if (param < 0) { xx = x1; yy = y1; } else if (param > 1) { xx = x2; yy = y2; } else { xx = x1 + param * C; yy = y1 + param * D; } float dx = x - xx; float dy = y - yy; return dx * dx + dy * dy; //return sqrt(dx * dx + dy * dy); } static bool SortForMinXMaxY (const Point& a, const Point& b) { if (a.x() != b.x()) { return (a.x() < b.x()); } return (a.y() > b.y()); } static bool SortForMinYMaxX(const Point& a, const Point& b) { if (a.y() != b.y()) { return (a.x() < b.x()); } return (a.y() > b.y()); } static void sortPoints (std::vector& pts) { std::sort(pts.begin(), pts.end(), SortForMinXMaxY); } // TODO: what happens if all/more points are on hor/vert line? -> sort for x, than y should handle this static std::pair getMinMaxX(std::vector& pts) { // TODO: check if already sorted? assume array is sorted? call sort utility function??? //sortPoints(pts); std::sort(pts.begin(), pts.end(), SortForMinXMaxY); return std::make_pair(pts[0], pts[pts.size() - 1]); } #endif // UTILITY_H