代码之家  ›  专栏  ›  技术社区  ›  Travis

如何比较坐标python[闭合]

  •  0
  • Travis  · 技术社区  · 9 年前

    我有两个不同的海龟,每一个都是独立的实体,我想比较它们的坐标,但在我的代码中,它们是图片,所以0,0在不同的地方,所以我必须把一个移动到100,-100,让它看起来像样,在我想要的地方。

    import turtle
    turtle = Turtle()
    Shop = Turtle()
    Shop.up()
    turtle.up()
    Shop.goto(100,-100)
    if (#insert comparison here):
        print("compared")
    

    提前感谢

    2 回复  |  直到 9 年前
        1
  •  1
  •   cdlane    9 年前

    要做到这一点,我们需要处理两个问题。首先是图像偏移。其次,turtle使用浮点坐标,所以做一个简单的 == 0.00001 == 0.0 为了您的目的?)这里有一种方法:

    from turtle import Turtle
    
    customer = Turtle()
    customer.up()
    
    SHOP_OFFSET = (100, -100)
    X, Y = 0, 1
    
    shop = Turtle()
    shop.up()
    shop.goto(SHOP_OFFSET[X], SHOP_OFFSET[Y])
    
    if abs(customer.xcor() - (shop.xcor() - SHOP_OFFSET[X])) < 0.5 and \
        abs(customer.ycor() - (shop.ycor() - SHOP_OFFSET[Y])) < 0.5:
        print("same location")
    
        2
  •  -1
  •   Glassimal Brokan    9 年前

    使用对象方法

    xcor() 
    

    ycor()
    

    在你的情况下,你想使用

    if (turtle.xcor() == shop.xcor() and turtle.ycor() == turtle.ycor()): #rest of code