add length

main
Brett 2025-03-19 19:14:40 -04:00
parent 66e764be6b
commit e1e2bb18a4
3 changed files with 40 additions and 2 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25)
include(FetchContent)
set(BLT_GRAPHICS_VERSION 2.0.1)
set(BLT_GRAPHICS_VERSION 2.0.2)
set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})

View File

@ -102,12 +102,13 @@ namespace blt::gfx
return *this;
}
[[nodiscard]] f32 length() const;
std::vector<line2d_t> lines;
};
struct curve2d_t
{
public:
curve2d_t(vec2 p0, vec2 p1, vec2 p2);
curve2d_t(vec2 p0, vec2 p1, vec2 p2, vec2 p3);
@ -117,6 +118,10 @@ namespace blt::gfx
[[nodiscard]] curve2d_mesh_data_t to_mesh(i32 segments, f32 thickness = 1.0) const;
[[nodiscard]] f32 length(i32 segments) const;
[[nodiscard]] f32 length_fast() const;
vec2 m_p0, m_p1, m_p2, m_p3;
};

View File

@ -116,6 +116,14 @@ namespace blt::gfx
return vertices;
}
f32 curve2d_mesh_data_t::length() const
{
f32 length = 0;
for (const auto& line : lines)
length += (line.p2 - line.p1).magnitude();
return length;
}
curve2d_t::curve2d_t(const vec2 p0, const vec2 p1, const vec2 p2): m_p0(p0), m_p1(p1), m_p2(p1), m_p3(p2)
{}
@ -157,6 +165,31 @@ namespace blt::gfx
return mesh_data;
}
f32 curve2d_t::length(const i32 segments) const
{
f32 length = 0;
float t = 0;
const float diff = 1.0f / static_cast<float>(segments);
for (i32 i = 0; i < segments; ++i)
{
auto begin = get_point(t);
t += diff;
auto end = get_point(t);
length += (end - begin).magnitude();
}
return length;
}
f32 curve2d_t::length_fast() const
{
const auto d1 = (m_p1 - m_p0).magnitude();
const auto d2 = (m_p2 - m_p1).magnitude();
const auto d3 = (m_p3 - m_p2).magnitude();
return d1 + d2 + d3;
}
void batch_renderer_2d::create()
{
{