Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cubicBezierToPoints does not consider the symmetry of control points #1

Closed
defpis opened this issue Jul 5, 2021 · 1 comment
Closed

Comments

@defpis
Copy link

defpis commented Jul 5, 2021

Current Behavior

when cubic bezier like this:

const p1 = { x: 0.0, y: 200.0 };
const p2 = { x: 100.0, y: 0.0 };
const p3 = { x: 200.0, y: 400.0 };
const p4 = { x: 300.0, y: 200.0 };

render as follow:

image

Expected Behavior

image

Possible Solution

Add flag first to force recursion for the first time

export const cubicBezierToPoints = (
  bezier: CubicBezier,
  splitThreshold: number
): Array<Point> => {
  const points = [
    { x: bezier.p1.x, y: bezier.p1.y },
    { x: bezier.p4.x, y: bezier.p4.y },
  ];

  const cubicBezierSplit = (
    bezier: CubicBezier,
    min: number,
    max: number,
    insertIndex: number,
    first = false
  ) => {
    const time = lerp(min, max, 0.5);
    const midLerp = lerp2(bezier.p2, bezier.p3, time);

    const pointOnCurve = lerp2(
      lerp2(lerp2(bezier.p1, bezier.p2, time), midLerp, time),
      lerp2(midLerp, lerp2(bezier.p3, bezier.p4, time), time),
      time
    );

    const prevPoint = points[insertIndex - 1];
    const nextPoint = points[insertIndex];

    points.splice(insertIndex, 0, pointOnCurve);

    if (
      dot(
        normalize(sub(prevPoint, pointOnCurve)),
        normalize(sub(nextPoint, pointOnCurve))
      ) > splitThreshold ||
      first
    ) {
      cubicBezierSplit(bezier, time, max, insertIndex + 1);
      cubicBezierSplit(bezier, min, time, insertIndex);
    }
  };

  cubicBezierSplit(bezier, 0, 1, 1, true);

  return points;
};
@andrewlowndes
Copy link
Owner

Closing now #2 has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants