From b564918e1c44dd54e3de51e8fbf5f5fad559b3b7 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Fri, 28 Feb 2025 19:29:27 -0500 Subject: [PATCH] Use multidimensional subscript operator in Grid --- src/grid.cpp | 7 +++---- src/grid.h | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/grid.cpp b/src/grid.cpp index 473dc52..64fd085 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -58,10 +58,10 @@ glm::vec3 Grid::normal_at(const size_t x, const size_t y, const auto x_diff = glm::vec3(static_cast(high_x - low_x), 0, - amplification * (get(high_x, y) - get(low_x, y))); + amplification * ((*this)[high_x, y] - (*this)[low_x, y])); const auto y_diff = glm::vec3(0, static_cast(high_y - low_y), - amplification * (get(x, high_y) - get(x, low_y))); + amplification * ((*this)[x, high_y] - (*this)[x, low_y])); return normalize(cross(x_diff, y_diff)); } @@ -135,12 +135,11 @@ Grid *Grid::PerlinNoise(const int globalX, const int globalY, // Interpolates between the dot_major results of the four grid vectors // around the current point - const auto val = Interpolate( + (*grid)[x, y] = Interpolate( x_offset, Interpolate(y_offset, dot_major(false, false), dot_major(false, true)), Interpolate(y_offset, dot_major(true, false), dot_major(true, true))); - grid->set(x, y, val); } } diff --git a/src/grid.h b/src/grid.h index 851f641..5d94c9e 100644 --- a/src/grid.h +++ b/src/grid.h @@ -17,11 +17,11 @@ constexpr std::size_t index(const std::size_t x, const std::size_t y) { class Grid { public: - float get(const std::size_t x, const std::size_t y) const { + float &operator[](const std::size_t x, const std::size_t y) { return (*data_)[index(x, y)]; } - void set(const std::size_t x, const std::size_t y, const float value) const { - (*data_)[index(x, y)] = value; + float &operator[](const std::size_t x, const std::size_t y) const { + return (*data_)[index(x, y)]; } Grid operator+(const Grid &) const;