【附代码】判断线段是否相交算法(Python,C++)

【附代码】判断线段是否相交算法(Python,C++)


作者:小猪快跑

基础数学&计算数学,从事优化领域5年+,主要研究方向:MIP求解器、整数规划、随机规划、智能优化算法

如有错误,欢迎指正。如有更好的算法,也欢迎交流!!!——@小猪快跑

相关文献

测试电脑配置

博主三千元电脑的渣渣配置:

CPU model: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics, instruction set [SSE2|AVX|AVX2|AVX512]
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads

基础

在这里插入图片描述

这里假设:
O A → = a ⃗ = ( x a , y a ) O B → = b ⃗ = ( x b , y b ) O C → = p ⃗ = ( x c , y c ) C B → = w ⃗ ∠ A O B = α \overrightarrow{OA} = \vec{a} = (x_a,y_a) \\ \overrightarrow{OB} = \vec{b} = (x_b,y_b) \\ \overrightarrow{OC} = \vec{p} = (x_c,y_c) \\ \overrightarrow{CB} = \vec{w} \\ ∠AOB = \alpha OA =a =(xa,ya)OB =b =(xb,yb)OC =p =(xc,yc)CB =w AOB=α

向量旋转

任意向量都能表示成:
( r cos ⁡ α r sin ⁡ α ) \left ( \begin{matrix} r\cos{\alpha} \\ r\sin{\alpha} \\ \end{matrix} \right ) (rcosαrsinα)
假设向量逆时针旋转了 β \beta β,那么我们容易知道旋转后向量是:
( r cos ⁡ ( α + β ) r sin ⁡ ( α + β ) ) \left ( \begin{matrix} r\cos({\alpha + \beta}) \\ r\sin({\alpha + \beta}) \\ \end{matrix} \right ) (rcos(α+β)rsin(α+β))
那么容易得到:
( r cos ⁡ ( α + β ) r sin ⁡ ( α + β ) ) = ( cos ⁡ α − sin ⁡ α sin ⁡ α cos ⁡ α ) ( r cos ⁡ α r sin ⁡ α ) \left ( \begin{matrix} r\cos({\alpha + \beta}) \\ r\sin({\alpha + \beta}) \\ \end{matrix} \right )= \left ( \begin{array}{rr} \cos{\alpha} & -\sin{\alpha} \\ \sin{\alpha} & \cos{\alpha} \\ \end{array} \right ) \left ( \begin{matrix} r\cos{\alpha} \\ r\sin{\alpha} \\ \end{matrix} \right ) (rcos(α+β)rsin(α+β))=(cosαsinαsinαcosα)(rcosαrsinα)
于是旋转向量就是:
( cos ⁡ α − sin ⁡ α sin ⁡ α cos ⁡ α ) \left ( \begin{array}{rr} \cos{\alpha} & -\sin{\alpha} \\ \sin{\alpha} & \cos{\alpha} \\ \end{array} \right ) (cosαsinαsinαcosα)

向量缩放

( r 0 0 r ) \left ( \begin{array}{rr} r & 0 \\ 0 & r \\ \end{array} \right ) (r00r)

向量投影

推导

主要利用 O C → \overrightarrow{OC} OC C B → \overrightarrow{CB} CB 垂直,点积为0:
w ⃗ = b ⃗ − p ⃗ w ⃗ ⋅ p ⃗ = 0 } ⇒ ( b ⃗ − p ⃗ ) ⋅ p ⃗ = 0 p ⃗ = k a ⃗ } ⇒ ( b ⃗ − k a ⃗ ) ⋅ k a ⃗ = 0 p ⃗ = k a ⃗ } ⇒ p ⃗ = a ⃗ ⋅ b ⃗ a ⃗ ⋅ a ⃗ a ⃗ \left. \begin{array}{r} \left. \begin{array}{l} \vec{w}=\vec{b}-\vec{p} \\ \vec{w} \cdot \vec{p} = 0 \end{array} \right\} \Rightarrow (\vec{b}-\vec{p}) \cdot \vec{p} = 0\\ \vec{p} = k \vec{a} \end{array} \right\} \Rightarrow \left. \begin{array}{r} (\vec{b}-k \vec{a}) \cdot k \vec{a} = 0 \\ \vec{p} = k \vec{a} \end{array} \right\} \Rightarrow \vec{p} = \frac{\vec{a} \cdot \vec{b}}{\vec{a} \cdot \vec{a}} \vec{a} w =b p w p =0}(b p )p =0p =ka (b ka )ka =0p =ka }p =a a a b a
那么投影矩阵
P b ⃗ = p ⃗ = a ⃗ a ⃗ ⋅ b ⃗ a ⃗ ⋅ a ⃗ ⇒ P = a ⃗ a ⃗ T a ⃗ T a ⃗ \begin{array}{l} & P\vec{b} = \vec{p} = \vec{a} \frac{\vec{a} \cdot \vec{b}}{\vec{a} \cdot \vec{a}} \\ \Rightarrow & P = \frac{\vec{a}\vec{a}^T}{\vec{a}^T\vec{a}} \end{array} Pb =p =a a a a b P=a Ta a a T

点乘

点乘(Dot Product)的结果是点积,又称数量积标量积(Scalar Product)

定义

a ⃗ ⋅ b ⃗ = ∣ a ⃗ ∣ ∣ b ⃗ ∣ cos ⁡ α \vec{a} \cdot \vec{b} = |\vec{a}||\vec{b}|\cos{\alpha} a b =a ∣∣b cosα

推导

那么如何用解析几何来表示呢?

我们其实可以把 a ⃗ \vec{a} a 旋转 α \alpha α 再缩放 ∣ b ⃗ ∣ / ∣ a ⃗ ∣ |\vec{b}|/|\vec{a}| b ∣/∣a 倍,就是 b ⃗ \vec{b} b 了:
( ∣ b ⃗ ∣ ∣ a ⃗ ∣ 0 0 ∣ b ⃗ ∣ ∣ a ⃗ ∣ ) ( cos ⁡ α − sin ⁡ α sin ⁡ α cos ⁡ α ) ( x a y a ) = ( x b y b ) ⇒ ( ( x a cos ⁡ α − y a sin ⁡ α ) ∣ b ⃗ ∣ ( x a sin ⁡ α + y a cos ⁡ α ) ∣ b ⃗ ∣ ) = ( x b ∣ a ⃗ ∣ y b ∣ a ⃗ ∣ ) ⇒ ∣ a ⃗ ∣ ∣ b ⃗ ∣ cos ⁡ α = x a x b + y a y b \begin{array}{l} &\left ( \begin{matrix} \frac{|\vec{b}|}{|\vec{a}|} & 0 \\ 0 & \frac{|\vec{b}|}{|\vec{a}|} \\ \end{matrix} \right ) \left ( \begin{array}{rr} \cos{\alpha} & -\sin{\alpha} \\ \sin{\alpha} & \cos{\alpha} \\ \end{array} \right ) \left ( \begin{matrix} x_a \\ y_a \\ \end{matrix} \right )= \left ( \begin{matrix} x_b \\ y_b \\ \end{matrix} \right ) \\ \Rightarrow & \left ( \begin{matrix} (x_a\cos{\alpha} - y_a\sin{\alpha})|\vec{b}| \\ (x_a\sin{\alpha} + y_a\cos{\alpha})|\vec{b}| \\ \end{matrix} \right )= \left ( \begin{matrix} x_b|\vec{a}| \\ y_b|\vec{a}| \\ \end{matrix} \right ) \\ \Rightarrow & |\vec{a}||\vec{b}|\cos{\alpha} = x_a x_b + y_a y_b \end{array} a b 00a b (cosαsinαsinαcosα)(xaya)=(xbyb)((xacosαyasinα)b (xasinα+yacosα)b )=(xba yba )a ∣∣b cosα=xaxb+yayb

几何意义

点乘的结果表示 a ⃗ \vec{a} a b ⃗ \vec{b} b 方向上的投影 b ⃗ \vec{b} b 的乘积,反映了两个向量在方向上的相似度,结果越大越相似。基于结果可以判断这两个向量是否是同一方向,是否正交垂直,具体对应关系为:

  1. a ⃗ ⋅ b ⃗ > 0 \vec{a} \cdot \vec{b} > 0 a b >0 则方向基本相同,夹角在0°到90°之间
  2. a ⃗ ⋅ b ⃗ = 0 \vec{a} \cdot \vec{b} = 0 a b =0 则正交,相互垂直
  3. a ⃗ ⋅ b ⃗ < 0 \vec{a} \cdot \vec{b} < 0 a b <0 则方向基本相反,夹角在90°到180°之间

叉乘

叉乘(Cross Product)又称向量积(Vector Product)。

定义

a ⃗ × b ⃗ = ∣ a ⃗ ∣ ∣ b ⃗ ∣ sin ⁡ α \vec{a} \times \vec{b} = |\vec{a}||\vec{b}|\sin{\alpha} a ×b =a ∣∣b sinα

推导

那么如何用解析几何来表示呢?

我们其实可以把 a ⃗ \vec{a} a 旋转 α \alpha α 再缩放 ∣ b ⃗ ∣ / ∣ a ⃗ ∣ |\vec{b}|/|\vec{a}| b ∣/∣a 倍,就是 b ⃗ \vec{b} b 了:
( ∣ b ⃗ ∣ ∣ a ⃗ ∣ 0 0 ∣ b ⃗ ∣ ∣ a ⃗ ∣ ) ( cos ⁡ α − sin ⁡ α sin ⁡ α cos ⁡ α ) ( x a y a ) = ( x b y b ) ⇒ ( ( x a cos ⁡ α − y a sin ⁡ α ) ∣ b ⃗ ∣ ( x a sin ⁡ α + y a cos ⁡ α ) ∣ b ⃗ ∣ ) = ( x b ∣ a ⃗ ∣ y b ∣ a ⃗ ∣ ) ⇒ ∣ a ⃗ ∣ ∣ b ⃗ ∣ sin ⁡ α = x a y b − x b y a \begin{array}{l} & \left ( \begin{matrix} \frac{|\vec{b}|}{|\vec{a}|} & 0 \\ 0 & \frac{|\vec{b}|}{|\vec{a}|} \\ \end{matrix} \right ) \left ( \begin{array}{rr} \cos{\alpha} & -\sin{\alpha} \\ \sin{\alpha} & \cos{\alpha} \\ \end{array} \right ) \left ( \begin{matrix} x_a \\ y_a \\ \end{matrix} \right )= \left ( \begin{matrix} x_b \\ y_b \\ \end{matrix} \right ) \\ \Rightarrow & \left ( \begin{matrix} (x_a\cos{\alpha} - y_a\sin{\alpha})|\vec{b}| \\ (x_a\sin{\alpha} + y_a\cos{\alpha})|\vec{b}| \\ \end{matrix} \right )= \left ( \begin{matrix} x_b|\vec{a}| \\ y_b|\vec{a}| \\ \end{matrix} \right ) \\ \Rightarrow & |\vec{a}||\vec{b}|\sin{\alpha} = x_a y_b - x_b y_a \end{array} a b 00a b (cosαsinαsinαcosα)(xaya)=(xbyb)((xacosαyasinα)b (xasinα+yacosα)b )=(xba yba )a ∣∣b sinα=xaybxbya

几何意义

如果以向量 a ⃗ \vec{a} a b ⃗ \vec{b} b 为边构成一个平行四边形,那么这两个向量外积的模长与这个平行四边形的面积相等。

判断线段是否相交

在这里插入图片描述

我们有了上面的基础后,其实思路就一下打开了!

其实我们只要想着 A B → \overrightarrow{AB} AB 的两边是 C C C D D D ,那么也就是说 A B → × A D → \overrightarrow{AB} \times \overrightarrow{AD} AB ×AD A B → × A C → \overrightarrow{AB} \times \overrightarrow{AC} AB ×AC 有正有负,同时呢 C D → × C A → \overrightarrow{CD} \times \overrightarrow{CA} CD ×CA C D → × C B → \overrightarrow{CD} \times \overrightarrow{CB} CD ×CB 有正有负(这里要注意一下叉乘可能为0的情况,比如说 A A A C D → \overrightarrow{CD} CD 上)。这里我们有正有负采用直接判断而不是相乘小于零,这是因为相乘可能存在数值溢出等问题。而且一般的,和零的判断比乘法快很多。

我们直接上测试用例看看效果!!!
在这里插入图片描述

代码

C++
#include <iostream>
#include <chrono>

using namespace std;

int cross_product(int x1, int y1, int x2, int y2) {
    // 计算向量 (x1, y1) 和向量 (x2, y2) 的叉积
    return x1 * y2 - x2 * y1;
}

int dot_product(int x1, int y1, int x2, int y2) {
    // 计算向量 (x1, y1) 和向量 (x2, y2) 的点乘
    return x1 * x2 + y1 * y2;
}

bool is_intersected(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
    /*
    判断线段 (x1, y1)-(x2, y2) 和线段 (x3, y3)-(x4, y4) 是否相交
    AB×AC
    AB×AD
    CD×CA
    CD×CB
     */

    if ((max(x1, x2) < min(x3, x4)) or (max(x3, x4) < min(x1, x2)) or (max(y1, y2) < min(y3, y4)) or (
            max(y3, y4) < min(y1, y2))) {
        return false;
    }

    int abx = x2 - x1;
    int aby = y2 - y1;
    int acx = x3 - x1;
    int acy = y3 - y1;
    int adx = x4 - x1;
    int ady = y4 - y1;
    int bcx = x3 - x2;
    int bcy = y3 - y2;
    int cdx = x4 - x3;
    int cdy = y4 - y3;
    int cp1 = cross_product(abx, aby, acx, acy);
    int cp2 = cross_product(abx, aby, adx, ady);
    int cp3 = cross_product(cdx, cdy, -acx, -acy);
    int cp4 = cross_product(cdx, cdy, -bcx, -bcy);
    // 如果两个叉积的乘积小于0,则两个向量在向量 (x1, y1)-(x2, y2) 的两侧,即线段相交
    if (((cp1 > 0 and 0 > cp2) or (cp1 < 0 and 0 < cp2) or cp1 == 0 or cp2 == 0) and
        ((cp3 > 0 and 0 > cp4) or (cp3 < 0 and 0 < cp4) or cp3 == 0 or cp4 == 0)) {
        return true;
    }
    return false;
}

int test(int n) {
    int res = 0;
    for (auto x1 = 0; x1 < n; x1++) {
        for (auto y1 = 0; y1 < n; y1++) {
            for (auto x2 = 0; x2 < n; x2++) {
                for (auto y2 = 0; y2 < n; y2++) {
                    if (x1 == x2 and y1 == y2) {
                        continue;
                    }
                    for (auto x3 = 0; x3 < n; x3++) {
                        for (auto y3 = 0; y3 < n; y3++) {
                            for (auto x4 = 0; x4 < n; x4++) {
                                for (auto y4 = 0; y4 < n; y4++) {
                                    if (x3 == x4 and y3 == y4) {
                                        continue;
                                    }
                                    res += is_intersected(x1, y1, x2, y2, x3, y3, x4, y4);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return res;
}

int main() {
    auto start = std::chrono::high_resolution_clock::now();

    std::cout << test(7) << std::endl;

    auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = finish - start;
    std::cout << "Elapsed time: " << elapsed.count() << " s\n" << std::endl;

    return 0;
}
Python
from time import time
import math
from numba import njit


@njit
def cross_product(x1, y1, x2, y2):
    """
    计算向量 (x1, y1) 和向量 (x2, y2) 的叉积
    """
    return x1 * y2 - x2 * y1


@njit
def dot_product(x1, y1, x2, y2):
    """
    计算向量 (x1, y1) 和向量 (x2, y2) 的点乘
    """
    return x1 * x2 + y1 * y2


@njit
def is_intersected(x1, y1, x2, y2, x3, y3, x4, y4):
    """
    判断线段 (x1, y1)-(x2, y2) 和线段 (x3, y3)-(x4, y4) 是否相交
    AB×AC
    AB×AD
    CD×CA
    CD×CB
    """
    if (max(x1, x2) < min(x3, x4)) or (max(x3, x4) < min(x1, x2)) or (max(y1, y2) < min(y3, y4)) or (
            max(y3, y4) < min(y1, y2)):
        return False
    abx = x2 - x1
    aby = y2 - y1
    acx = x3 - x1
    acy = y3 - y1
    adx = x4 - x1
    ady = y4 - y1
    bcx = x3 - x2
    bcy = y3 - y2
    cdx = x4 - x3
    cdy = y4 - y3
    cp1 = cross_product(abx, aby, acx, acy)
    cp2 = cross_product(abx, aby, adx, ady)
    cp3 = cross_product(cdx, cdy, -acx, -acy)
    cp4 = cross_product(cdx, cdy, -bcx, -bcy)
    # 如果两个叉积的乘积小于0,则两个向量在向量 (x1, y1)-(x2, y2) 的两侧,即线段相交
    if ((cp1 > 0 > cp2) or (cp1 < 0 < cp2) or cp1 == 0 or cp2 == 0) and (
            (cp3 > 0 > cp4) or (cp3 < 0 < cp4) or cp3 == 0 or cp4 == 0):
        return True
    return False


def test(n):
    res = 0
    for x1 in range(n):
        for y1 in range(n):
            for x2 in range(n):
                for y2 in range(n):
                    if x1 == x2 and y1 == y2:
                        continue
                    for x3 in range(n):
                        for y3 in range(n):
                            for x4 in range(n):
                                for y4 in range(n):
                                    if x3 == x4 and y3 == y4:
                                        continue
                                    res += is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)
    return res


if __name__ == '__main__':
    s = time()
    print(test(7))
    print(time() - s)

画图代码

# figures.py
from math import sqrt
from shapely import affinity

GM = (sqrt(5) - 1.0) / 2.0
W = 8.0
H = W * GM
SIZE = (W, H)

BLUE = '#6699cc'
GRAY = '#999999'
DARKGRAY = '#333333'
YELLOW = '#ffcc33'
GREEN = '#339933'
RED = '#ff3333'
BLACK = '#000000'


def add_origin(ax, geom, origin):
    x, y = xy = affinity.interpret_origin(geom, origin, 2)
    ax.plot(x, y, 'o', color=GRAY, zorder=1)
    ax.annotate(str(xy), xy=xy, ha='center', textcoords='offset points', xytext=(0, 8))


def set_limits(ax, x0, xN, y0, yN):
    ax.set_xlim(x0, xN)
    ax.set_xticks(range(x0, xN + 1))
    ax.set_ylim(y0, yN)
    ax.set_yticks(range(y0, yN + 1))
    ax.set_aspect("equal")
# main.py
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString, Polygon
from shapely.plotting import plot_polygon, plot_points, plot_line

from csdn_line_intersect import is_intersected
from figures import BLUE, GRAY, set_limits

fig = plt.figure(1, figsize=(9, 9), dpi=300)
fig.subplots_adjust(wspace=0.5, hspace=0.5)  # 调整边距和子图的间距

ax = fig.add_subplot(4, 4, 1)
x1, y1, x2, y2 = 1, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 3, 2
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 2)
x1, y1, x2, y2 = 1, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 3, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 3)
x1, y1, x2, y2 = 1, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 2, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 4)
x1, y1, x2, y2 = 1, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 5)
x1, y1, x2, y2 = 1, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 1, 2
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 6)
x1, y1, x2, y2 = 2, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 1, 2
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 7)
x1, y1, x2, y2 = 2, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 8)
x1, y1, x2, y2 = 2, 2, 3, 1
x3, y3, x4, y4 = 1, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 9)
x1, y1, x2, y2 = 1, 2, 3, 1
x3, y3, x4, y4 = 1, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 10)
x1, y1, x2, y2 = 1, 2, 3, 2
x3, y3, x4, y4 = 1, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 11)
x1, y1, x2, y2 = 2, 2, 3, 2
x3, y3, x4, y4 = 1, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 12)
x1, y1, x2, y2 = 2, 2, 3, 2
x3, y3, x4, y4 = 2, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 13)
x1, y1, x2, y2 = 2, 2, 3, 2
x3, y3, x4, y4 = 3, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 14)
x1, y1, x2, y2 = 1, 2, 3, 2
x3, y3, x4, y4 = 3, 3, 1, 1
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 15)
x1, y1, x2, y2 = 2, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 3, 3
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

ax = fig.add_subplot(4, 4, 16)
x1, y1, x2, y2 = 1, 1, 3, 1
x3, y3, x4, y4 = 1, 3, 3, 3
a = LineString([(x1, y1), (x2, y2)])
b = LineString([(x3, y3), (x4, y4)])
plot_line(a, ax=ax, color=GRAY)
plot_line(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'is_intersected:{is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)}')
set_limits(ax, 0, 4, 0, 4)

plt.savefig('output.png')
plt.show()

测试结果

C++: 0.0157648 s
Python(numba): 1.3376786708831787 s
Python(no numba): 3.585803985595703 s
Python(shapely): 73.45080494880676 s
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 首先,我们需要分别获取这两条线段的端点坐标。假设这两条线段分别为线段A和线段B,则线段A有两个端点坐标(x1,y1)和(x2,y2),线段B有两个端点坐标(x3,y3)和(x4,y4)。 然后,我们可以使用叉积来判断两条线段是否相交。我们可以计算出线段A的向量(x2-x1,y2-y1)和线段B的向量(x4-x3,y4-y3)的叉积。如果两条线段相交,则这两个向量的叉积一定不为零。 具体代码如下: ``` bool isIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double v1 = (x4-x3)*(y1-y3) - (y4-y3)*(x1-x3); double v2 = (x4-x3)*(y2-y3) - (y4-y3)*(x2-x3); double v3 = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1); double v4 = (x2-x1)*(y4-y1) - (y2-y1)*(x4-x1); return (v1*v2 < 0) && (v3*v4 < 0); } ``` 这段代码中,函数isIntersect()接收两条线段的端点坐标作为参数,并返回一个布尔值,表示这两条线段是否相交。 ### 回答2: 要判断两条线段是否相交可以使用QGIS中的C++代码。以下是一个简单的示例代码: ```cpp #include <qgsgeometry.h> bool areLinesIntersecting(const QgsPointXY& p1, const QgsPointXY& p2, const QgsPointXY& p3, const QgsPointXY& p4) { QgsGeometry g1(QgsWkbTypes::LineString); QgsGeometry g2(QgsWkbTypes::LineString); QgsPointXY points[] = { p1, p2 }; g1.addPart(QgsLineString(points, sizeof(points) / sizeof(points[0]))); QgsPointXY points2[] = { p3, p4 }; g2.addPart(QgsLineString(points2, sizeof(points2) / sizeof(points2[0]))); return g1.intersects(g2); } int main() { QgsPointXY p1(0, 0); QgsPointXY p2(10, 10); QgsPointXY p3(5, 0); QgsPointXY p4(5, 10); bool intersecting = areLinesIntersecting(p1, p2, p3, p4); if (intersecting) { qDebug() << "The lines are intersecting."; } else { qDebug() << "The lines are not intersecting."; } return 0; } ``` 以上代码定义了一个`areLinesIntersecting`函数,它接受四个点作为参数来表示两条线段。使用QgsGeometry创建两个线段,然后通过`intersects`函数来判断是否相交。在`main`函数中,我们定义了四个点作为示例输入,并调用`areLinesIntersecting`函数进行判断。根据返回结果,打印出相应的信息。 请注意,代码中使用了QGIS的相关类和函数,所以确保已正确安装并配置了QGIS库。另外,此代码只是一个简单示例,实际使用时可能需要考虑更多的边界情况和错误处理。 ### 回答3: 首先,QGIS是一个开源的地理信息系统软件,支持Python编程语言。要判断两条线段是否相交,可以利用QGIS的Geometry类来进行计算。下面是一个简单的示例代码,演示如何使用QGIS判断两条线段是否相交: ```python from qgis.core import * # 创建两条线段的几何对象 line1 = QgsGeometry.fromPolyline([QgsPointXY(0, 0), QgsPointXY(2, 2)]) line2 = QgsGeometry.fromPolyline([QgsPointXY(1, 0), QgsPointXY(1, 3)]) # 判断线段是否相交 if line1.intersects(line2): print("线段相交") else: print("线段相交") ``` 在上面的代码中,我们首先导入了`qgis.core`模块,该模块提供了与QGIS核心功能相关的功能。然后,我们使用`QgsGeometry`类创建了两条线段的几何对象。其中,`fromPolyline`方法用于创建线段的几何对象,传入的参数是一系列地理坐标点。接着,我们使用`intersects`方法判断两条线段是否相交。如果相交,则输出"线段相交",否则输出"线段相交"。 请注意,上述代码仅仅是一个示例,只适用于简单的线段相交判断。如果需要处理复杂的情况,例如线段重叠、线段共线等,可能需要使用更复杂的算法或者调用其他库来完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值