Support non-const width lines in gfx::painter

This commit is contained in:
Nikita Lisitsa 2024-08-18 01:12:17 +03:00
parent 82f7d5d429
commit a2c83633ae
2 changed files with 11 additions and 10 deletions

View file

@ -57,7 +57,7 @@ namespace psemek::gfx
void rect(geom::box<float, 2> const & box, color const & c);
void circle(geom::point<float, 2> const & center, float radius, color const & c, int quality = 24);
void line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width, color const & c, bool smooth = true);
void line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width, color const & c0, color const & c1, bool smooth = true);
void line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float w0, float w1, color const & c0, color const & c1, bool smooth = true);
void besier(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, geom::point<float, 2> const & p2, float width, color const & c, int quality = 8, bool smooth = true);
// 2D text

View file

@ -235,21 +235,22 @@ namespace psemek::gfx
void painter::line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width, color const & c, bool smooth)
{
line(p0, p1, width, c, c, smooth);
line(p0, p1, width, width, c, c, smooth);
}
void painter::line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width, color const & c0, color const & c1, bool smooth)
void painter::line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width0, float width1, color const & c0, color const & c1, bool smooth)
{
std::uint32_t const base = impl().vertices.size();
float const r = width / 2.f;
float const r0 = width0 / 2.f;
float const r1 = width1 / 2.f;
auto const d = geom::normalized(p1 - p0);
geom::vector<float, 2> const o { -d[1], d[0] };
impl().vertices.push_back({{p0[0] + r * o[0], p0[1] + r * o[1], 0.f}, c0});
impl().vertices.push_back({{p0[0] - r * o[0], p0[1] - r * o[1], 0.f}, c0});
impl().vertices.push_back({{p1[0] + r * o[0], p1[1] + r * o[1], 0.f}, c1});
impl().vertices.push_back({{p1[0] - r * o[0], p1[1] - r * o[1], 0.f}, c1});
impl().vertices.push_back({{p0[0] + r0 * o[0], p0[1] + r0 * o[1], 0.f}, c0});
impl().vertices.push_back({{p0[0] - r0 * o[0], p0[1] - r0 * o[1], 0.f}, c0});
impl().vertices.push_back({{p1[0] + r1 * o[0], p1[1] + r1 * o[1], 0.f}, c1});
impl().vertices.push_back({{p1[0] - r1 * o[0], p1[1] - r1 * o[1], 0.f}, c1});
impl().indices.push_back(base + 0);
impl().indices.push_back(base + 1);
@ -260,8 +261,8 @@ namespace psemek::gfx
if (smooth)
{
circle(p0, r, c0);
circle(p1, r, c1);
circle(p0, r0, c0);
circle(p1, r1, c1);
}
}