0

I researched about Douglas Peucker algorithm. Maybe I can use it as an alternative solution to just free flow my drawing. But my problem is that when I'm drawing, the previous drawn points are also moving. Is there any way to make the drawn lines stationary while drawing within the same collection of points in an array.

Here is the code

ajbee
  • 3,511
  • 3
  • 29
  • 56

1 Answers1

1

The mousemoves event gives you a timestamp (event.timeStamp).

Use that timestamp to calculate the distance moved over time (distance/time==speed): var distance=Math.sqrt((prevX-thisX)*(prevX-thisX)+(prevY-thisY)*(prevY-thisY));

To force the line to an X-axis: If the speed is below your "slow" threshold, just use the previous Y-coordinate instead of the Y supplied by the mouse event.

markE
  • 102,905
  • 11
  • 164
  • 176
  • Can you give an example jsfiddle? i will really appreciate it. – ajbee Oct 31 '15 at 01:48
  • @ThomasM. I'm out of position to do extended answers -- sorry. But the concept is quite simple. Just calculate the speed of the mouse since the last mousemove and then `[ clamp | don't clamp ]` the X coordinate based on whether the speed is `[ slow | not slow ]`. – markE Oct 31 '15 at 03:38
  • No worries. Btw I researched about Douglas Peucker algorithm. Maybe I can use it as an alternative solution to just free flow my drawing. But my problem is that when I'm drawing, the previous drawn points are also moving. Is there any way to make the drawn lines stationary while drawing within the same collection of points in an array.https://gist.github.com/twss/d1a831927c25f8e88f3b – ajbee Oct 31 '15 at 06:55
  • The Douglas Peucker algorithm eliminates "unnecessary" points on a path by comparing 3 sequential points. It checks if the middle point is near the line connecting the outer points. If near, that middle point does not add definition to the path and can be eliminated. As such, it might remove recently drawn points (and therefore move the previously drawn path). That's just the nature of the algorithm. – markE Oct 31 '15 at 16:56
  • is there other algorithm that I can use to minimize the saved points? without altering the previously drawn/saved points – ajbee Nov 01 '15 at 02:37
  • You might want to ask new questions as you think of them. This question has changed from "snap to" to "Douglas Peucker" to "minimize saved points another way". So we are currently waaay off the original topic. ;-) – markE Nov 01 '15 at 02:43
  • My bad. Thanks for the advice. – ajbee Nov 01 '15 at 02:47