# Calculating points along a curveVertex line

**URL:** <https://discourse.processing.org/t/calculating-points-along-a-curvevertex-line/11337>\
**Category:** Gallery\
**Created:** [May 17, 2019, 4:43pm UTC](https://discourse.processing.org/t/calculating-points-along-a-curvevertex-line/11337 "2019-05-17T16:43:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoeMcKay](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JoeMcKay](https://discourse.processing.org/u/JoeMcKay)\
**Post date:** [May 17, 2019, 4:43pm UTC](https://discourse.processing.org/t/calculating-points-along-a-curvevertex-line/11337/1 "2019-05-17T16:43:55Z")

</div>

I am using curveVertex to create a line. I wanted to have a sprite move along the line and found that curvePoint() wasn’t giving me really accurate results. A little digging into the Catmull-Rom splines and I found the formula for calculating points on a line and it works great. Here it is in an example sketch if anyone is interested.

```auto
PVector plist [] = new PVector [4]; 

void setup() {
  size(400, 400);
  plist[0] = new PVector(84, 91);
  plist[1] = new PVector(68, 19);
  plist[2] = new PVector(21, 17);
  plist[3] = new PVector(32, 100);
}
void draw() {
  background(200); 
  noFill();
  beginShape();
  curveVertex(plist[0].x, plist[0].y);
  curveVertex(plist[0].x, plist[0].y);
  curveVertex(plist[1].x, plist[1].y);
  curveVertex(plist[2].x, plist[2].y);
  curveVertex(plist[3].x, plist[3].y);
  curveVertex(plist[3].x, plist[3].y);
  endShape();
  for (int i = 0; i< plist.length; i ++) {
    ellipse(plist[i].x, plist[i].y, 6, 6);
  }
  float lerpPoint = map(mouseX, 0, width, 1, 0); 
  PVector pointOnCurve = getCurveVertexPoint(plist[0], plist[1], plist[2], plist[3], lerpPoint); 
  ellipse(pointOnCurve.x, pointOnCurve.y, 12, 12);
}

PVector getCurveVertexPoint(PVector P0, PVector P1, PVector P2, PVector P3, float t) {
  float alpha = 0.5; 
  float px = alpha * ((2 * P1.x) + (-P0.x+P2.x) * t + (2 * P0.x-5 * P1.x + 4*P2.x - P3.x) * (t * t) + (-P0.x + 3 * P1.x-3 * P2.x + P3.x) * (t * t * t)) ;
  float py = alpha * ((2 * P1.y) + (-P0.y+P2.y) * t + (2 * P0.y-5 * P1.y + 4*P2.y - P3.y) * (t * t) + (-P0.y + 3 * P1.y-3 * P2.y + P3.y) * (t * t * t)) ;
  PVector returnVector = new PVector(px, py); 
  return returnVector;
}

```

---

<div class="post-metadata">

**Author:** ![yairkass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yairkass/32/5366_2.png) [@yairkass](https://discourse.processing.org/u/yairkass)\
**Post date:** [May 26, 2019, 12:44pm UTC](https://discourse.processing.org/t/calculating-points-along-a-curvevertex-line/11337/2 "2019-05-26T12:44:37Z")

</div>

thanks, I need this :-))
