From 121ee4d40c2cd203d1a0dfef2b8e1d8d83b0b43f Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Wed, 1 Jul 2015 20:07:37 -0400 Subject: SHERLOCK: RT: Fix zone checks in closestZone method --- engines/sherlock/scene.cpp | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/engines/sherlock/scene.cpp b/engines/sherlock/scene.cpp index c3917fb003..69deedef28 100644 --- a/engines/sherlock/scene.cpp +++ b/engines/sherlock/scene.cpp @@ -1380,16 +1380,45 @@ int Scene::whichZone(const Common::Point &pt) { } int Scene::closestZone(const Common::Point &pt) { - int dist = 1000; int zone = -1; + int dist = 9999; + int d; for (uint idx = 0; idx < _zones.size(); ++idx) { - Common::Point zc((_zones[idx].left + _zones[idx].right) / 2, - (_zones[idx].top + _zones[idx].bottom) / 2); - int d = ABS(zc.x - pt.x) + ABS(zc.y - pt.y); + Common::Rect &r = _zones[idx]; + // Check the distance from the point to the center of the zone + d = ABS(r.left + (r.width() / 2) - pt.x) + ABS(r.top + (r.height() / 2) - pt.y); + if (d < dist) { + dist = d; + zone = idx; + } + + // Check the distance from the point to the upper left of the zone + d = ABS((int)(r.left - pt.x)) + ABS((int)(r.top - pt.y)); + if (d < dist) + { + dist = d; + zone = idx; + } + + // Check the distance from the point to the upper right of the zone + d = ABS(r.left + r.width() - pt.x) + ABS(r.top - pt.y); + if (d < dist) { + dist = d; + zone = idx; + } + + // Check the distance from the point to the lower left of the zone + d = ABS(r.left - pt.x) + ABS(r.top + r.height() - pt.y); + if (d < dist) { + dist = d; + zone = idx; + } + + // Check the distance from the point to the lower right of the zone + d = ABS(r.left + r.width() - pt.x) + ABS(r.top + r.height() - pt.y); if (d < dist) { - // Found a closer zone dist = d; zone = idx; } -- cgit v1.2.3