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

文章目录

    • 相关文献
    • 基础
    • 点面关系展示图
    • 点面关系代码

作者:小猪快跑

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

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

相关文献

  • The Shapely User Manual — Shapely 2.0.1 documentation

基础

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

from shapely import Point, Polygon, GeometryCollection,LineStringpoint = 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])

在这里插入图片描述

点面关系展示图

在这里插入图片描述

点面关系代码

# figures.py
from math import sqrt
from shapely import affinityGM = (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 figures import BLUE, GRAY, set_limitsfig = 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()

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Linux CentOS7 vim宏操作

vim的macro就是用来解决重复的问题。在vim寄存器的文章里面已经对macro有所涉及,macro的操作都是以文本的方式存放在寄存器中。 宏是一组命令的集合,应用极其广泛,包括MS Office中的word编辑器,excel编辑器和各种文本编辑器&…

输入电压转化为电流性 5~20mA方案

输入电压转化为电流性 5~20mA方案 方案一方案二方案三 方案一 XTR111是一款精密的电压-电流转换器是最广泛应用之一。原因有二:一是线性度非常好、二是价格便宜。总结成一点,就是性价比高。 典型电路 最终电路 Z1二极管处输出电流表达式:…

Linux-centos系统安装MySql5.7

1.配置yum仓库 1.1配置yum仓库 rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 1.2 安装Mysql yum库 rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm 2.使用yum安装Msql 说明:下载大约5分钟左右 yum -y install mysq…

2023计算机保研——双非上岸酒吧舞

我大概是从22年10月份开始写博客的,当时因为本校专业的培养方案的原因,课程很多,有些知识纸质记录很不方便,于是选择了打破了自己的成见使用博客来记录学习生活。对于我个人而言,保研生活在前一大半过程中都比较艰难&a…

网络安全行业真的内卷了吗?网络安全就业就业必看

前言 有一个特别流行的词语叫做“内卷”: 城市内卷太严重了,年轻人不好找工作;教育内卷;考研内卷;当然还有计算机行业内卷…… 这里的内卷当然不是这个词原本的意思,而是“过剩”“饱和”的替代词。 按照…

c语言进阶部分详解(详细解析字符串常用函数,并进行模拟实现)

前段时间也是把指针较为详细系统的讲解完毕,接下来介绍一个全新的知识点,就是字符函数和字符串函数 前几期文章可进我主页观看:总之就是非常唔姆_Matlab,经验分享,c语言题目分享-CSDN博客 想要源代码可以去我的github看看:Neros…

5. Mysql卸载

Mysql卸载 已经成功安装mysql,没有必要卸载,卸载之后不一定再次会安装成功。 双击安装包 检查如下三个目录是否有mysql,有的话,删除掉即可(前提,电脑只有Mysql8,否则mysql其他版本也会被删除)…

【数据结构】排序算法(二)—>冒泡排序、快速排序、归并排序、计数排序

👀樊梓慕:个人主页 🎥个人专栏:《C语言》《数据结构》《蓝桥杯试题》《LeetCode刷题笔记》《实训项目》 🌝每一个不曾起舞的日子,都是对生命的辜负 目录 前言 1.冒泡排序 2.快速排序 2.1Hoare版 2.2占…

vue3 中使用echarts图表——柱状图

柱状图是比较常用的图形结构&#xff0c;所以我先收集一些精美的柱状图 一、柱状图&#xff1a;设置圆角和颜色 <template><div class"box" ref"chartDom"></div> </template> <script setup> import { ref, onMounted } fr…

无法启动此程序,因为计算机中“找不到msvcp140.dll”的解决方法

msvcp140.dll是Microsoft Visual C 2015 Redistributable的一个动态链接库文件&#xff0c;它是许多基于Visual Studio开发的应用程序和游戏的必要组件。当计算机上缺失msvcp140.dll文件时&#xff0c;可能会导致以下问题&#xff1a; 1. 程序无法启动&#xff0c;提示“找不到…

位图/布隆过滤器

一、位图 1.1位图的概念 所谓位图&#xff0c;就是用每一位来存放某种状态&#xff0c;适用于海量数据&#xff0c;数据无重复的场景。通常是用来判断某个数据存不存在的。 1.2位图的实现 template<size_t N>class bitset{public:bitset(){//需要N个比特位&#xff0c;…

生产设备巡检管理系统

凡尔码搭建生产设备巡检系统是通过确保巡检工作的质量以及提高巡检工作的效率来提高设备维护水平的一种系统&#xff0c;它对巡检管理考核工作从巡检人员、巡检任务、隐患管理、图像视频、盯防考核进行严格、科学的统计、分析&#xff0c;从而有效的保障巡检工作的顺利展开&…

Opengl之立方体贴图

简单来说,立方体贴图就是一个包含了6个2D纹理的纹理,每个2D纹理都组成了立方体的一个面:一个有纹理的立方体。你可能会奇怪,这样一个立方体有什么用途呢?为什么要把6张纹理合并到一张纹理中,而不是直接使用6个单独的纹理呢?立方体贴图有一个非常有用的特性,它可以通过一…

约束优化算法(optimtool.constrain)

import optimtool as oo from optimtool.base import np, sp, pltpip install optimtool>2.4.2约束优化算法&#xff08;optimtool.constrain&#xff09; import optimtool.constrain as oc oc.[方法名].[函数名]([目标函数], [参数表], [等式约束表], [不等式约数表], [初…

VulnHub Earth

一、信息收集 1.主机和端口扫描 nmap -sS 192.168.103.1/24 发现443端口有DNS解析&#xff0c;在hosts文件中添加DNS解析&#xff1a; 2.收集earth.local信息 发现有Previous Messages 37090b59030f11060b0a1b4e0000000000004312170a1b0b0e4107174f1a0b044e0a000202134e0a161…

Electron笔记

基础环境搭建 官网:https://www.electronjs.org/zh/ 这一套笔记根据这套视频而写的 创建项目 方式一: 官网点击GitHub往下拉找到快速入门就能看到下面这几个命令了 git clone https://github.com/electron/electron-quick-start //克隆项目 cd electron-quick-start //…

前端position: absolute是相对于谁定位的?

1. 当祖父元素是relative定位, 父元素是absolute定位, 子元素也是absolute定位 <script setup></script><template><div class"relative"><p class"absolute1">absolute1<p class"absolute2">absolute2<…

计算机竞赛 题目:基于python的验证码识别 - 机器视觉 验证码识别

文章目录 0 前言1 项目简介2 验证码识别步骤2.1 灰度处理&二值化2.2 去除边框2.3 图像降噪2.4 字符切割2.5 识别 3 基于tensorflow的验证码识别3.1 数据集3.2 基于tf的神经网络训练代码 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 基于pyt…

使用ebpf 监控linux内核中的nat转换

1.简介 Linux NAT&#xff08;Network Address Translation&#xff09;转换是一种网络技术&#xff0c;用于将一个或多个私有网络内的IP地址转换为一个公共的IP地址&#xff0c;以便与互联网通信。 在k8s业务场景中&#xff0c;业务组件之间的关系十分复杂. 由于 Kubernete…

gin 框架的 JSON Render

gin 框架的 JSON Render gin 框架默认提供了很多的渲染器&#xff0c;开箱即用&#xff0c;非常方便&#xff0c;特别是开发 Restful 接口。不过它提供了好多种不同的 JSON Render&#xff0c;那么它们的区别是什么呢&#xff1f; // JSON contains the given interface obje…