【附代码】使用Shapely计算点面关系

文章目录

作者:小猪快跑

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

本文档介绍如何使用 Shapely Python 包 计算几何点面关系。
如有错误,欢迎指正。如有更好的算法,也欢迎交流!!!------@小猪快跑

相关文献

基础

先来看下如何创建点、线、面

python 复制代码
from shapely import Point, Polygon, GeometryCollection,LineString

point = Point(3, 3)
polygon = Polygon([(0, 5), (1, 1), (3, 0)])
circ = Point(4, 0).buffer(2)
line = LineString([(0, 0), (2, 2)])
polygon.intersection(circ)

GeometryCollection([point, polygon, circ,line])

点面关系展示图

点面关系代码

python 复制代码
# 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")
python 复制代码
# 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 figures import BLUE, GRAY, set_limits

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

# 1.1 判断点和点是否重叠
ax = fig.add_subplot(621)
a = Point(1, 1)
b = Point(2, 1)
plot_points(a, ax=ax, color=GRAY)
plot_points(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 1.2 判断点和点是否重叠
ax = fig.add_subplot(622)
a = Point(1.5, 1)
b = Point(1.5, 1)
plot_points(a, ax=ax, color=GRAY)
plot_points(b, ax=ax, color=GRAY)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 2.1 判断点和线是否重叠
ax = fig.add_subplot(623)
a = Point(1, 0)
b = LineString([(0, 0), (2, 2)])
plot_points(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'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 2.2 判断点和点是否重叠
ax = fig.add_subplot(624)
a = Point(1, 1)
b = LineString([(0, 0), (2, 2)])
plot_points(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'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 3.1 判断点和面是否重叠
ax = fig.add_subplot(625)
a = Point(3, 0)
b = Polygon([(0, 0), (0, 2), (2, 2), (2, 0)])
plot_points(a, ax=ax, color=GRAY)
plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 3.2 判断点和面是否重叠
ax = fig.add_subplot(626)
a = Point(1, 1)
b = Polygon([(0, 0), (0, 2), (2, 2), (2, 0)])
plot_points(a, ax=ax, color=GRAY)
plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 4.1 判断线和线是否重叠
ax = fig.add_subplot(627)
a = LineString([(0, 0), (2, 2)])
b = LineString([(0, 1), (1, 2)])
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'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 4.2 判断线和线是否重叠
ax = fig.add_subplot(628)
a = LineString([(0, 0), (2, 2)])
b = LineString([(2, 0), (0, 2)])
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'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 5.1 判断线和面是否重叠
ax = fig.add_subplot(629)
a = LineString([(0, 1), (2, 2)])
b = Point(1, 0).buffer(1)
plot_line(a, ax=ax, color=GRAY)
plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 5.2 判断线和面是否重叠
ax = fig.add_subplot(6, 2, 10)
a = LineString([(0, 0), (2, 2)])
b = Point(1, 0).buffer(1)
plot_line(a, ax=ax, color=GRAY)
plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_line(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 6.1 判断面和面是否重叠
ax = fig.add_subplot(6, 2, 11)
a = Point(0, 2).buffer(1)
b = Point(1, 0).buffer(1)
plot_polygon(a, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_points(a.intersection(b), ax=ax, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}')
set_limits(ax, -1, 4, -1, 3)

# 6.2 判断面和面是否重叠
ax = fig.add_subplot(6, 2, 12)
a = Point(0, 2).buffer(1)
b = Point(2, 1).buffer(2)
plot_polygon(a, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
plot_polygon(a.intersection(b), ax=ax, add_points=False, color=BLUE)
ax.set_title(f'a.intersection(b):{a.intersection(b)}'[:30])
set_limits(ax, -1, 4, -1, 3)

plt.savefig('output.png')
plt.show()
相关推荐
Auc2437 分钟前
使用scrapy框架爬取微博热搜榜
开发语言·python
梦想画家1 小时前
Python Polars快速入门指南:LazyFrames
python·数据分析·polars
程序猿000001号1 小时前
使用Python的Seaborn库进行数据可视化
开发语言·python·信息可视化
API快乐传递者1 小时前
Python爬虫获取淘宝详情接口详细解析
开发语言·爬虫·python
公众号Codewar原创作者1 小时前
R数据分析:工具变量回归的做法和解释,实例解析
开发语言·人工智能·python
FL16238631291 小时前
python版本的Selenium的下载及chrome环境搭建和简单使用
chrome·python·selenium
巫师不要去魔法部乱说1 小时前
PyCharm专项训练5 最短路径算法
python·算法·pycharm
Chloe.Zz2 小时前
Python基础知识回顾
python
骑个小蜗牛2 小时前
Python 标准库:random——随机数
python
Trouvaille ~2 小时前
【机器学习】从流动到恒常,无穷中归一:积分的数学诗意
人工智能·python·机器学习·ai·数据分析·matplotlib·微积分