Get intersection point of line and CIE chromaticity curve

Great! Sounds like you have a solution, then. Loop over your segments and test them against your line for intersection.

Here is Thompson’s tutorial method that I linked to earlier, modified to return the intersection point (or null if no intersection):

PVector lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
  // calculate the distance to intersection point
  float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
  float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
  // if uA and uB are between 0-1, lines are colliding
  if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
    // computer intersection point
    float intersectionX = x1 + (uA * (x2-x1));
    float intersectionY = y1 + (uA * (y2-y1));
    return new PVector(intersectionX, intersectionY);
  }
  return null;
}

So given a list of curve segments segments and a line line, you could use that method something like this:

// loop over segments and mark the point if intersection is not null
for (float[] seg : segments){
  PVector intersection = lineLine(seg[0], seg[1], seg[2], seg[3], line[0], line[1], line[2], line[3]);
  if(intersection != null){
    ellipse(intersection.x, intersection.y, 20, 20);
    break; 
  }
}
1 Like