1

We are trying to reproduce a curve drawn on an HTML5 canvas, X points every 100 MS, but some points get lost in the reproduced copy. We have all the points used for the original curve.

Original curve: http://jsfiddle.net/NWBV4/12/

Reproduced curve: http://jsfiddle.net/NWBV4/15/

In the reproduced curve, if we change SEGMENT_PER_POINTS to greater than the number of points (e.g., 1000), it obviously draws perfectly.

But as you can tell, with smaller numbers, there are missing points in the second curve.

Anyone understand why?

Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

1

The problem is that you're using splice. This removes the points from the array, so after you get 10 (or whatever) points, the last point in those 10 is no longer in your array of points.

This means the next ten points are starting with no connection from the first of their points to the last of the previous points!

kiswa
  • 14,737
  • 1
  • 21
  • 29
  • I made a hacky version too: http://jsfiddle.net/NWBV4/17/ I think yours is a bit cleaner. – kiswa Apr 02 '12 at 19:08
  • @Yoshi, thanks, that did it! Could you post as an answer so we can credit you? Kiswa, thanks for highlighting the missing connection. So stupid of us. The reason we're not crediting you is because the resulting curve has some minor distortions from the original. See our adaptation: http://jsfiddle.net/NWBV4/18/. Unless we did something wrong, which is highly likely. :) – Crashalot Apr 02 '12 at 19:09
  • Credit whoever you like. I thought the question was, "Anyone understand why?" which is the only thing I was trying to answer. – kiswa Apr 02 '12 at 19:11
  • 1
    @Crashalot I won't ;) as kiswas answer is the already the correct answer to the problem. – Yoshi Apr 02 '12 at 19:11
  • Now I must find things Yoshi has answered and provide upvotes. – kiswa Apr 02 '12 at 19:11
  • Are you sure, @Yoshi? Kiswa's answer smartly highlighted a stupid thing we did, but the algorithm contained slight distortions to the original curve. If you insist, we'll award Kiswa the answer. Your call. – Crashalot Apr 02 '12 at 19:14
  • Accept his answer. Though my playing arround with the fiddle may be slightly more to your final solution, still it was kiswa to spot the actual problem. – Yoshi Apr 02 '12 at 19:15
  • @kiswa, thanks for your help! Didn't mean to criticize, only explain why Yoshi should get credit. But we appreciate your help because it put us down the right path. It's only that Yoshi got us there before we had to do anything else. :) – Crashalot Apr 02 '12 at 19:16
  • Done! Thanks to you both @Yoshi and Kiswa! All hail SO. – Crashalot Apr 02 '12 at 19:16