Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

intersect

polyshape 对象的交集

说明

示例

polyout = intersect(poly1,poly2) 返回一个 polyshape 对象,它的区域是两个 polyshape 对象的几何交集。交集包含 poly1poly2 的重叠区域。poly1poly2 必须具有兼容的数组大小。

示例

polyout = intersect(polyvec) 返回一个 polyshape 对象,它的区域是向量 polyvec 中所有 polyshape 对象的交集。交集包含 polyvec 中所有 polyshape 对象的重叠区域。

示例

[polyout,shapeID,vertexID] = intersect(poly1,poly2) 还会返回从 polyout 中的顶点到 poly1poly2 中的顶点的顶点映射信息。仅当 poly1poly2 是标量 polyshape 对象时,intersect 函数才支持此语法。

shapeID 元素标识 polyout 中对应的顶点是源于 poly1poly2,还是因相交而产生的。vertexIDpolyout 的顶点映射到 poly1poly2 或交集的顶点。

[polyout,shapeID,vertexID] = intersect(polyvec) 返回从 polyoutpolyshape 对象向量 polyvec 的每个元素的顶点映射信息。

___ = intersect(___,'KeepCollinearPoints',TF) 指定在上述任一语法的基础上是保留还是删除 polyout 中的共线点。

示例

[in,out] = intersect(poly1,lineseg) 返回 lineseg 位于 poly1 内部和外部的线段。矩阵 lineseg 包含两列。第一列定义线段的 x 坐标,第二列定义对应的 y 坐标。

仅当 poly1 是标量 polyshapelineseg 不包含自相交时,intersect 才支持此语法。

示例

全部折叠

创建并绘制两个多边形。

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
poly2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
plot(poly1)
hold on
plot(poly2)

Figure contains an axes object. The axes object contains 2 objects of type polygon.

figure

计算并绘制 poly1poly2 的交集。

polyout = intersect(poly1,poly2)
polyout = 
  polyshape with properties:

      Vertices: [4x2 double]
    NumRegions: 1
      NumHoles: 0

plot(polyout)
xlim([-0.2 1.4]);
ylim([-0.2 1.2]);

Figure contains an axes object. The axes object contains an object of type polygon.

创建包含两个多边形的向量。

polyarray1 = polyshape([0 0 1 1],[1 0 0 1]);
polyarray2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
poly1 = [polyarray1 polyarray2]
poly1 = 
  1x2 polyshape array with properties:

    Vertices
    NumRegions
    NumHoles

plot(poly1(1))
hold on
plot(poly1(2))

Figure contains an axes object. The axes object contains 2 objects of type polygon.

figure

计算 poly1 的元素的交集。

polyout = intersect(poly1)
polyout = 
  polyshape with properties:

      Vertices: [4x2 double]
    NumRegions: 1
      NumHoles: 0

plot(polyout)
xlim([-0.2 1.4]);
ylim([-0.2 1.2]);

Figure contains an axes object. The axes object contains an object of type polygon.

创建两个多边形并计算其交集。显示交集的顶点坐标和对应的顶点映射信息。

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
poly2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
[polyout,shapeID,vertexID] = intersect(poly1,poly2);

[polyout.Vertices shapeID vertexID]
ans = 4×4

    0.7500    0.2500    2.0000    1.0000
    0.7500    0.7500    2.0000    2.0000
    1.0000    0.7500         0         0
    1.0000    0.2500         0         0

交集的前两个顶点源于 poly2,因为 shapeID 中的对应值为 2。这些顶点分别是属性 poly2.Vertices 中的第一个和第二个顶点,因为它们在 vertexID 中的对应值是 1 和 2。polyout 的后两个顶点是因相交而产生的,因为 shapeIDvertexID 中的对应值为 0。

创建一个矩形多边形和一个线段。

poly1 = polyshape([0 0 1 1],[1 0 0 1]);
lineseg = [0.5 0.5; 1.5 1.5];

计算该多边形与线段的交集,并确定线段的哪些部分在多边形的内部,哪些在其外部。

[in,out] = intersect(poly1,lineseg);
plot(poly1)
hold on
plot(in(:,1),in(:,2),'b',out(:,1),out(:,2),'r')
legend('Polygon','Inside','Outside','Location','NorthWest')

Figure contains an axes object. The axes object contains 3 objects of type polygon, line. These objects represent Polygon, Inside, Outside.

输入参数

全部折叠

第一个输入 polyshape,指定为标量、向量、矩阵或多维数组。

数据类型: polyshape

第二个输入 polyshape,指定为标量、向量、矩阵或多维数组。

数据类型: polyshape

polyshape 向量。

数据类型: polyshape

线段坐标,指定为两列矩阵。第一列定义线段的 x 坐标,第二列定义 y 坐标。lineseg 必须至少有两行且不包含自相交。

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

共线顶点指示符,指定为 falsetrue

  • false - 删除共线点,使输出 polyshape 包含定义边界所需的最少顶点。

  • true - 保留所有共线点作为顶点。

如果未指定 'KeepCollinearPoints' 参数,则根据创建输入 polyshape 对象时使用的值为该参数赋值:

  • 如果每个输入 polyshape 在创建时的值为 true,则输出 polyshape 的值设置为 true

  • 如果每个输入 polyshape 在创建时的值为 false,则输出 polyshape 的值设置为 false

  • 如果输入 polyshape 对象的值不匹配,则输出 polyshape 的值设置为 false

数据类型: logical

输出参数

全部折叠

输出 polyshape,以标量、向量、矩阵或多维数组形式返回。

  • 如果输入两个 polyshape 参数,则它们必须具有兼容的大小。例如,如果两个输入 polyshape 向量具有不同的长度 M 和 N,则它们必须有不同的方向(一个必须是行向量,一个必须是列向量)。polyout 则是 M×N 或 N×M,具体取决于每个输入向量的方向。有关兼容的数组大小的详细信息,请参阅基本运算的兼容数组大小

  • 如果您提供单个输入参数 polyvec,则 polyout 是一个标量 polyshape 对象。

形状 ID,以列向量形式返回,其各个元素表示交集中顶点的来源。

  • shapeID 的长度等于输出 polyshapeVertices 属性中的行数。

  • shapeID 的元素取决于输入参数的数量:

    • 如果您提供两个输入参数 poly1poly2,则它们必须是标量 polyshape 对象。如果输出 polyshape 的顶点是因相交而产生的,则 shapeID 中对应元素的值为 0。当顶点源于 poly1 时,对应元素为 1,当顶点源于 poly2 时,对应元素为 2。

    • 如果您提供一个输入参数 polyvecpolyshape 对象向量),则 shapeID 包含作为输出顶点来源的对应 polyvec 元素的索引。如果顶点是因相交而产生的,则对应元素的值为 0。

数据类型: double

顶点 ID,以列向量形式返回,其元素将输出 polyshape 中的顶点映射到来源的 polyshape 中的顶点。vertexID 的元素包含输入 polyshapeVertices 属性中对应顶点的行号。如果输出 polyshape 的顶点是因相交而产生的,则对应元素为 0。

vertexID 的长度等于输出 polyshapeVertices 属性中的行数。如果您提供两个输入 polyshape 对象,则 intersect 仅在它们都是标量时才支持此输出参数。

数据类型: double

内部线段坐标,以两列矩阵形式返回。in 的第一列包含位于输入 polyshape 内部的线段的 x 坐标,第二列包含对应的 y 坐标。

数据类型: double

外部线段坐标,以两列矩阵形式返回。out 的第一列包含位于输入 polyshape 外部的线段的 x 坐标,第二列包含对应的 y 坐标。

数据类型: double

扩展功能

基于线程的环境
使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

版本历史记录

在 R2017b 中推出

另请参阅

|