From: Tim Moore Date: Sun, 18 Jan 2009 23:04:59 +0000 (+0100) Subject: Protect against division by zero in QuadTreeBuilder X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=1a8fd626803635c7edf0647fbdb4be30d32572aa;p=simgear.git Protect against division by zero in QuadTreeBuilder This could only happen when there's one leaf in the tree, or all the objects happen to have the same position. Noticed by Csaba Halaz --- diff --git a/simgear/scene/util/QuadTreeBuilder.hxx b/simgear/scene/util/QuadTreeBuilder.hxx index 2c5c4bf5..62734e3b 100644 --- a/simgear/scene/util/QuadTreeBuilder.hxx +++ b/simgear/scene/util/QuadTreeBuilder.hxx @@ -80,10 +80,14 @@ public: { using namespace osg; const Vec3 center(_getLocalCoords(obj)); - int x = (int)(_dimension * (center.x() - _min.x()) + int x = 0; + if (_max.x() != _min.x()) + x = (int)(_dimension * (center.x() - _min.x()) / (_max.x() - _min.x())); x = clampTo(x, 0, (_dimension - 1)); - int y = (int)(_dimension * (center.y() - _min.y()) + int y = 0; + if (_max.y() != _min.y()) + y = (int)(_dimension * (center.y() - _min.y()) / (_max.y() - _min.y())); y = clampTo(y, 0, (_dimension -1)); _addLeafObject(_leaves(y, x), obj);