
Boost Users : 
Subject: Re: [Boostusers] Enquiry on boost.geometry and intersection()
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 20181116 03:30:21
Hi and welcome,
Zhang Qun Via BoostusersÂ wrote:
> Hi guys, firsttime post here. Hello to everyone.
>
> I'm using boost.geometry to implement a project. I need to calculate
> the intersection between a line and a polygon. I know there is no
> infinite line concept in boost.geometry. So I try to use the segment
> to intersect with a polygon, however, it seems not supported yet. Is
> there any other workaround that I can implement the intersection
> between a line and a polygon? The line is like y=mx+b or axÂ + byÂ + c = 0.
>
Indeed the intersection of segment and polygon does not compile even
though it should. Fortunately you can represent a segment as a
linestring with 2 points. Something like this:
namespace bg = boost::geometry;
typedef bg::model::point<double, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
typedef bg::model::segment<point> segment;
typedef bg::model::linestring<point> linestring;
typedef bg::model::multi_linestring<linestring> multi_linestring;
typedef bg::model::polygon<point> polygon;
// create some polygon, a circle
polygon poly;
for (double a = 0; a < 2 * 3.14; a += 0.1)
Â Â Â bg::append(poly, point(cos(a), sin(a)));
// correct data to match compiletime metadata
// (clockwise, closed polygon)
bg::correct(poly);
// calculate polygon's bounding box which could be used with line
// equation to calculate segment's endpoints
box b = bg::return_envelope<box>(poly);
// here you could calculate a segment represented as linestring
// using min and max coordinates of polygon's envelope
// and line's parameters
// but for now:
linestring ls;
bg::append(ls, point(1, 1));
bg::append(ls, point(1, 1));
// the result of the intersection of linear and areal geometry
// is multilinestring
multi_linestring result;
bg::intersection(ls, poly, result);
std::cout << bg::wkt(ls) << std::endl;
std::cout << bg::wkt(poly) << std::endl;
std::cout << bg::wkt(result) << std::endl;
Regards,
Adam
Boostusers list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net