bool Tower::colRect(Vec A, Vec B) const { double minX = std::min(A.x, B.x); double minY = std::min(A.y, B.y); double maxX = std::max(A.x, B.x); double maxY = std::max(A.y, B.y); return (x > minX && x < maxX && (y + r() > minY && y - r() < maxY)) || (y > minY && y < maxY && (x + r() > minX && x - r() < maxX)) || distSquared(getPos(), A) < r()*r() || distSquared(getPos(), B) < r()*r() || distSquared(getPos(), Vec(A.x, B.y)) < r()*r() || distSquared(getPos(), Vec(B.x, A.y)) < r()*r(); }
int main() { PD m, prev, p, best, cur; while(cin >> m.XX >> m.YY) { int N; cin >> N; cin >> prev.XX >> prev.YY; best = prev; FORI(N) { cin >> p.XX >> p.YY; cur = nearestPointOnLineSegment(prev, p, m); if(distSquared(m, cur) < distSquared(m, best)) best = cur; prev = p; } printf("%.4lf\n%.4lf\n", best.XX, best.YY); } return 0; }
// Check for collisions between Pacman and any dots. Play a sound and remove the dot on collision. // A collision occurs if the distance between the center of Pacman and a dot is less than or equal to the sum of their radii. void checkForCollision() { for(int i = 0; i < numDots; i++) if(distSquared(pmX + pmHeight/2, dotRect[i]->x + dotWH/2, pmY + pmHeight/2, dotRect[i]->y + dotWH/2) <= 0.8*(double)pow((double)pmHeight/2.0 + (double)dotWH/2.0, 2)) { Mix_PlayChannel(-1, pmWaka, 0); for(int j = i; j < numDots; j++) dotRect[i] = dotRect[j]; numDots--; } }
// Stolen from: http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment // Return minimum distance point on line segment vw minimizing the distance to point p PD nearestPointOnLineSegment(PD v, PD w, PD p) { if(epsEq(v, w)) return v; // v == w case. Avoid division by zero. double l2 = distSquared(v, w); // Consider the line extending the segment, parameterized as v + t (w - v). // We find projection of point p onto the line. // It falls where t = [(p-v) . (w-v)] / |w-v|^2 // We clamp t from [0,1] to handle points outside the segment vw. PD wv = w-v; double t = clamp01(dotProduct(p-v, wv) / l2); PD projection = v + (wv * t); // Projection falls on the segment return projection; }
double dist(PD a, PD b) { return sqrt(distSquared(a, b)); }
bool Tower::isInside(Vec vec) const { return distSquared(getPos(), vec) < r()*r(); }