X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fobject.cpp;h=93af822b8476008c2f60e6158955078cdaf38c0d;hb=e8987f4028a1f9c0eeb33a45bd11b2e409b9c2c5;hp=404d30576113a97ad9516b04677d087f81ad4346;hpb=eb711912d64f17cf9c18c74c4d78d9867bd066ad;p=architektonas diff --git a/src/object.cpp b/src/object.cpp index 404d305..93af822 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -17,6 +17,7 @@ #include "object.h" #include +#include // Initialize static variables bool Object::fixedAngle = false; @@ -31,6 +32,7 @@ bool Object::ignoreClicks = false; bool Object::dontMove = false; bool Object::selectionInProgress = false; QRectF Object::selection; +double Object::gridSpacing; Object::Object(): position(Vector(0, 0)), parent(0), type(OTObject), @@ -291,3 +293,25 @@ void Object::SetSnapMode(bool state/*= true*/) { snapToGrid = state; } + + +// +// This looks strange, but it's really quite simple: We want a point that's +// more than half-way to the next grid point to snap there while conversely we +// want a point that's less than half-way to to the next grid point then snap +// to the one before it. So we add half of the grid spacing to the point, then +// divide by it so that we can remove the fractional part, then multiply it +// back to get back to the correct answer. +// +Vector Object::SnapPointToGrid(Vector point) +{ + point += gridSpacing / 2.0; // *This* adds to Z!!! + point /= gridSpacing; + point.x = floor(point.x);//need to fix this for negative numbers... + point.y = floor(point.y); + point.z = 0; // Make *sure* Z doesn't go anywhere!!! + point *= gridSpacing; + return point; +} + +