From f10eb2c8e53724d2e6055b539da14e65aaa843c3 Mon Sep 17 00:00:00 2001 From: dvlierop2 Date: Tue, 4 Dec 2007 19:09:09 +0000 Subject: [PATCH] When snapping, consider all four grid lines around the current point instead of only the nearest two --- src/display/canvas-axonomgrid.cpp | 29 ++---------------------- src/display/canvas-grid.cpp | 27 +++++----------------- src/util/mathfns.h | 37 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 49 deletions(-) diff --git a/src/display/canvas-axonomgrid.cpp b/src/display/canvas-axonomgrid.cpp index 6bb5d47c0..76e081a72 100644 --- a/src/display/canvas-axonomgrid.cpp +++ b/src/display/canvas-axonomgrid.cpp @@ -21,6 +21,7 @@ #include "sp-canvas-util.h" #include "canvas-axonomgrid.h" +#include "util/mathfns.h" #include "display-forward.h" #include @@ -603,32 +604,6 @@ CanvasAxonomGrid::Render (SPCanvasBuf *buf) } } - - - - - - - - - - - -/** - * \return x rounded to the nearest multiple of c1 plus c0. - * - * \note - * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero - * mean "ignore the grid in this dimention". We're currently discussing "good" semantics - * for guide/grid snapping. - */ - -/* FIXME: move this somewhere else, perhaps */ -static double round_to_nearest_multiple_plus(double x, double const c1, double const c0) -{ - return floor((x - c0) / c1 + .5) * c1 + c0; -} - CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d) { this->grid = grid; @@ -652,7 +627,7 @@ CanvasAxonomGridSnapper::_getSnapLines(NR::Point const &p) const scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom(); } - NR::Coord const rounded = round_to_nearest_multiple_plus(p[0], + NR::Coord const rounded = Inkscape::Util::round_to_nearest_multiple_plus(p[0], scaled_spacing, grid->origin[0]); diff --git a/src/display/canvas-grid.cpp b/src/display/canvas-grid.cpp index a2443ca17..db4648ec0 100644 --- a/src/display/canvas-grid.cpp +++ b/src/display/canvas-grid.cpp @@ -15,6 +15,7 @@ #include "sp-canvas-util.h" +#include "util/mathfns.h" #include "display-forward.h" #include #include "desktop-handles.h" @@ -859,24 +860,6 @@ CanvasXYGrid::Render (SPCanvasBuf *buf) } } - - - -/** - * \return x rounded to the nearest multiple of c1 plus c0. - * - * \note - * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero - * mean "ignore the grid in this dimention". We're currently discussing "good" semantics - * for guide/grid snapping. - */ - -/* FIXME: move this somewhere else, perhaps */ -static double round_to_nearest_multiple_plus(double x, double const c1, double const c0) -{ - return floor((x - c0) / c1 + .5) * c1 + c0; -} - CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d) { this->grid = grid; @@ -902,10 +885,10 @@ CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom(); } - NR::Coord const rounded = round_to_nearest_multiple_plus(p[i], - scaled_spacing, - grid->origin[i]); - + NR::Coord rounded; + rounded = Inkscape::Util::round_to_upper_multiple_plus(p[i], scaled_spacing, grid->origin[i]); + s.push_back(std::make_pair(NR::Dim2(i), rounded)); + rounded = Inkscape::Util::round_to_lower_multiple_plus(p[i], scaled_spacing, grid->origin[i]); s.push_back(std::make_pair(NR::Dim2(i), rounded)); } diff --git a/src/util/mathfns.h b/src/util/mathfns.h index 9ebd8bc2f..9f22ad11b 100644 --- a/src/util/mathfns.h +++ b/src/util/mathfns.h @@ -26,6 +26,43 @@ triangle_area (NR::Point p1, NR::Point p2, NR::Point p3) return (p1[NR::X]*p2[NR::Y] + p1[NR::Y]*p3[NR::X] + p2[NR::X]*p3[NR::Y] - p2[NR::Y]*p3[NR::X] - p1[NR::Y]*p2[NR::X] - p1[NR::X]*p3[NR::Y]); } +/** + * \return x rounded to the nearest multiple of c1 plus c0. + * + * \note + * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero + * mean "ignore the grid in this dimension". + */ +inline double round_to_nearest_multiple_plus(double x, double const c1, double const c0) +{ + return floor((x - c0) / c1 + .5) * c1 + c0; +} + +/** + * \return x rounded to the lower multiple of c1 plus c0. + * + * \note + * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero + * mean "ignore the grid in this dimension". + */ +inline double round_to_lower_multiple_plus(double x, double const c1, double const c0) +{ + return floor((x - c0) / c1) * c1 + c0; +} + +/** + * \return x rounded to the upper multiple of c1 plus c0. + * + * \note + * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero + * mean "ignore the grid in this dimension". + */ +inline double round_to_upper_multiple_plus(double x, double const c1, double const c0) +{ + return ceil((x - c0) / c1) * c1 + c0; +} + + } } -- 2.30.2