线段与多边形的交集

Intersection of segment with polygon

我必须在 MATLAB 中创建一个函数来执行以下任务:

输入:

p 形式的多边形

p = [x1,y1; x2,y2; x3,y3; x4,y4...]

s 具有从 A 到 B 的段的结构

s = struct('A',[x,y],'B'[u,w])

Return:

1) 一个整数,表示线段和多边形之间有多少个交点(例如,0,1,2)

2)从A到B的新线段,其中A是输入线段的第一个交点或起点,B是交点的第二个点或线段输入的最后一个点。

我对如何使用函数 inpolygon 有一个想法。我一直在阅读如何使用这个函数,并且知道要使用它,我应该提供一个查询点和多边形顶点的坐标。它会 return 1 或 0 取决于它是否在里面。

我的问题是,如何获取恰好位于边界内的线段的查询点(在线段与其相交的情况下)?

如果您安装了 Mapping Toolbox,您可以使用 polyxpoly. As this is a rather basic problem, there are quite a few free MATLAB-codes out there on the File Exchange. Here is what I found for the search term 'polygon intersect':

  • 2D Polygon edges intersection 作者:Bruno Luong

    Find the intersection points of the edges of two 2D polygons, a simple function made to follow up a Newsgroup discussion

  • Curve Intersect 2 作者:Sebastian Hölz

    This file is based on the Curve Intersect function by Duane Hanselman. It extends the scope of the function to handle arbitrary lines / polygons, which may also have vertical segments or segments with non-increasing x-values.

  • Curve intersections 来自 NS

    While a few other functions already exist in FEX that compute the intersection points of curves, this short piece of code was written with speed being the highest priority. No loops are used throughout, taking full advantage of MATLAB's vectorization capabilities

  • Fast and Robust Curve Intersections 作者:Douglas Schwarz

    This function computes the (x,y) locations where two curves intersect. The curves can be broken with NaNs or have vertical segments. It is also very fast (at least on data that represents what I think is a typical application).

  • geom2d 作者:David Legland

    [...] derive new shapes: intersection between 2 lines, between a line and a circle, parallel and perpendicular lines