Determine if the point is in the polygon or passed

I am trying to figure out how to do this if I have a vector (a line consisting of 2 points) on a 2d plane, how can I determine if it passed through a polygon?

I know that I can take every line that makes up a polygon and see if they intersect, but is there a better way?

I read this post. How to determine if a 2D point is inside a polygon? which gives me some ideas to find out if a point is within the polygon, but I need to check if it has moved / crossed it.

I am not very interested in technology specification, I will probably implement in python.

Greetings

David

+5
source share
4 answers

If you want to use the python library for geometric operations, take a look shapely. It makes it as easy as someline.intersects(somepolygon).

Here is a quick example of intersections, buffer, and clipping (with a good plot ... I use descartesto easily convert slender polygons to matplotlib patches.).

import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
import descartes

circle = shapely.geometry.Point(5.0, 0.0).buffer(10.0)
clip_poly = shapely.geometry.Polygon([[-9.5, -2], [2, 2], [3, 4], [-1, 3]])
clipped_shape = circle.difference(clip_poly)

line = shapely.geometry.LineString([[-10, -5], [15, 5]])
line2 = shapely.geometry.LineString([[-10, -5], [-5, 0], [2, 3]])

print 'Blue line intersects clipped shape:', line.intersects(clipped_shape)
print 'Green line intersects clipped shape:', line2.intersects(clipped_shape)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(*np.array(line).T, color='blue', linewidth=3, solid_capstyle='round')
ax.plot(*np.array(line2).T, color='green', linewidth=3, solid_capstyle='round')
ax.add_patch(descartes.PolygonPatch(clipped_shape, fc='blue', alpha=0.5))
ax.axis('equal')

plt.show()

This gives:

Blue line intersects clipped shape: True
Green line intersects clipped shape: False

enter image description here

+16
source

A line intersects a polygon if and only if it intersects one of its edges (ignoring for a second the cases when it passes through a vertex). So, in your case, you just need to check all the edges of your polygon on your line and see if there is an intersection.

, (a, b) .

Ax + By + C = 0

Ax + By + C a b. a b , (a, b) .

, , - , ( ), .

+2

, , . , , .

, , wikipedia: http://en.wikipedia.org/wiki/Line-line_intersection

,

function line-polygon_intersection:
   Given points p0, p1 on plane P (your reference line)
   Given points q0..qn on plane P (the polygon)
   foreach ( qi, qi+1 ) pair of adjacent points:
      if line( p0, p1 ) intersects line( qi, qi+1 ):
          return true
   return false

(qn, q0), poly!

!

+1
source

Is there no point in the polygon? Using one, you can determine if one of the points is inside, which can also mean an intersection. If they both lie outside, one of the other methods is still required.

0
source

All Articles