user3514538 - 4 months ago 25

C++ Question

I'm trying to use Boost.Geometry library to find the intersection of square and line,

`model::ring<model::d2::point_xy<double>> ring { {0, 0}, {2, 0}, {2, 2}, {0, 2} };`

model::polygon<model::d2::point_xy<double>> pol;

pol.inners().push_back (ring);

model::linestring<model::d2::point_xy<double>> line { {1, 3}, {-1, -1} };

model::multi_point<model::d2::point_xy<double>> out;

intersection (pol, line, out); //out returns only {0.5, 2}, but not {0, 1}

but it returns only one point, althougt actually there is two points of intersection

How can I find all the points of intersection?

Answer

Close your ring and put it in the expected order (by default clockwise, see default template parameters):

```
model::ring<model::d2::point_xy<double>> ring {
{0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}
};
```

Your ring was not valid, i.e. not fullfilling the requirements of the specified template arguments.

As per documentation (see under rules) using an invalid geometry as input may give wrong results and validity is neither checked not corrected by the algorithm.

The ring is also not automatically closed on construction or before first use (how should it know you are not going to append more points?). Here is an example construction with repeated closing point.

There are however `is_valid`

and `correct`

to remedy this.

You also probably want to push the points to the exterior ring, i.e. `pol.outer()`

. Your polygon needs to have an exterior ring, interior rings determine holes. You can directly construct the polygon without interior rings:

```
model::polygon<model::d2::point_xy<double>> pol {
{ {0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0} }
};
```