Python地区地图坐标

2024-04-18 16:48:55 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题有点棘手,但我希望你能帮我。 我收到了一个编码任务,我在解决问题的方法上遇到了一些困难。在

情况是这样的:假设我有一件武器在坐标系(X,Y);当它开火时,一个声音伤害函数会影响到离枪很近的人。我需要写一个函数,根据发射的口径来计算一个人(person类的一个实例)是否在危险区域。枪发出的声音以45度角传播,半径随发射口径的变化而变化(但这是我预先确定的,所以假设半径等于5)。 我的解释可能有点混乱,所以我将试着把它组织起来,使事情更清楚:我需要编写一个函数,每当有人开枪时,这个函数就会被调用。该功能需要检查一个人是否处于危险的靠近被发射的枪的位置,就好像他是,他将受到损害他的听力(由一个单独的,预先制作的功能)。损害程度根据人与枪的相对距离而定。这个面积是用一个圆锥来测量的,这个圆锥体在一个随机半径内以45度的角度展开枪的边缘。在

我对如何解决这个问题有点困惑。我已经写了一个函数来计算每个“人”的坐标。但是我需要一种方法来绘制出撞击区域的所有坐标,这样我就可以a)判断这个人是否应该受到声损伤函数的影响,以及b)知道他被击中的程度(因为如果他站在离枪1米远的地方,而不是4米远的地方,伤害会更严重)。在

也许我用一种错误的方式来处理这个问题——我想学习并乐于听取任何建议。无论如何我都不是专家,我很想知道如何以最有效的方式解决这个问题。在

编辑:到目前为止,我编写的代码如下:

class Soldier(object):
def __init__(self):
    # Soldier ID is a randomly generated string including of 3 numbers.
    ID = str(int(random.random() * 1000))
    self.ID = ID
    # soldier's position is set by default to [0,0]
    self.position = [0,0]
def getID(self):
    # gets the ID number (string) of the instance
    print (self.ID)
def setPosition(self, position):
    # sets the position (a list) of the instance
    self.position = position
def getPosition(self):
    # gets the position (list) of the instance
    print(self.position)

def randomize_soldier_position(soldiersList):
"""
Gets as input a list of instances of soldiers, and assigns a random location to each one of the instances.
"""
for i in soldiersList:
    # for the values of X and Y we generate a random floating point number extending to 3 digits. 
    X = round((random.random() * 100), 3)
    Y = round((random.random()* 100), 3)
    # sets the [X,Y] values for each of the instances.
    i.setPosition([X,Y])
    print(i.position)

我还没有接触到我需要编写的实际函数的代码,因为我甚至不知道从哪里开始。。。在

提前谢谢!:)


Tags: ofthetoinstancesinstance函数selfid
1条回答
网友
1楼 · 发布于 2024-04-18 16:48:55

假设你有一个欧几里德二维坐标网格和角度参考

枪应具有x、y位置和相对于全局坐标轴的角度
如果枪有一个方向向量而不是一个角度,只需应用math.atan2(x, y),这将以弧度返回角度

从人的全局x,y位置减去枪x,y得到枪与人的相对矢量,用atan2计算角度,用毕达哥拉斯math.hypot(x, y)计算距离

然后测试一下这个人是否在角度范围和距离范围内

相关问题 更多 >