From cab7f47bebe6830acdf4c377e2e04fb26b95d922 Mon Sep 17 00:00:00 2001 From: aismann Date: Sat, 4 Jan 2025 22:30:37 +0100 Subject: [PATCH 1/2] Fix for issue #2302 --- core/2d/DrawNode.cpp | 29 +++++++------------ .../Source/DrawNodeTest/DrawNodeTest.cpp | 11 ++++++- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/core/2d/DrawNode.cpp b/core/2d/DrawNode.cpp index 30aaf649beeb..a8af8953f539 100644 --- a/core/2d/DrawNode.cpp +++ b/core/2d/DrawNode.cpp @@ -484,22 +484,21 @@ void DrawNode::drawCardinalSpline(PointArray* config, ssize_t p; float lt; + float dt; float deltaT = 1.0f / config->count(); for (unsigned int i = 0; i < segments; i++) { - float dt = (float)i / segments; + dt = (float)i / segments; + p = static_cast(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(dt / deltaT); - lt = (dt - deltaT * (float)p) / deltaT; + _vertices[i] = config->getControlPointAtIndex(config->count() - 1); + segments = i + 1; + _vertices.resize(segments); + break; } // Interpolate @@ -508,14 +507,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); diff --git a/tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp b/tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp index 124df8b6b148..deeefeae157a 100644 --- a/tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp +++ b/tests/cpp-tests/Source/DrawNodeTest/DrawNodeTest.cpp @@ -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) //{ From 112442650159446b62e54108def29bd6d58ec3f0 Mon Sep 17 00:00:00 2001 From: aismann Date: Sat, 4 Jan 2025 22:43:35 +0100 Subject: [PATCH 2/2] Codacy Static Code Analysis fix --- core/2d/DrawNode.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/2d/DrawNode.cpp b/core/2d/DrawNode.cpp index a8af8953f539..217244f654ae 100644 --- a/core/2d/DrawNode.cpp +++ b/core/2d/DrawNode.cpp @@ -484,12 +484,11 @@ void DrawNode::drawCardinalSpline(PointArray* config, ssize_t p; float lt; - float dt; float deltaT = 1.0f / config->count(); for (unsigned int i = 0; i < segments; i++) { - dt = (float)i / segments; + float dt = (float)i / segments; p = static_cast(dt / deltaT); // Check last control point reached