Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void WDrawNode::drawSmoothSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color)
- {
- Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
- if (!vertices)
- return;
- ssize_t p;
- float lt;
- float deltaT = 1.0f / config->count();
- Vec2 lastPos;
- Vec2 lastN;
- for (unsigned int i = 0; i < segments + 1; i++) {
- float dt = (float)i / segments;
- // border
- if (dt == 1) {
- p = config->count() - 1;
- lt = 1;
- }
- else {
- p = dt / deltaT;
- lt = (dt - deltaT * (float)p) / deltaT;
- }
- // Interpolate
- Vec2 pp0 = config->getControlPointAtIndex(p - 1);
- Vec2 pp1 = config->getControlPointAtIndex(p + 0);
- Vec2 pp2 = config->getControlPointAtIndex(p + 1);
- Vec2 pp3 = config->getControlPointAtIndex(p + 2);
- Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
- if (i == 0)
- {
- lastPos = newPos;
- continue;
- }
- Vec2 n = newPos - lastPos;
- n = Vec2(-n.y, n.x);
- n.normalize();
- if (i == 1)
- {
- lastN = 0.5f * n;
- }
- Vec2 p1 = lastPos + lastN * _lineWidth;
- Vec2 p2 = lastPos + lastN * -_lineWidth;
- Vec2 p3 = newPos + n * _lineWidth;
- Vec2 p4 = newPos + n * -_lineWidth;
- drawTriangle(p1, p3, p2, color);
- drawTriangle(p3, p4, p2, color);
- lastPos = newPos;
- lastN = n;
- //vertices[i].x = newPos.x;
- //vertices[i].y = newPos.y;
- }
- //drawPoly(vertices, segments + 1, false, color);
- CC_SAFE_DELETE_ARRAY(vertices);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement