Browse Source

Shift center of tiling to center of the map

Tomasz Zieliński 1 year ago
parent
commit
ec20e9e907
2 changed files with 25 additions and 0 deletions
  1. 23 0
      lib/rmg/PenroseTiling.cpp
  2. 2 0
      lib/rmg/PenroseTiling.h

+ 23 - 0
lib/rmg/PenroseTiling.cpp

@@ -21,11 +21,21 @@ Point2D Point2D::operator * (float scale) const
 	return Point2D(x() * scale, y() * scale);
 }
 
+Point2D Point2D::operator / (float scale) const
+{
+	return Point2D(x() / scale, y() / scale);
+}
+
 Point2D Point2D::operator + (const Point2D& other) const
 {
 	return Point2D(x() + other.x(), y() + other.y());
 }
 
+Point2D Point2D::operator - (const Point2D& other) const
+{
+	return Point2D(x() - other.x(), y() - other.y());
+}
+
 bool Point2D::operator < (const Point2D& other) const
 {
 	if (x() < other.x())
@@ -163,6 +173,19 @@ std::set<Point2D> PenroseTiling::generatePenroseTiling(size_t numZones, CRandomG
 
 		split(t, points, indices, DEPTH);
 	}
+	// Find center of the mass, shift that center to (0.5, 0.5)
+
+	Point2D center = Point2D(0.0f, 0.0f);
+	for (auto & point : points)
+	{
+		center = center + point;
+	};
+	center = center / points.size();
+
+	for (auto & point : points)
+	{
+		point = point - center + Point2D(0.5f, 0.5f);
+	};
 
 	vstd::copy_if(points, vstd::set_inserter(finalPoints), [](const Point2D point)
 	{

+ 2 - 0
lib/rmg/PenroseTiling.h

@@ -28,7 +28,9 @@ public:
 	using point_xy::point_xy;
 
 	Point2D operator * (float scale) const;
+	Point2D operator / (float scale) const;
 	Point2D operator + (const Point2D& other) const;
+	Point2D operator - (const Point2D& other) const;
 	Point2D rotated(float radians) const;
 
 	bool operator < (const Point2D& other) const;