python - 你如何防止一条线相交
问题描述
我一直在研究一种从一个随机点到另一个随机点随机生成一条线的代码。但是,我希望创建的线不与自身相交。如果线与自身相交,或者可能刷新随机坐标,有没有办法替换它?如果有人可以提供帮助,那就太好了!这是我创建的代码(是的,据我所知,为了让它运行,所有这些都是必需的);
#imports
from tkinter import *
import random
from random import randint
import math
import pip
import shapely
from shapely.geometry import LineString
#setting up the canvas
master = Tk()
master.geometry("500x500")
master.title("Sprouts")
w = Canvas(master, width=500, height=500, bg="white")
w.pack()
#creating the circle
def create_circle(x, y, r, w): #center coordinates, radius
x0 = x - r
y0 = y - r
x1 = x + r
y1 = y + r
return w.create_oval(x0, y0, x1, y1)
#creating coordinate variables
xC = random.randint(10,490)
yC = random.randint(10,490)
xC2 = random.randint(10,490)
yC2 = random.randint(10,490)
L1C1 = random.randint(10,490)
L1C2 = random.randint(10,490)
L1C3 = random.randint(10,490)
L1C4 = random.randint(10,490)
#displaying the circle
c1 = create_circle(xC, yC, 5, w)
c2 = create_circle(xC2, yC2, 5, w)
#displaying the line #implementing the curve
Line1 = LineString([(xC, yC), (xC2, yC2)]
or [(xC, yC), (L1C1, L1C2), (xC2, yC2)]
or [(xC,yC), (L1C1, L1C2), (L1C3, L1C4), (xC2, yC2)])
Line1show = w.create_line(xC, yC, xC2, yC2 or
xC, yC, L1C1, L1C2, xC2, yC2 or
xC, yC, L1C1, L1C2, L1C3, L1C4, xC2, yC2,
smooth='1',width="2")
#defining the intersects variable
intersection1 = Line1.intersection(Line1)
if Line1 == intersection1:
print('try again') #this is a filler so the code functions
#either replace Line1, or use another method to prevent intersection
w.mainloop()
解决方案
推荐阅读
- sockets - 如何在socket.io中存储数据
- c - Linux 上的 UDP 或 TCP 缓冲区大小应该有多大?
- php - 如何在 OctoberCMS 的联接表中使用 where 子句?
- amazon-s3 - 如何修复ClientError:调用CreateBucket操作时发生错误(AccessDenied):调用create_bucket时访问被拒绝
- python - How to return aiohttp like python requests?
- python - ValueError: int() 以 10 为底的无效文字:'glassdoor',同时从自定义用户模型创建用户对象
- node.js - Resetting the state after every integration test - Supertest, Nodejs, Typescript
- c# - 如何从共享同一接口的对象列表中访问对象的特定属性
- python - Tox“错误:找不到文件:
" - javascript - 在 PHP 中单击下拉菜单中的“是”后显示输入文本?