Files
core_math/tjp/core/math/SimpleLine.cpp
Timothy Prepscius 0807c0286a flatten 20260225
2026-02-25 12:36:47 -05:00

63 lines
1.0 KiB
C++
Executable File

//
// Math.hpp
// amoeba
//
// Created by Timothy Prepscius on 12/30/16.
// Copyright © 2016 Timothy Prepscius. All rights reserved.
//
#include "SimpleLine.hpp"
#include "Vector2.inl"
#include <tjp/core/assert/debug_assert.h>
namespace tjp::core::math {
SimpleLineCompute::SimpleLineCompute(std::initializer_list<math::Vector2f> l, bool cap_) :
cap(cap_)
{
p.insert(p.end(), l.begin(), l.end());
s.resize(p.size()-1);
if (p.empty())
return ;
auto next = p.begin();
auto prev = next++;
auto write = s.begin();
for ( ;next!=p.end(); prev = next++)
{
auto d = *next - *prev;
debug_assert(d[0] != 0);
auto sv = d[1]/d[0];
*write++ = sv;
}
}
float SimpleLineCompute::operator ()(float x)
{
debug_assert(!p.empty());
if (cap && x < p.front().x())
return p.front().y();
auto last = p.size()-1;
for (auto i=1; ; ++i)
{
auto &r = p[i];
if (x < r.x() || i == last)
{
if (i == last && cap)
return p[i].y();
auto &l = p[i-1];
auto &s_ = s[i-1];
return l.y() + (x-l.x()) * s_;
}
}
}
} // namespace