python求图形面积_求高效的,计算两多边形重叠面积的算法,python实现

[Asm] 纯文本查看 复制代码import random

import time

from sympy.geometry import *

import numpy as np

from scipy import stats

def polygon_over_area(obj_a, obj_b):

p_a = obj_a # sympy.geometry.Polygon

p_b = obj_b

x_obj = p_a.intersection(p_b)

if not x_obj:

if p_b.encloses_point(p_a.vertices[0]):

return p_a.area

if p_a.encloses_point(p_b.vertices[0]):

return p_b.area

return 0

x_point = []

segment_list = []

for item in x_obj:

if isinstance(item, Point):

x_point.append(item)

elif isinstance(item, Segment):

for p in item.points:

if p in x_point:

x_point.remove(p)

else:

x_point.append(p)

segment_list.append(item)

region = []

a_point_in_b = []

for v in p_a.vertices:

if p_b.encloses_point(v):

a_point_in_b.append(v)

b_point_in_a = []

for v in p_b.vertices:

if p_a.encloses_point(v):

b_point_in_a.append(v)

new_region_point = a_point_in_b + b_point_in_a + x_point

over_area = 0

while 1:

if segment_list:

while 1:

if not segment_list:

select_segment = None

break

select_segment = segment_list.pop()

verify_point = select_segment.points[0]

point_segment = select_segment

need_continue = False

while 1:

if verify_point not in x_point:

for segment in segment_list:

if segment == point_segment:

continue

if segment.contains(verify_point):

point_segment = segment

if segment.points[1] == verify_point:

verify_point = segment.points[0]

else:

verify_point = segment.points[1]

break

else:

need_continue = True

break

else:

break

if need_continue:

continue

for side in p_a.sides:

if not side.contains(select_segment.points[0]):

continue

if not side.contains(select_segment.points[1]):

continue

a_side = side

break

else:

raise ValueError

for side in p_b.sides:

if not side.contains(select_segment.points[0]):

continue

if not side.contains(select_segment.points[1]):

continue

b_side = side

break

else:

raise ValueError

v_a = a_side.points[1] - a_side.points[0]

v_b = b_side.points[1] - b_side.points[0]

if (v_a[0] * v_b[0] < 0) or (v_a[1] * v_b[1] < 0):

continue

if select_segment.points[0] == a_side.points[1]:

end_point = a_side.points[1]

elif select_segment.points[0] == b_side.points[1]:

end_point = b_side.points[1]

elif select_segment.points[1] == a_side.points[1]:

end_point = a_side.points[1]

elif select_segment.points[1] == b_side.points[1]:

end_point = b_side.points[1]

else:

raise ValueError

while 1:

for segment in segment_list:

if segment.contains(end_point):

end_point = segment.points[0] if end_point == segment.points[1] else segment.points[1]

segment_list.remove(segment)

break

else:

break

point_line_a = []

point_line_b = []

for side in p_a.sides:

if side.contains(end_point):

point_line_a.append(side)

for side in p_b.sides:

if side.contains(end_point):

point_line_b.append(side)

if len(point_line_a) == 2:

if len(point_line_b) != 1:

raise ValueError

select_inner_point = end_point

select_polygon = p_a

next_polygon = p_b

break

elif len(point_line_b) == 2:

if len(point_line_a) != 1:

raise ValueError

select_inner_point = end_point

select_polygon = p_b

next_polygon = p_a

break

else:

raise ValueError

if not select_segment:

continue

elif a_point_in_b:

select_inner_point = a_point_in_b.pop()

select_polygon = p_a

next_polygon = p_b

elif b_point_in_a:

select_inner_point = b_point_in_a.pop()

select_polygon = p_b

next_polygon = p_a

else:

break

new_region_points = [select_inner_point]

while 1:

for side in select_polygon.sides:

if side.contains(select_inner_point) and side.points[1] != select_inner_point:

select_side = Segment(select_inner_point, side.points[1])

break

else:

raise ValueError

x_p_list = []

for x_p in x_point:

if select_side.contains(x_p):

if select_inner_point == x_p:

continue

x_p_list.append(x_p)

if x_p_list:

area = select_inner_point.distance(x_p_list[0])

next_point = x_p_list[0]

for x_p in x_p_list:

if 0 < select_inner_point.distance(x_p) < area:

area = select_inner_point.distance(x_p)

next_point = x_p

select_inner_point = next_point

x_point.remove(next_point)

select_polygon, next_polygon = next_polygon, select_polygon

else:

select_inner_point = select_side.points[1]

if select_inner_point in a_point_in_b:

a_point_in_b.remove(select_inner_point)

if select_inner_point in b_point_in_a:

b_point_in_a.remove(select_inner_point)

if select_inner_point in new_region_points:

over_area += Polygon(*new_region_points).area

break

else:

new_region_points.append(select_inner_point)

return over_area

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

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

相关文章

linux下c代码调用.so,Linux下C程序调用.so(动态链)的一个例子

/*********************************************** 使用Linux下C的动态链接库.So文件* void *pdlHandle;* pdlHandle dlopen("/home/fengsy/len/Len.So/len.so", RTLD_LAZY);* pnswapdlsym(pdlHandle, "swap");****************************************…

dbnetlib sqlserver不存在或拒绝访问_404:对不起,您访问的网页不存在

404&#xff1a;对不起您访问的网页不存在https://www.zhihu.com/video/1101123296195723264你可能在上网时也好奇过&#xff0c;为什么点开一个无法显示信息的页面&#xff0c;上面会出现数字404&#xff1f;要解释这个问题&#xff0c;我们需要从互联网诞生之初说起。20世纪8…

linux中控和安卓中控哪个比较好,原厂中控和改装中控有什么区别?哪个更好?...

很多人都喜欢车辆上有一个大屏的中控。那种大屏的中控不仅功能很多&#xff0c;而且还很实用。但是有很多车需要高配才能有中控。而且还需要加钱。很多人在买车的时候都会选择一些低配的车&#xff0c;然后自己去安装中控。有些人认为原厂的中控不仅价格贵&#xff0c;而且还有…

bufferedreader读取中文乱码_python之pandas模块关于csv文件乱码问题解决

介绍相信部分小伙伴们在处理windows系统生成的csv文件时会遇到中文显示乱码的问题&#xff0c;尤其是使用Excel打开这类文件时这类问题尤为突出。解决如图&#xff0c;我们通过Excel工具打开该csv文件时&#xff0c;中文展示为乱码。此时&#xff0c;只需要我们通过功能强大的数…

linux mint python3.6,Linux 上如何安装并切换最新版本的 Python 3.6

原标题&#xff1a;Linux 上如何安装并切换最新版本的 Python 3.6如果你安装了 Linux 系统&#xff0c;正在学习 Python 并想要使用最新的版本的话&#xff0c;那么这篇文章就是为你而写的。-- ShekinPython是 Linux 中一种最流行的编程语言。它被写成了各种工具和库。除此之外…

centos 卸载软件_Linux服务器运维必备技能 软件包和启动项超详细整理

一、RedHat/CentOS包管理1. 包管理工具RPM(1) 简介最早用在 RedHat的包管理器&#xff0c;现在已经成为Linux常见的包管理系统。RPM包有两种类型&#xff1a;二进制RPM包源码RPM包可以在www.rpmfind.net网站查询软件包&#xff0c;也可以查到其依赖项&#xff1a;(2) RPM包命名…

linux内存分配器类型,内核早期内存分配器:memblock

原标题&#xff1a;内核早期内存分配器&#xff1a;memblock本文转载自Linux爱好者本文来自 程雪涛的自荐投稿Linux内核使用伙伴系统管理内存&#xff0c;那么在伙伴系统工作前&#xff0c;如何管理内存&#xff1f;答案是memblock。memblock在系统启动阶段进行简单的内存管理&…

java substring截取字符串_java基础教程之字符串的介绍,比较重要的一个知识点【下】...

字符串操作1、获取子字符串通过String类的substring()方法可对字符串进行截取。这些方法的共同点就是都是利用字符串的下标进行截取。应明确字符串下标是从0开始的。substring()方法被两种不同的方法重载&#xff0c;来满足不同的需要。(1)substring(intbeginIndex)该方法返回的…

在linux环境下安装wiringpi库,wiringPi库的pwm配置及使用说明

本文介绍树莓派(raspberry pi)在linux c 环境下的硬件pwm配置及使用方法。1. 下载安装wiringPi此步骤建议参考官网指南&#xff0c;wiringPi提供了对树莓派的硬件IO访问&#xff0c;包括GPIO/I2C/PWM等&#xff0c;下载安装后本地会出现wiringPi文件夹&#xff0c;根目录下有/e…

excel split函数_Excel 字符串拆分

用 Excel 处理数据时&#xff0c;有时需要对字符串进行拆分。对于比较简单的拆分&#xff0c;使用 Excel 函数可以顺利完成&#xff0c;但碰到一些特殊需求&#xff0c;或者拆分的规则比较复杂时&#xff0c;则很难用 Excel 实现了。这里列出一些拆分需求示例&#xff0c;分析拆…

python优雅编程_Python优雅地可视化数据

[导读]声明&#xff1a;由于本文的代码大部分是参考书中的例子&#xff0c;所以不提供完整代码&#xff0c;只提供示例片段&#xff0c;也就是只能看出某一部分用法&#xff0c;感兴趣的需要在自己的数据上学习测试。 声明&#xff1a;由于本文的代码大部分是参考书中的例子&am…

linux 未找到wifi适配器,无线 - 重新启动后找不到WI-FI适配器

问题&#xff1a;我已经成功安装了ubuntu 18.04.3 LTS&#xff0c;已经成功连接到无线网络&#xff0c;但是重新启动后&#xff0c;找不到wifi适配器&#xff0c;在每次重新启动时要重新安装wifi驱动程序很烦人&#xff0c;如果能帮帮我&#xff0c;将非常感激&#xff0c;网络…

在c语言中数组下标的最小值,数组元素下标的上限_c语言中,数组元素的下标下限为...

展开全部c语言中&#xff0c;数组元素的下标下限为0。数组中的各元素的存e69da5e887aa62616964757a686964616f31333431373230储是有先后顺序的&#xff0c;它们在内存中按照这个先后顺序连续存放在一起。数组元素用整个数组的名字和它自己在数组中的顺序位置来表示。例如&#…

python求矩形面积_关于python:创建矩形类

我真的不太懂课程&#xff0c;任何帮助都会很好。 矩形类应具有以下私有数据属性&#xff1a; __length __width Rectangle类应该有一个创建这些属性并将其初始化为1的__init__方法。它还应具有以下方法&#xff1a; set_length—此方法为__length字段赋值。 set_width—此方法…

linux epel7安装,在CentOS6.x或CentOS7.x上安装EPEL Repo,Extra Packages for Enterprise Linux (EPEL)...

在CentOS6.x或CentOS7.x上安装EPEL Repo,Extra Packages for Enterprise Linux (EPEL)Error: Cannot retrieve metalink for repository: epel. Please verify its path and try againReporepository&#xff0c;资源库&#xff0c;源的意思。RHEL EPEL(Extra Packages for Ent…

sparkstreaming 读取mysql_第十篇|SparkStreaming手动维护Kafka Offset的几种方式

Spark Streaming No Receivers 方式的createDirectStream 方法不使用接收器&#xff0c;而是创建输入流直接从Kafka 集群节点拉取消息。输入流保证每个消息从Kafka 集群拉取以后只完全转换一次&#xff0c;保证语义一致性。但是当作业发生故障或重启时&#xff0c;要保障从当前…

c语言程序设计题2015,2015年荐C语言程序设计等级考试习题汇编.doc

C语言程序设计等级考试习题汇编1、设计程序&#xff1a;数列第1项为81,此后各项均为它前1项的正平方根&#xff0c;统计该数列前30项之和&#xff0c;并以格式"%.3f"写到考生目录中Paper子目录下的新建文件design.dat中。#include #include void main(){ FILE *p; fl…

用了python之后笔记本卡了_应用

想参与时下最热门的话题讨论吗&#xff1f;看过这些2012最炙手可热的电影电视剧吗&#xff1f;想了解最新职场信息&#xff0c;轻松晋升职场达人么&#xff1f;……本应用为你精选了5个分类(经济、教育、娱乐、生活、科技)共150篇妙趣横生的时尚双语新闻&#xff0c; 7天时间完…

c语言tmplink,为了便于阅读,偿试把BLHeli的汇编源程序改成C语言格式

只是为了方便阅读和理解&#xff0c;所以未做详细的正确性检查。因为能力所限&#xff0c;程度中还是保留了很多跳转(goto)语句。主循环已经完成&#xff0c;发上来供大家参考int main(void){if(0 Prepare_Lock_Or_Fuse_Read() & 0x80){while(1);}Disable_Watchdog();Init…

anaconda 安装pytorch_conda上安装PyTorch

conda上安装PyTorch这里的安装系统&#xff1a;Windows系统第一步&#xff0c;打开Anaconda Prompt第二步&#xff0c;为pytorch创建一个环境&#xff1a;conda create -n pytorch python3.8&#xff08;这里的pytorch是环境的名称&#xff0c;python3.8表示安装的是3.8版本的p…