Simplified Grid::snap.

Fri, 03 Mar 2017 23:16:14 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 03 Mar 2017 23:16:14 +0200
changeset 1179
1a9ffd5e0399
parent 1178
3a88e7a60b63
child 1180
2005e4147ad6

Simplified Grid::snap.

src/editmodes/abstractEditMode.cpp file | annotate | diff | comparison | revisions
src/editmodes/circleMode.cpp file | annotate | diff | comparison | revisions
src/glcamera.cpp file | annotate | diff | comparison | revisions
src/grid.cpp file | annotate | diff | comparison | revisions
src/grid.h file | annotate | diff | comparison | revisions
--- a/src/editmodes/abstractEditMode.cpp	Fri Mar 03 23:08:04 2017 +0200
+++ b/src/editmodes/abstractEditMode.cpp	Fri Mar 03 23:16:14 2017 +0200
@@ -311,8 +311,8 @@
 		renderer()->getRelativeAxes(relativeX, relativeY);
 		QLineF line = {vertex0[relativeX], vertex0[relativeY], vertex1[relativeX], vertex1[relativeY]};
 		line.setAngle(roundToInterval<int>(line.angle(), 45));
-		result.setCoordinate(relativeX, grid()->snap(line.x2(), Grid::Coordinate));
-		result.setCoordinate(relativeY, grid()->snap(line.y2(), Grid::Coordinate));
+		result.setCoordinate(relativeX, grid()->snap(line.x2()));
+		result.setCoordinate(relativeY, grid()->snap(line.y2()));
 	}
 
 	return result;
--- a/src/editmodes/circleMode.cpp	Fri Mar 03 23:08:04 2017 +0200
+++ b/src/editmodes/circleMode.cpp	Fri Mar 03 23:16:14 2017 +0200
@@ -56,7 +56,7 @@
 		renderer()->getRelativeAxes(localx, localy);
 		double dx = m_drawedVerts[0][localx] - v1[localx];
 		double dy = m_drawedVerts[0][localy] - v1[localy];
-		return grid()->snap(hypot(dx, dy), Grid::Coordinate);
+		return grid()->snap(hypot(dx, dy));
 	}
 
 	return 0.0;
--- a/src/glcamera.cpp	Fri Mar 03 23:08:04 2017 +0200
+++ b/src/glcamera.cpp	Fri Mar 03 23:16:14 2017 +0200
@@ -107,8 +107,8 @@
 		// If a grid was passed, snap coordinates to it.
 		if (grid)
 		{
-			cx = grid->snap(cx, Grid::Coordinate);
-			cy = grid->snap(cy, Grid::Coordinate);
+			cx = grid->snap(cx);
+			cy = grid->snap(cy);
 		}
 
 		roundToDecimals(cx, 4);
--- a/src/grid.cpp	Fri Mar 03 23:08:04 2017 +0200
+++ b/src/grid.cpp	Fri Mar 03 23:16:14 2017 +0200
@@ -65,17 +65,10 @@
 }
 
 
-qreal Grid::snap(qreal value, const Grid::Config type) const
+qreal Grid::snap(qreal value) const
 {
-	double snapvalue = (type == Grid::Coordinate) ? coordinateSnap() : angleSnap();
-	double multiplier = floor (qAbs<double>(value / snapvalue));
-	double out = multiplier * snapvalue;
-
-	if (qAbs (value) - (multiplier * snapvalue) > snapvalue / 2)
-		out += snapvalue;
-
-	if (value < 0)
-		out = -out;
-
-	return out;
+	// First, extract the amount of grid steps the value is away from zero, round that to remove the remainder,
+	// and multiply back by the the grid size.
+	double size = coordinateSnap();
+	return round(value / size) * size;
 }
--- a/src/grid.h	Fri Mar 03 23:08:04 2017 +0200
+++ b/src/grid.h	Fri Mar 03 23:16:14 2017 +0200
@@ -41,6 +41,6 @@
 	qreal angleAsRadians() const;
 	int bezierCurveSegments() const;
 	qreal coordinateSnap() const;
-	qreal snap(qreal value, const Grid::Config type) const;
+	qreal snap(qreal value) const;
 };
 

mercurial