# How to tell if a line intersects a polygon not only at endpoints?

How do I do this in .net topology suite or jts? Please see image.

Any other library that can I run on .NET is fine, or even a general algorithm if there is no option.

For example this answer is only for endpoints: Check if line crosses a polygon

Right now I'm randomly experimenting and if i intersect and check

if (intersection.Boundary.ToString().Contains("EMPTY"))

it seems to work, but... yeah no idea why or if it's all cases.

• You need to utilize the DE-9IM matrix. The post you already linked touches on it, but you can read more here: en.wikipedia.org/wiki/DE-9IM Jun 12, 2015 at 12:50
• Ok Ill start reading it.
– Edza
Jun 12, 2015 at 12:52
• You should clarify if you mean that endpoints must also touch the boundary of the polygon because Intersects does not require that. Perhaps you are after "Covers" as in this blog lin-ear-th-inking.blogspot.fi/2007/06/…. Jun 12, 2015 at 14:13
• End points do "touch", as they are the same points that define the polygon.
– Edza
Jun 12, 2015 at 14:21
• Depending on your exact scenario, you needn't utilise most of the complex interaction methods as you can probably achieve the same result with a couple of quick tests. @Edza, just to clarify, is the line always straight?
– Guy
Jun 15, 2015 at 9:45

• intersects with the interior of the polygon
• does not just touch the boundary of the polygon

Just combine the requirements. This is the method using JTS that should answer your question:

``````public static boolean lineReallyIntersectsPolygon(LineString ls, Polygon p){
if(ls.intersects(p) && !ls.touches(p)){
return true;
} else {
return false;
}
}
``````
• A year late :D. I think I ended up using the hack I mentioned: intersection.Boundary.ToString().Contains("EMPTY")). Maybe someone else will find this useful.
– Edza
Jul 13, 2016 at 19:02