Hunk
Hunk

Reputation: 485

Calculate intersection point between line and polygon with Boost.Geometry

I want the calculation of an intersection point of a line and a polygon, or two lines, etc. How to calculate this point using Boost.Geometry?

Upvotes: 1

Views: 1331

Answers (2)

Silmathoron
Silmathoron

Reputation: 1981

In case someone stumbles upon this like I did, as hinted in this answer, the operation performed by bg::intersection actually depends on the type of object you provide as a result.

Let's define

typedef bg::model::d2::point_xy<double> BPoint;
typedef bg::model::multi_point<BPoint> BMultiPoint;
typedef bg::model::multi_linestring<BLineString> BMultiLineString;

then, running

BMultiPoint mp;
bg::intersection(line, polygon, mp);
bg::intersection(line, other_line, mp);

will return the intersection points between the polygon and the line, then between the line and the other_line.

On the other hand, note that

BMultiLineString mls;
bg::intersection(line, polygon, mls);

Will return the subparts of line where it overlaps with polygon.

Upvotes: 1

Joao Vasconcelos
Joao Vasconcelos

Reputation: 99

although i never used boost library in my uni years we had to make this kind of calculations in a very basic 2d game engine.

I have solved it back then with vectors, i had a vector for position and one for direction of a line, and calculated the colision point with another line based on given information. (using atan2 to know the direction in radians/degrees)

when i did that with an object (a square) i have taken the 4 corners of the object and calculated if the position of the line at given Y positions (being Y values the top and bottom of the square) was within the X area of the square.

i am currently at work so i cant give you the code i used, but this should give you a rough idea of the approach.

i know this is not exactly what you are looking for, but it may be helpful for future reference

Upvotes: 0

Related Questions