How to detect linerenderer overlap with itself

I am drawing linerenderer and want to find out when it crosses itself.

This is a problem that may seem simple, but has enough edge cases that it can be complex (someone drawing circles or near-circles for example). Let’s start with a simple case though, and can get more complex. ASSUMING: Line is drawn such that there are no loop-backs, this means: The X values of all points will be in ascending or descending order (This is something you can check for and reject the line if not true).

For each object you want to classify:

  • Find the X,Y position of the object
  • Find the two points on the line that have X values immediately to the left and right of the object (We’ll call them reference points)
  • The Y values of those reference points are hopefully both be greater than, or less than, your object. This will tell you if the object is above/below the line
  • If one Y value is above, and the other is below, it gets a little trickier. You can use the two reference points to define a line, get the line’s Y value at object’s X position, and see if that’s above/below the object’s Y position.