简体   繁体   English

查找到x或y坐标之一的距离

[英]Finding distance to one of either the x or y coordinate

Using turtle.distance() we can find the distance from the current position ( turtle.position ) to the specified x,y coordinate. 使用turtle.distance()我们可以找到从当前位置( turtle.position )到指定的x,y坐标的距离。 However, it doesn't appear that we can leave either option blank. 但是,似乎没有一个选项可以留空。 Is there a simple way to find the distance from the turtle's current position to ONLY the x or ONLY the y coordinate? 是否有一种简单的方法可以找到从海龟当前位置到仅x或仅y坐标的距离?

The task is to draw a line that will turn around when it hits a 400x400 bounding box and the best way I can think of to do that is find out if it's in range of either the x or y value of the box. 任务是画一条线,当它碰到400x400边界框时会转弯,我能想到的最好方法是找出它是否在框的x或y值范围内。

def check_bounds(pos,a_rect):
    rect_left = a_rect[0][0]
    rect_top = a_rect[0][1]
    rect_right = a_rect[1][0]
    rect_top = a_rect[1][1]
    hit_x = rect_left <= pos[0] <= rect_right 
    hit_y = rect_top <= pos[1] <= rect_bottom
    return hit_x and  hit_y  #the player has hit the box

box = [(50,50),(100,100)] #TL,BR
turtle_pos = (40,40)

check_bounds(turtle_pos,box)

of course you can simplify it.... just wanted it nice and verbose so OP could understand what's going on. 当然,您可以简化它。...只是希望它漂亮且冗长,以便OP可以了解发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM