diff options
-rw-r--r-- | engines/sherlock/scene.cpp | 39 |
1 files 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; } |