Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. void WDrawNode::drawSmoothSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color)
  2. {
  3.     Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  4.     if (!vertices)
  5.         return;
  6.  
  7.     ssize_t p;
  8.     float lt;
  9.     float deltaT = 1.0f / config->count();
  10.  
  11.     Vec2 lastPos;
  12.     Vec2 lastN;
  13.  
  14.     for (unsigned int i = 0; i < segments + 1; i++) {
  15.  
  16.         float dt = (float)i / segments;
  17.  
  18.         // border
  19.         if (dt == 1) {
  20.             p = config->count() - 1;
  21.             lt = 1;
  22.         }
  23.         else {
  24.             p = dt / deltaT;
  25.             lt = (dt - deltaT * (float)p) / deltaT;
  26.         }
  27.  
  28.         // Interpolate
  29.         Vec2 pp0 = config->getControlPointAtIndex(p - 1);
  30.         Vec2 pp1 = config->getControlPointAtIndex(p + 0);
  31.         Vec2 pp2 = config->getControlPointAtIndex(p + 1);
  32.         Vec2 pp3 = config->getControlPointAtIndex(p + 2);
  33.  
  34.         Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
  35.  
  36.         if (i == 0)
  37.         {
  38.             lastPos = newPos;
  39.             continue;
  40.         }
  41.        
  42.         Vec2 n = newPos - lastPos;
  43.         n = Vec2(-n.y, n.x);
  44.         n.normalize();
  45.  
  46.         if (i == 1)
  47.         {
  48.             lastN = 0.5f * n;
  49.         }
  50.  
  51.         Vec2 p1 = lastPos + lastN * _lineWidth;
  52.         Vec2 p2 = lastPos + lastN * -_lineWidth;
  53.         Vec2 p3 = newPos + n * _lineWidth;
  54.         Vec2 p4 = newPos + n * -_lineWidth;
  55.  
  56.         drawTriangle(p1, p3, p2, color);
  57.         drawTriangle(p3, p4, p2, color);
  58.         lastPos = newPos;
  59.         lastN = n;
  60.  
  61.         //vertices[i].x = newPos.x;
  62.         //vertices[i].y = newPos.y;
  63.     }
  64.     //drawPoly(vertices, segments + 1, false, color);
  65.  
  66.     CC_SAFE_DELETE_ARRAY(vertices);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement