JavaScript recursion: Maximum call stack size exceeded

前端 未结 4 804
醉话见心
醉话见心 2021-02-13 02:29

I have a recursive function for moving some circles on a canvas. Overed circle is enlarged (zoom in) and all the other circles is pushed away. Pushed circles push other circles

4条回答
  •  旧巷少年郎
    2021-02-13 02:53

    It doesn't surprise this overflows because the algorithm grows the stack as it iterates but the exit condition is unpredictable, actions are not tightly localized (they have knock-on effects to nearby circles), so processing time will be chaotic.

    I would reconsider the algorithm. Consider finding the two closest circles, if these are further apart than a given threshold apart, abort. Otherwise move them apart a little and repeat.

提交回复
热议问题