Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bezier intersects Rectangle/Cricle

Discussion in '2D' started by LucaHofmann, Aug 26, 2016.

  1. LucaHofmann

    LucaHofmann

    Joined:
    Feb 24, 2015
    Posts:
    12
    Hello,

    I am drawing a bezier curve using:

    Code (CSharp):
    1. Handles.DrawBezier(startPos, endPos, tan01, tan02, color, null, width)
    I need to know if a given shape intersects this bezier. The shape could be a rectangle or circle.. depends on what is simpler to implement for the intersection logic. The width value can be ignored for the intersection.

    I am searching for something like this:
    Code (CSharp):
    1. bool BezierIntersects(startPos, endPos, tan01, tan02, Rect area, int subdivisions)
    where subdivisions would be the accuracy of the algorithm. (I guess this is needed)

    An alternative would be something like this:
    Code (CSharp):
    1. Vector2 BezierGetPosition(startPos, endPos, tan01, tan02, float time)
    where time is a value between 0 and 1 for the length in the bezier path.

    With the second solution I could loop through the curve and get line segments of it. I could check this line segments with a simple line-rectangle intersection algorithm.

    I always wonder why there is no static class in Unity that offers intersection methods for almost every case. Or did I just missed it?

    Anyway thank you for the help.
     
  2. LucaHofmann

    LucaHofmann

    Joined:
    Feb 24, 2015
    Posts:
    12