boost geometry intersection give strange results

前端 未结 1 552
耶瑟儿~
耶瑟儿~ 2021-01-21 15:45

I want to use the intersection function from boost geometry with a line and a polygon. I expect that the intersection is the part of the line which lies inside the polygon.

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 16:52

    Make sure your input geometries satisfy the pre-conditions documented.

    You can use bg::correct to fix most issues (such as proper CCW ordering of points in the polygon, closing unclosed polygons etc.):

    Live On Coliru

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    namespace bg = boost::geometry;
    
    using value_type       = double;
    using cs_type          = bg::cs::cartesian;
    using point_type       = bg::model::point;
    using polygon_type     = bg::model::ring;
    using line_string_type = bg::model::linestring;
    using multi_line_type  = bg::model::multi_linestring;
    
    int main()
    {
        line_string_type line;
        line.push_back(point_type{13.37688020921095, 53.66231710654281});
        line.push_back(point_type{13.3857295713429,  53.6636835518369});
        line.push_back(point_type{13.39213495232734, 53.66501934623722});
        line.push_back(point_type{13.39719615524716, 53.66546436809296});
        line.push_back(point_type{13.40724694386097, 53.66240690770171});
        bg::correct(line);
    
        polygon_type polygon;
        polygon.push_back(point_type{13.35, 53.64});
        polygon.push_back(point_type{13.39, 53.64});
        polygon.push_back(point_type{13.39, 53.68});
        polygon.push_back(point_type{13.35, 53.68});
        polygon.push_back(point_type{13.35, 53.64});
        bg::correct(polygon);
    
        multi_line_type intersection;
        bg::intersection(line, polygon, intersection);
    
        std::cout << bg::wkt(intersection);
    }
    

    This prints

    MULTILINESTRING((13.3769 53.6623,13.3857 53.6637,13.39 53.6646))
    

    enter image description here

    With the input un-corrected it would have printed this instead:

    MULTILINESTRING((13.39 53.6646,13.3921 53.665,13.3972 53.6655,13.4072 53.6624))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题