//part of the quick hull algorithm based on http://www.ahristov.com/tutorial/geometry-games/convex-hull.html int point_location(City A, City B, City P){ int cp1 = (B.x() - A.x())*(P.y()-A.y()) - (B.y()-A.y())*(P.x()-A.x()); if(cp1 > 0) return 1; else return -1; }
//**in** //City A the starting point of the line //City B the ending point of the line //City point the point to find the distance to //**out** //the distance from this line to the point double point_to_line(Vec2 A, Vec2 B, City point) { float diffX = B.x() - A.x(); float diffY = B.y() - A.y(); if ((diffX == 0) && (diffY == 0)){ diffX = point.x() - A.x(); diffY = point.y() - A.y(); return sqrt(diffX * diffX + diffY * diffY); } float t = ((point.x() - A.x()) * diffX + (point.y() - A.y()) * diffY) / (diffX * diffX + diffY * diffY); if (t < 0){ //point is nearest to the first point i.e x1 and y1 diffX = point.x() - A.x(); diffY = point.y() - A.y(); } else if (t > 1){ //point is nearest to the end point i.e x2 and y2 diffX = point.x() - B.x(); diffY = point.y() - B.y(); } else { //if perpendicular line intersect the line segment. diffX = point.x() - (A.x() + t * diffX); diffY = point.y() - (A.y() + t * diffY); } //return shortest distance return sqrt(diffX * diffX + diffY * diffY); }
//part of the quick hull algorithm based on http://www.ahristov.com/tutorial/geometry-games/convex-hull.html int distance(City A, City B, City C){ int ABx = B.x() - A.x(); int ABy = B.y() - A.y(); int num = ABx * (A.y() - C.y()) - ABy * (A.x() - C.x()); if (num < 0) num = -num; return num; }