多边形裁剪

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#python实现可视化的多边形裁剪

import matplotlib.pyplot as plt

import copy

import math

from tkinter import *

def cross_point(line1,line2,winx,winy):#计算交点函数

year1 = copy.deepcopy(line1)

pop1 = copy.deepcopy(line2)

winx1 = copy.deepcopy(winx)

winy1 = copy.deepcopy(winy)

node_class = []

minx = min(winx)

maxx = max(winx)

miny = min(winy)

maxy = max(winy)

count = 0

for i in range(len(line1)-1):

count1=0

x1=line1[i]#取四点坐标

y1=line2[i]

x2=line1[i+1]

y2=line2[i+1]

k1=(y2-y1)*1.0/(x2-x1)#计算k1,由于点均为整数,需要进行浮点数转化

b1=y1*1.0-x1*k1*1.0#整型转浮点型是关键

nodey = k1*minx*1.0+b1*1.0

if (y1<=nodey<=y2 or y2<=nodey<=y1) and (miny<=nodey<=maxy) and ([maxx,nodey] not in node_class):

node_class.append([minx,nodey])

count=count+1

count1=count1+1

nodey1 = k1*maxx*1.0+b1*1.0

if (y1<=nodey1<=y2 or y2<=nodey1<=y1) and (miny<=nodey1<=maxy) and ([maxx,nodey1] not in node_class):

node_class.append([maxx,nodey1])

count=count+1

count1=count1+1

nodex = (miny*0.1 - b1*0.1)/(k1*0.1)

if (minx<=nodex<=maxx) and (x1<=nodex<=x2 or x2<=nodex<=x1) and ([nodex,miny] not in node_class):

node_class.append([nodex,miny])

count=count+1

count1=count1+1

nodex1 = (maxy*0.1 - b1*0.1)/(k1*0.1)

if (minx<=nodex1<=maxx) and (x1<=nodex1<=x2 or x2<=nodex1<=x1) and ([nodex1,maxy] not in node_class):

node_class.append([nodex1,maxy])

count=count+1

count1=count1+1

print(count1)

if count1 == 1:

year1.insert(i+count,node_class[count-1][0])

pop1.insert(i+count,node_class[count-1][1])

if count1 ==2:

if (math.fabs(node_class[count-2][0]-x1)>math.fabs(node_class[count-1][0]-x1)): n = node_class[count-2]

node_class[count-2] = node_class[count-1]

node_class[count-1] = n

year1.insert(i+count-1,round(node_class[count-2][0],5))

pop1.insert(i+count-1,node_class[count-2][1])

year1.insert(i+count,node_class[count-1][0])

pop1.insert(i+count,node_class[count-1][1])

print(year1,pop1,node_class)

return year1,pop1

def line(year,pop,year1,pop1,x,y):

minx = min(x)

maxx = max(x)

miny = min(y)

maxy = max(y)

print(minx,maxx,miny,maxy)

linex = []

liney = []

b=[]

for i in range (len(year1)-1):

if (minx<=year1[i]<=maxx) and (miny<=pop1[i]<=maxy):

b.append(i)

linex.append(year1[i])

liney.append(pop1[i])

linex.append(year1[b[0]])

liney.append(pop1[b[0]])

print(linex,liney)

return linex,liney

def run1(n,m,a,b):

n = int(n)

m = int(m)

a = int(a)

b = int(b)

year = [4,20,25,15,7,4]

pop = [2,3,1,6,5,2]

year1 = []

year2 = []

x = [n+a,n,n,n+a,n+a]

y = [m+b,m+b,m,m,m+b]

year1,pop1 = cross_point(year,pop,x,y)

linex,liney=line(year,pop,year1,pop1,x,y)

plt.plot(year,pop)

plt.plot(x,y,'r')

plt.plot(linex,liney,'b')

plt.show()

root = Tk()

root.geometry('500x240')

root.title('多边形裁剪')

lb1 = Label(root, text='请输入裁剪框右下角坐标,宽,高') lb1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)

inp1 = Entry(root)

inp1.place(relx=0.1, rely=0.2, relwidth=0.15, relheight=0.1) inp2 = Entry(root)

inp2.place(relx=0.3, rely=0.2, relwidth=0.15, relheight=0.1) inp3 = Entry(root)

inp3.place(relx=0.5, rely=0.2, relwidth=0.15, relheight=0.1) inp4 = Entry(root)

inp4.place(relx=0.7, rely=0.2, relwidth=0.15, relheight=0.1) # 方法-直接调用run1()

相关文档
最新文档