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

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

文章目录

  • 【附代码】判断线段是否相交算法(Python,C++)
    • 相关文献
    • 测试电脑配置
    • 基础
      • 向量旋转
      • 向量缩放
      • 向量投影
        • 推导
      • 点乘
        • 定义
        • 推导
        • 几何意义
      • 叉乘
        • 定义
        • 推导
        • 几何意义
      • 判断线段是否相交
      • 代码
        • C++
        • Python
    • 画图代码
      • 测试结果

作者:小猪快跑

基础数学&计算数学,从事优化领域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×ACAB×ADCD×CACD×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×ACAB×ADCD×CACD×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 Falseabx = x2 - x1aby = y2 - y1acx = x3 - x1acy = y3 - y1adx = x4 - x1ady = y4 - y1bcx = x3 - x2bcy = y3 - y2cdx = x4 - x3cdy = y4 - y3cp1 = 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 Truereturn Falsedef test(n):res = 0for 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:continuefor 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:continueres += is_intersected(x1, y1, x2, y2, x3, y3, x4, y4)return resif __name__ == '__main__':s = time()print(test(7))print(time() - s)

画图代码

# main.py
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString, Polygon
from shapely.plotting import plot_polygon, plot_points, plot_linefrom csdn_line_intersect import is_intersected
from figures import BLUE, GRAY, set_limitsfig = 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/212057.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

怎样用AIDL Service 传递复杂数据

大家都知道在Android中通过AIDL可以跨进程调用Service中的数据&#xff0c;网上也有很多实例&#xff0c;但是大部分实例都是关于基本数据类型的远程调用&#xff0c;很少讲到复杂数据的调用&#xff0c;今天我用一个例子来演示一下怎样用AIDL Service 传递复杂数据。 我们分2…

【神印王座】龙皓晨美妆胜过月夜,魔神皇识破无视,撮合月夜阿宝

Hello,小伙伴们&#xff0c;我是拾荒君。 《神印王座》国漫第82集已更新&#xff0c;拾荒君和大多数人一样&#xff0c;更新就去看了。魔神皇枫秀&#xff0c;威严凛然&#xff0c;突然空降月魔宫&#xff0c;整个宫殿都在这股无与伦比的强大气息中颤栗。为了顺利躲避魔神皇的…

延时任务定时发布,基于 Redis 与 DB 实现

目录 1、什么是延时任务&#xff0c;分别可以使用哪些技术实现&#xff1f; 1.2 使用 Redis 和 DB 相结合的思路图以及分析 2、实现添加任务、删除任务、拉取任务 3、实现未来数据的定时更新 4、将数据库中的任务数据&#xff0c;同步到 Redis 中 1、什么是延时任务&#xff…

【工具栏】热部署不生效

目录 配置热部署&#xff1a; 解决热部署不生效&#xff1a; 首先检查&#xff1a; 第一步&#xff1a; 第二步&#xff1a; 第三步&#xff1a; 第四步&#xff1a; 配置热部署&#xff1a; https://blog.csdn.net/m0_67930426/article/details/133690559 解决热部署不…

深入理解JVM 类加载机制

深入理解JVM 类加载机制 虚拟机如何加载Class文件&#xff1f; Class文件中的信息进入到虚拟机后会发生什么变化&#xff1f; 类加载机制就是Java虚拟机把描述类的数据从Class文件加载到内存&#xff0c;并对数据进行校验、转换解析和初始化&#xff0c;最终形成可以被虚拟机…

6.2.SDP协议

那今天呢&#xff1f;我们来介绍一下sdp协议&#xff0c;那实际上呢&#xff1f;sdp协议非常的简单。我们如果拿到一个stp的文档去看的话&#xff0c;那你要分阅里边的所有的内容会觉得很枯燥&#xff0c;但实际上呢&#xff0c;如果我们按照这张图所展示的结构去看stp的话。你…

[MySQL] MySQL 表的增删查改

本篇文章对mysql表的增删查改进行了详细的举例说明解释。对表的增删查改简称CRUD : Create(创建), Retrieve(读取)&#xff0c;Update(更新)&#xff0c;Delete&#xff08;删除&#xff09;。其中重点是对查询select语句进行了详细解释&#xff0c;并且通过多个实际例子来帮助…

【从零开始实现意图识别】中文对话意图识别详解

前言 意图识别&#xff08;Intent Recognition&#xff09;是自然语言处理&#xff08;NLP&#xff09;中的一个重要任务&#xff0c;它旨在确定用户输入的语句中所表达的意图或目的。简单来说&#xff0c;意图识别就是对用户的话语进行语义理解&#xff0c;以便更好地回答用户…

【RtpRtcp】1: webrtc m79:audio的ChannelReceive 创建并使用

m79中,RtpRtcp::Create 的调用很少 不知道谁负责创建ChannelReceiveclass ChannelReceive : public ChannelReceiveInterface,public MediaTransportAudioSinkInterface {接收编码后的音频帧:接收rtcp包:

VTK表面画贴合线条避免陷入局部最小值

通常画线&#xff0c;由于距离太远会陷入局部最小值&#xff0c;通过算法解决这一问题。 1、陷入局部最小值 2、算法解决陷入局部最小值问题

大模型增量预训练参数说明

在增量预训练过程中通常需要设置三类或四类参数,模型参数,数据参数,训练参数,额外参数。 下面分别针对这四种参数进行说明。 欢迎关注公众号 模型参数 model_type模型类型,例如bloom,llama,baichuan,qwen等。 model_name_or_path模型名称或者路径。 tokenizer_name_or…