Added polar grid snapping code.

Sat, 04 Mar 2017 01:59:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 04 Mar 2017 01:59:58 +0200
changeset 1182
813d020f92d4
parent 1181
ca6d0ef9aadb
child 1183
caa20ed02728

Added polar grid snapping code.

src/canvas.cpp file | annotate | diff | comparison | revisions
src/grid.cpp file | annotate | diff | comparison | revisions
--- a/src/canvas.cpp	Sat Mar 04 00:54:46 2017 +0200
+++ b/src/canvas.cpp	Sat Mar 04 01:59:58 2017 +0200
@@ -95,7 +95,7 @@
  */
 void Canvas::drawFixedCameraBackdrop()
 {
-	static const enum { Cartesian, Polar } gridType = Cartesian;
+	static const enum { Cartesian, Polar } gridType = Polar;
 
 	// Find the top left corner of the grid
 	Vertex topLeft = currentCamera().idealize(currentCamera().convert2dTo3d({0, 0}));
--- a/src/grid.cpp	Sat Mar 04 00:54:46 2017 +0200
+++ b/src/grid.cpp	Sat Mar 04 01:59:58 2017 +0200
@@ -67,10 +67,23 @@
 
 QPointF Grid::snap(QPointF point) const
 {
-	// For each co-ordinate, 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(point.x() / size) * size, round(point.y() / size) * size};
+	if (false)
+	{
+		// For each co-ordinate, 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(point.x() / size) * size, round(point.y() / size) * size};
+	}
+	else
+	{
+		qreal radius = hypot(point.x() - pole().x(), point.y() - -pole().y());
+		qreal azimuth = atan2(point.y() - -pole().y(), point.x() - pole().x());
+		double size = coordinateSnap();
+		double angleStep = 2 * pi / polarDivisions();
+		radius = round(radius / size) * size;
+		azimuth = round(azimuth / angleStep) * angleStep;
+		return {pole().x() + cos(azimuth) * radius, -pole().y() + sin(azimuth) * radius};
+	}
 }
 
 /*

mercurial