phetsims / kite

A library for creating, manipulating and displaying 2D shapes in JavaScript.
MIT License
12 stars 6 forks source link

Problem creating a loop that starts and ends at the same point using cubicCurveToPoint #87

Closed jbphet closed 3 years ago

jbphet commented 3 years ago

In the number-line-operation sim, I needed a shape where a loop started and ended at the same point on a number line. The idea was to represent an operation with a value of zero. Here's a picture:

image

I was able to get this to work using cubicCurveToPoint, but I needed to add a small "tweak factor" so that the start and end points weren't exactly the same. Otherwise, the shape looked wrong. Here's a screenshot:

image

Is this a bug? It seems like it to me, but maybe there is some mathematical reason why a cubic curve can't start and end in the same place.

I've added a TODO item in the code where this shape is created, please see NumberLineOperationNode, line 508 as of this writing.

jonathanolson commented 3 years ago

Looks like a bug for sure, presumably with the degeneracy detection.

Reproducing Shape turns into 4 line segments:

window.shape = new kite.Shape().moveTo(
  553.2, 309
).cubicCurveTo(
  538.2, 271.5,
  568.2, 271.5,
  553.2, 309
);
jonathanolson commented 3 years ago

I believe the above commit should remove the need for the workaround. Can you verify?

jbphet commented 3 years ago

Yep, I was able to remove the workaround code and the shape looks find now.

@jonathanolson - I think you can probably close this, but I wasn't sure if there was anything else that you wanted to verify. I looked at the commit, and it roughly makes sense to me, but I don't have enough expertise in collinear cubic degeneracy to be able to truly evaluate it.

jonathanolson commented 3 years ago

Sounds good, closing!