Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A C algorithm for finding the intersection points of two 2D quads?

I have a quad type which is defined as:

typedef struct __point {
    float x;
    float y;
} point_t;

typedef struct __quad {
    point_t p1;
    point_t p2;
    point_t p3;
    point_t p4;
} quad_t;

If I have two of those quads on the same plane, I would like to be able to work out the intersection points of those quads. For example, if we have quad A and quad B, if any of B's points fall outside of A, the algoritm should yield a quad with points as shown in the illustration below (A is in red, B is in purple):

Example

Edit: Ordering of the points is not important because I will later use those points to construct a quad that is going to be drawn inside A.

like image 1000
Kristina Brooks Avatar asked Nov 13 '22 23:11

Kristina Brooks


1 Answers

If the only reason to do this is to draw the resulting polygon, why not use the GPU to do the work for you - you're using OpenGL after all. So instead of messing about working out how to construct the intersection, do the following:-

Set Z values of Polygon A and Polygon B to some constant

Set Z test to no testing (always write Z regardless)

Disable Z test
Enable Z writes
Disable colour writes
Render Polygon A

Set Z test to z equal

Enable Z test
Disable Z write
Enable colour write
Render Polygon B

Hey presto, the intersection polygon!

You could probably make this far more efficient if you restricted yourself to OpenGL 4 and used the various shaders that are available.

like image 150
Skizz Avatar answered Nov 15 '22 12:11

Skizz