Skip to content

Commit

Permalink
Fix continues drawCardinalSplines() drawing outside the control point…
Browse files Browse the repository at this point in the history
…s on last control point (axmolengine#2303)

* Fix for issue axmolengine#2302

* Codacy Static Code Analysis fix
  • Loading branch information
aismann authored and halx99 committed Jan 5, 2025
1 parent c05e69a commit 16ff7c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
26 changes: 9 additions & 17 deletions core/2d/DrawNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,15 @@ void DrawNode::drawCardinalSpline(PointArray* config,
for (unsigned int i = 0; i < segments; i++)
{
float dt = (float)i / segments;
p = static_cast<ssize_t>(dt / deltaT);

// border
if (dt == 1)
// Check last control point reached
if (p >= (config->count() - 1))
{
p = config->count() - 1;
lt = 1;
}
else
{
p = static_cast<ssize_t>(dt / deltaT);
lt = (dt - deltaT * (float)p) / deltaT;
_vertices[i] = config->getControlPointAtIndex(config->count() - 1);
segments = i + 1;
_vertices.resize(segments);
break;
}

// Interpolate
Expand All @@ -508,14 +506,8 @@ void DrawNode::drawCardinalSpline(PointArray* config,
Vec2 pp2 = config->getControlPointAtIndex(p + 1);
Vec2 pp3 = config->getControlPointAtIndex(p + 2);

Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
_vertices[i].x = newPos.x;
_vertices[i].y = newPos.y;
if (newPos == config->getControlPointAtIndex(config->count() - 1) && i > 0)
{
segments = i + 1;
break;
}
lt = (dt - deltaT * (float)p) / deltaT;
_vertices[i] = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
}

_drawPoly(_vertices.data(), segments, false, color, thickness, true);
Expand Down
11 changes: 10 additions & 1 deletion tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3515,9 +3515,18 @@ void DrawNodeSpLinesTest::update(float dt)

drawNode->drawCardinalSpline(array, 0.2f, points.size() * 8, Color4F::GREEN, 20.0f);
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 8, Color4F::BLUE);

drawNode->drawCardinalSpline(array, 0.2f, points.size() * 16, Color4F(1.0f, 1.0f, 0.5f, 1.0f), 10.0f);

// Issue #2302
auto array3 = PointArray::create(20);
for (int i = 0; i < 10; i++)
{
array3->addControlPoint(Vec2((i % 2) ? 20 : screen.width - 20, 50 + i * 20));
drawNode->drawPoint(array3->getControlPointAtIndex(i), 10, Color4F::BLUE);
}
drawNode->drawCardinalSpline(array3, 0.1, 20, Color4F::ORANGE);


// drawNode->drawCatmullRom(array, points.size() * 8, Color4F::YELLOW,5);
// if (points.size()>3)
//{
Expand Down

0 comments on commit 16ff7c7

Please sign in to comment.