feat: project skeleton, core types and math with transform tests

This commit is contained in:
Martino Ferrari committed 2026-09-27 20:54:19 +02:00
1 parent e4051bc6b5
commit 8a7e9e97f8
21 files changed
+515 -2

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
#include <plot/math/transform.hpp>
namespace plot::math {
namespace {
/// Resolve the endpoints of one axis in transform space, widening a degenerate
/// range by +/-0.5 so the mapping stays invertible.
void axis_ends(Range r, bool log, f64& lo, f64& hi) noexcept {
lo = log ? Transform::log10_positive(r.min) : r.min;
hi = log ? Transform::log10_positive(r.max) : r.max;
if (!(hi > lo)) {
lo -= 0.5;
hi += 0.5;
}
}
} // namespace
f64 Transform::log10_positive(f64 v) noexcept {
constexpr f64 kMin = 1e-300;
return std::log10(v < kMin ? kMin : v);
}
Transform Transform::from_range(Range x, Range y, f64 px0, f64 px1, f64 py0, f64 py1, bool lx,
bool ly) noexcept {
Transform t;
t.log_x = lx;
t.log_y = ly;
f64 x_lo = 0.0, x_hi = 0.0;
axis_ends(x, lx, x_lo, x_hi);
t.sx = (px1 - px0) / (x_hi - x_lo);
t.tx = px0 - x_lo * t.sx;
f64 y_lo = 0.0, y_hi = 0.0;
axis_ends(y, ly, y_lo, y_hi);
t.sy = (py1 - py0) / (y_hi - y_lo);
t.ty = py0 - y_lo * t.sy;
return t;
}
} // namespace plot::math