从边界内行进的距离获取位置-X

时间:2017-05-15 00:40:11

标签: python

我有两个相反方向移动的蓝点。我给出了X轴的尺寸限制,点可以移动。在这个例子中,总共300个单位(从0开始的任一方向150个)

每个蓝点从0(中心)开始,我得到每个点移动的总距离。我在编写一个函数时遇到了麻烦,该函数返回了我生成的X位置。这由下图中的浅蓝色点表示。红线表示球可以在其中移动的边界,将红线视为墙壁,将蓝点视为弹跳球。

我的目标是创建一个函数,在python中返回这个值。

enter image description here

你会在我的函数中看到我有一个参数dir,用于确定球的移动方向(最初左或右)

以下是我尝试解决它,但这并不完全正确。当%存在余数时似乎失败了。

def find_position(dir=1, width=10, traveled=100):
    dist = traveled
    x = dist % (width*0.5)
    print x

find_position(dir=1, width=300, traveled=567)
find_position(dir=1, width=300, traveled=5)
find_position(dir=-1, width=300, traveled=5)
find_position(dir=-1, width=300, traveled=325)


>> output
117.0
5.0
5.0
25.0

>> should be
-33.0
5.0
-5.0
25.0

4 个答案:

答案 0 :(得分:1)

以下将做,m的符号表示方向(您可以通过制作150和300参数轻松使其更通用):

def x(m):
    d = 150 + m
    # what matters the most is whether it bounced even times or odd times
    return (1 if d // 300 % 2 == 0 else -1) * (d % 300 - 150)    

assert x(567) == -33
assert x(-325) == 25

assert x(-100) == -100
assert x(-150) == -150
assert x(-160) == -140
assert x(-300) == 0
assert x(-310) == 10
assert x(-450) == 150 
assert x(-460) == 140

assert x(100) == 100
assert x(150) == 150
assert x(160) == 140
assert x(300) == 0
assert x(310) == -10 
assert x(450) == -150
assert x(460) == -140

答案 1 :(得分:1)

这是我的问题代码。我在代码中留下了评论,使其更加自我解释。但是你不应该期望当它从0开始行进-325时X的坐标是-25

def find_position(dir=1, width=10, traveled=100):
    # coordinate translation
    curr_X = 150

    if traveled >= width:
        # remove periodicity in distance traveled
        dist = traveled % width * dir
    else:
        dist = traveled * dir

    new_X = ( dist + curr_X ) % width

    # translate coordinate back to original configuration
    new_X = new_X - width * 0.5
    return new_X

assert find_position(dir=1, width=300, traveled=567) == -33
assert find_position(dir=1, width=300, traveled=5) == 5
assert find_position(dir=-1, width=300, traveled=5) == -5
assert find_position(dir=-1, width=300, traveled=325) == -25

答案 2 :(得分:1)

使用%和绝对值:

<强>代码:

def find_position(direction=1, width=10, traveled=100):
    half_width = width / 2
    t = abs((traveled + half_width) % (2 * width) - width) - half_width
    return -t * direction

测试代码:

assert int(find_position(direction=1, width=300, traveled=567)) == -33.0
assert int(find_position(direction=1, width=300, traveled=5)) == 5
assert int(find_position(direction=-1, width=300, traveled=5)) == -5
assert int(find_position(direction=-1, width=300, traveled=325)) == 25

答案 3 :(得分:0)

def find_position(dir=1, width=10, traveled=100):
    # If we have traveled less than half the width, just return 
    # the traveled distance
    if traveled < (width / 2):
        return traveled * dir

    dist = traveled % width

    # If the remainder is less than half the board, we know we are not 
    # "halfway" around the board, so we can just return the distance again
    if dist <= (width / 2):
        return dist * dir

    # Here we have wrapped around the final round, so we subtract the width 
    # of the board to get the final distance
    return (width - dist) * dir * -1

输出:

>>> find_position(dir=1, width=300, traveled=567)
-33
>>> find_position(dir=1, width=300, traveled=5)
5
>>> find_position(dir=-1, width=300, traveled=5)
-5
>>> find_position(dir=-1, width=300, traveled=325)
-25