简体   繁体   中英

Calculate intersection point between line and polygon with Boost.Geometry

我想计算一条线和一个多边形的交点,或两条线等。如何使用Boost.Geometry计算该点?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM