姓名:卜凡

学号:2020211066

指导老师:符强

日期:2022-06-30

综述

本项目是一个打靶游戏,实现的功能和模块如下:

Untitled

本报告划分出的各模块与代码模块相互照应,顺序一致。

下面是目录。

1 向量类与矩阵类的实现

本项目并没有使用 Numpy 库提供矩阵运算,而是自己实现了表示点或向量的 Point 类和表示 $3 \times 3$ 矩阵的 Matrix3x3 类。

1.1 点/向量类 Point 的实现

表示三维坐标中的坐标点或向量的 Point 类,主要由 xyz 三个坐标构成。在代码中已经实现完成,下面是方法列表。

Untitled

注1:这里的 Number 包括了整数或浮点数。

注2:标记有 [*] 的方法都是在具有可变参数的构造函数 __init__ 中判断参数类型而进行手动重载,如下部分代码:

# 表示三维坐标中的点或者向量。
class Point(object):

		def __init__(self, *args):

        # [0 参数] 默认构造方法:x 与 y 默认值均为 0
        if (len(args) == 0):
            self.x = self.y = self.z = 0.0

        # [1 参数] 复制构造方法
        elif (len(args) == 1 and isinstance(args[0], Point)):
            other = args[0];
            self.x = other.x
            self.y = other.y
            self.z = other.z

        # [1 参数] 构造方法,三参数均为同一值
        elif (len(args) == 1 and isinstance(args[0], (int, float))):
            value = args[0]
            self.x = self.y = self.z = value

        # [3 参数] 构造方法
        elif (len(args) == 3 and isinstance(args[0], (int, float))
                             and isinstance(args[1], (int, float))
                             and isinstance(args[2], (int, float))):
            self.x = args[0]
            self.y = args[1]
            self.z = args[2]

        # [其他参数] 抛出异常,表示参数不合法。
        else:
            raise ValueError("invalid args", args)

注3:表中带有运算符表示的方法均重载了运算符(运算符大全可参考附录中资料 [1]),如下:

# 定义 / 运算符:求两 Point 之间距离
    def __truediv__(self, other) -> float:
        return math.sqrt((self.x - other.x) ** 2
                         + (self.y - other.y) ** 2
                         + (self.z - other.z) ** 2)

顺便定义了帮助函数 averagePoints(List[Point]) -> Point,以求得由点构成的数组的平均点,在 2.3 节中会再次提到。

1.2 3x3 矩阵类 Matrix3x3 的实现

在代码中已经实现完成。该矩阵类仅用于在碰撞时进行一些坐标系变换,因而并没有实现得非常全面,只是实现了下面这些与坐标系变换相关的必要方法。