Python 中某个区域的地图坐标

Map coordinates of an area in Python

有点棘手的问题,但我希望你能帮助我。 我收到了一份编码作业,但在寻找解决方案时遇到了一些问题。

情况是这样的:假设我有一个坐标为(X,Y)的武器;当它开火时,声音伤害功能就会启动,影响靠近枪支的人。我需要编写一个函数来计算 - 根据被发射的口径 - 危险区域中是否存在一个人(Person class 的一个实例)。枪声从 45 度角传播出去,半径根据发射的口径而变化(但由我预先确定,所以让我们假设半径等于 5)。 我的解释可能有点乱,所以我会尝试稍微组织一下以使事情更清楚:我需要编写一个函数,每当开枪时都会调用该函数。该功能需要检查一个人是否在危险地靠近正在开火的枪支附近,就好像他的听力会受到损害一样(通过单独的预制功能)。伤害根据人与枪的相对距离而定。该区域是在一个圆锥体中测量的,该圆锥体以随机半径以 45 度角展开枪的边缘。

我对如何解决这个问题有点困惑。我已经写了一个函数来计算每个 "Person" 的坐标。但是我需要一种方法来绘制出撞击区域的所有坐标,以便我可以 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)

我还没有触及我需要编写的实际函数的代码,因为我什至不知道从哪里开始...

提前致谢! :)

假设您有欧几里得二维坐标网格和角度参考

枪应该有一个 x,y 位置和它指向的相对于全局坐标轴的角度
如果枪有方向矢量而不是角度,只需应用 math.atan2(x, y),这将 return 以弧度表示的角度

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

然后就测试人是否在角度范围和距离范围内