geometry: Intersection does not behave as expected

I created a square of width 10, and a flat triangle of height 1 that crosses the square. When calculating the intersection I get the whole plane above the triangle. I would expect to get small triangle back.

example code:

#include<iostream>
#include<boost/geometry.hpp>

using namespace boost::geometry;

int main(){
    using point = model::point<int, 2, cs::cartesian>;
    using poly = model::polygon<point>;

    poly r = {{{0,0}, {0,10}, {10,10}, {10,0},{0,0}}};
    poly cross = {{{-10,2},{5,3},{20,2},{-10,2}}};

    std::vector<poly> out;
    intersection(r, cross, out);

    for(auto x : out)
    {
        std::cout << wkt(x) << std::endl;
    }

}

compiler explorer link: https://compiler-explorer.com/z/GhfhTGz7q

Output: POLYGON((0 2,0 10,10 10,10 2,0 2)) I would expect to get POLYGON((0 2, 5 3, 10 2, 0 2))

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (2 by maintainers)

Most upvoted comments

@barendgehrels your solution is great for my use case, your effort is very much appreciated from my side.

@awulkiew I am working on an applicated that does some kind of integration over a grid, cutting out some piece of a polygon and calculating the volume inside of it. I’d love discuss it further, but not sure if this is the right place. Advice is always appreciated. So yea maybe I should use floating points, could be. Missing a little bit of points due to rounding is not such a big deal, but sometimes you get this kind of behavior that really messes things up for me.