Skip to content

Commit baf9083

Browse files
authored
Fix continues drawCardinalSplines() drawing outside the control points on last control point (#2303)
* Fix for issue #2302 * Codacy Static Code Analysis fix
1 parent 15711be commit baf9083

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

core/2d/DrawNode.cpp

+9-17
Original file line numberDiff line numberDiff line change
@@ -489,17 +489,15 @@ void DrawNode::drawCardinalSpline(PointArray* config,
489489
for (unsigned int i = 0; i < segments; i++)
490490
{
491491
float dt = (float)i / segments;
492+
p = static_cast<ssize_t>(dt / deltaT);
492493

493-
// border
494-
if (dt == 1)
494+
// Check last control point reached
495+
if (p >= (config->count() - 1))
495496
{
496-
p = config->count() - 1;
497-
lt = 1;
498-
}
499-
else
500-
{
501-
p = static_cast<ssize_t>(dt / deltaT);
502-
lt = (dt - deltaT * (float)p) / deltaT;
497+
_vertices[i] = config->getControlPointAtIndex(config->count() - 1);
498+
segments = i + 1;
499+
_vertices.resize(segments);
500+
break;
503501
}
504502

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

511-
Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
512-
_vertices[i].x = newPos.x;
513-
_vertices[i].y = newPos.y;
514-
if (newPos == config->getControlPointAtIndex(config->count() - 1) && i > 0)
515-
{
516-
segments = i + 1;
517-
break;
518-
}
509+
lt = (dt - deltaT * (float)p) / deltaT;
510+
_vertices[i] = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
519511
}
520512

521513
_drawPoly(_vertices.data(), segments, false, color, thickness, true);

tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -3515,9 +3515,18 @@ void DrawNodeSpLinesTest::update(float dt)
35153515

35163516
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 8, Color4F::GREEN, 20.0f);
35173517
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 8, Color4F::BLUE);
3518-
35193518
drawNode->drawCardinalSpline(array, 0.2f, points.size() * 16, Color4F(1.0f, 1.0f, 0.5f, 1.0f), 10.0f);
35203519

3520+
// Issue #2302
3521+
auto array3 = PointArray::create(20);
3522+
for (int i = 0; i < 10; i++)
3523+
{
3524+
array3->addControlPoint(Vec2((i % 2) ? 20 : screen.width - 20, 50 + i * 20));
3525+
drawNode->drawPoint(array3->getControlPointAtIndex(i), 10, Color4F::BLUE);
3526+
}
3527+
drawNode->drawCardinalSpline(array3, 0.1, 20, Color4F::ORANGE);
3528+
3529+
35213530
// drawNode->drawCatmullRom(array, points.size() * 8, Color4F::YELLOW,5);
35223531
// if (points.size()>3)
35233532
//{

0 commit comments

Comments
 (0)