This example demonstrates the mathematical beauty of the golden ratio by creating a logarithmic spiral overlaid on a heatmap with proportionally sized rectangles. It showcases the mathematical relationship between the golden ratio constant (φ ≈ 1.618) and natural spiral patterns found throughout nature.
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <vector>
auto getMaxOfArray(
const std::vector<double> &numArray) ->
double {
return *std::max_element(numArray.begin(), numArray.end());
}
auto getMinOfArray(
const std::vector<double> &numArray) ->
double {
return *std::min_element(numArray.begin(), numArray.end());
}
auto main(
int argc,
char *argv[]) ->
int {
int nspiral = 2;
auto th =
linspace((-M_PI) / 13, (2 * M_PI * nspiral), 1000);
std::vector<double> xValues;
std::vector<double> yValues;
for (double i : th) {
double a = 1.120529;
double b = 0.306349;
double r = a * std::exp((-b) * i);
double xResult = (r * std::cos(i));
double yResult = (r * std::sin(i));
xValues.push_back(xResult);
yValues.push_back(yResult);
}
std::vector<double> spiralX;
std::vector<double> spiralY;
for (size_t i = 0; i < xValues.size(); i++) {
spiralX.push_back(-(xValues[i]) + xValues[0]);
spiralY.push_back(yValues[i] - yValues[0] + yShift);
}
double phi = (1.0 + std::sqrt(5.0)) / 2.0;
std::vector<double> xe = {0, 1, (1 + (1 / std::pow(phi, 4))),
1 + (1 / std::pow(phi, 3)), phi};
std::vector<double> ye = {0, (1 / std::pow(phi, 3)),
(1 / std::pow(phi, 3)) + (1 / std::pow(phi, 4)),
(1 / std::pow(phi, 2)), 1};
std::vector<double> yeShifted;
yeShifted.reserve(ye.size());
for (double yi : ye) {
yeShifted.push_back(yi + yShift);
}
std::vector<std::vector<int>> zValues = {
{13, 3, 3, 5}, {13, 2, 1, 5}, {13, 10, 11, 12}, {13, 8, 8, 8}};
{"y", spiralY},
{"type", "scatter"},
{"line", {{"color", "white"}, {"width", 3}}}};
{"y", yeShifted},
{"z", zValues},
{"type", "heatmap"},
{"colorscale", "Viridis"}};
plotly::Object axisTemplate = {{
"range", std::vector<double>{0, 1.6}},
{"autorange", false},
{"showgrid", false},
{"zeroline", false},
{"linecolor", "black"},
{"showticklabels", false},
{"ticks", ""}};
{"title", {{"text", "Heatmap with Unequal Block Sizes"}}},
{"margin", {{"t", 200}, {"r", 200}, {"b", 200}, {"l", 200}}},
{"xaxis", axisTemplate},
{"yaxis", axisTemplate},
{"showlegend", false},
{"width", 700},
{"height", 700},
{"autosize", false}};
std::vector<plotly::Object> data = {spiralTrace, heatmapTrace};
if (!args.headless) {
} else {
{"width", 700},
{"height", 700},
{"filename", "golden_ratio"}};
}
return 0;
}
auto parseGalleryArgs(int argc, char *argv[]) -> GalleryArgs
Parse command line arguments for gallery examples.
Definition arg_parser.cpp:4
auto main() -> int
Definition gallery_animate_sin_wave.cpp:48
auto getMinOfArray(const std::vector< double > &numArray) -> double
Definition gallery_golden_ratio.cpp:56
auto getMaxOfArray(const std::vector< double > &numArray) -> double
Definition gallery_golden_ratio.cpp:52
auto linspace(double a, double b, int n) -> std::vector< double >
Create a linearly spaced vector of values between two endpoints.
Definition linspace.cpp:4
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.