【问题标题】:How to find if two dots on a graph intersect如何查找图形上的两个点是否相交
【发布时间】:2021-08-04 13:11:29
【问题描述】:

我正在做一个项目,其中有四只海龟在一个圆圈的中间,它们每只都遵循随机路径,直到其中一只离开。最后一部分是,如果两只或多只海龟相互碰撞,它们会向中心退两步。我拥有除此之外的一切,我不知道如何继续。以下是我到目前为止的代码。任何人都可以帮忙吗?谢谢!

from random import randrange
import matplotlib.pyplot as plt
import numpy as np

def turtle1234():
    x_points = []
    y_points = []
    x2_points = []
    y2_points = []
    x3_points = []
    y3_points = []
    x4_points = []
    y4_points = []
    x = 0
    y = 0
    x2 = 0
    y2 = 0
    x3 = 0
    y3 = 0
    x4 = 0
    y4 = 0
    while True:
        x = x + randrange(-1,2)
        y = y + randrange(-1,2) 
        x2 = x2 + randrange(-1,2)
        y2 = y2 + randrange(-1,2) 
        x3 = x3 + randrange(-1,2)
        y3 = y3 + randrange(-1,2) 
        x4 = x4 + randrange(-1,2)
        y4 = y4 + randrange(-1,2) 
        x_points.append(x)
        y_points.append(y)
        x2_points.append(x2)
        y2_points.append(y2)
        x3_points.append(x3)
        y3_points.append(y3)
        x4_points.append(x4)
        y4_points.append(y4)
        if x**2+y**2 > 100**2:
            print(x,y)
            print('Turtle 1 is outside the circle first')
            break
        if x2**2+y2**2 > 100**2:
            print(x2,y2)
            print('Turtle 2 is outside the circle first')
            break
        if x3**2+y3**2 > 100**2:
            print(x3,y3)
            print('Turtle 3 is outside the circle first')
            break
        if x4**2+y4**2 > 100**2:
            print(x4,y4)
            print('Turtle 4 is outside the circle first')
            break
    plt.plot(x_points,y_points)
    plt.plot(x2_points,y2_points)
    plt.plot(x3_points,y3_points) 
    plt.plot(x4_points,y4_points) 
x = np.linspace(-100,100,1000)
turtle1234()


plt.plot(x,-np.sqrt(100**2-x**2), color = 'b')
plt.plot(x, np.sqrt(100**2-x**2), color = 'b')

【问题讨论】:

  • 你的问题是什么?到目前为止,您尝试过什么?

标签: python numpy graphing


【解决方案1】:
turtles = [(x, y), (x1, y1), (x2, y2), (x3, y3)]
for index, turtle1 in enumerate(turtles):
    for index2, turtle2 in enumerate(turtles):
        if index == index2:
            continue
        if turtle1 == turtle2:
            pass # go tow steps to the middle

【讨论】:

    【解决方案2】:

    此代码应放在第 32 行# 之后

        xkeys = [[x,y],[x2,y2],[x3,y3],[x4,y4]]
        for xy in xkeys:
    
            if xkeys.count(xy) >1:
    
                first = xkeys.index(xy)
    
                xkeys.reverse()
                second = (len(xkeys) - 1)-xkeys.index(xy)
                xkeys.reverse()
    
                
                for x in xkeys[first]:
                    if x>=52:
                        xkeys[first][xkeys[first].index(x)]-=2
                    if x<=48:
                        xkeys[first][xkeys[first].index(x)]+=2
    
                for x in xkeys[second]:
                    if x>=52:
                        xkeys[second][xkeys[second].index(x)]-=2
                    if x<=48:
                        xkeys[second][xkeys[second].index(x)]+=2
                break
        x = xkeys[0][0]
        y = xkeys[0][1] 
        x2 = xkeys[1][0]
        y2 = xkeys[1][1] 
        x3 = xkeys[2][0]
        y3 = xkeys[2][1] 
        x4 = xkeys[3][0]
        y4 = xkeys[3][1]
    

    在这里,我根据给定的运动值制作了一个嵌套列表

        xkeys = [[x,y],[x2,y2],[x3,y3],[x4,y4]]
    

    然后以存在重复的条件对其进行迭代

        for xy in xkeys:
            if xkeys.count(xy) >1:
    

    first 是嵌套列表中第一个副本的索引

                first = xkeys.index(xy)
    

    为了获得第二个我将列表反转为自发解决方案,分配了第二个重复索引 然后再次反转列表

                xkeys.reverse()
                second = (len(xkeys) - 1)-xkeys.index(xy)
                xkeys.reverse()
    

    这里我假设中间点是(50,50)

                for x in xkeys[first]:
                    if x>=52:
                        xkeys[first][xkeys[first].index(x)]-=2
                    if x<=48:
                        xkeys[first][xkeys[first].index(x)]+=2
    
                for x in xkeys[second]:
                    if x>=52:
                        xkeys[second][xkeys[second].index(x)]-=2
                    if x<=48:
                        xkeys[second][xkeys[second].index(x)]+=2
                break
    

    在这里,我根据它们在 制作嵌套列表

        x = xkeys[0][0]
        y = xkeys[0][1] 
        x2 = xkeys[1][0]
        y2 = xkeys[1][1] 
        x3 = xkeys[2][0]
        y3 = xkeys[2][1] 
        x4 = xkeys[3][0]
        y4 = xkeys[3][1]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2012-05-18
      • 2017-07-19
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多