python实现三边算法

以下是Python实现的三边定位算法的一部分,想问一下当“idx等于0时,计算的是目标点到第4个信标的距离。当idx不等于0时,计算的是目标点到第1个信标的距离”如此计算的理由,还有这部分的数学表达式是什么?


 
self.position = np.array([[-0.5, -0.5],
                                  [5.5, -0.5],
                                  [5.5, 5.5],
                                  [-0.5, 5.5]])
        self.distances = []
        self.result = 0
 
    def trilaterate2D(self):
        A = []
        B = []
        # trilateration using SVD
        for idx in range(4):
            if idx == 0:  # i:1 j:4
                x_coefficient = self.position[3][0] - self.position[idx][0]  # x1-xidx
                y_coefficient = self.position[3][1] - self.position[idx][1]  # y1-yidx
                b = 1 / 2 * (self.distances[idx] ** 2 - self.distances[3] ** 2 -
                             ((self.position[idx][0] - self.position[3][0]) ** 2 + (
                                     self.position[idx][1] - self.position[3][1]) ** 2)) \
                    + x_coefficient * self.position[3][0] + y_coefficient * self.position[3][1]
                A.append([x_coefficient, y_coefficient])
                B.append([b])
            else:
                x_coefficient = self.position[0][0] - self.position[idx][0]  # x1-xidx
                y_coefficient = self.position[0][1] - self.position[idx][1]  # y1-yidx
                b = 1 / 2 * (self.distances[idx] ** 2 - self.distances[0] ** 2 -
                             ((self.position[idx][0] - self.position[0][0]) ** 2 + (
                                     self.position[idx][1] - self.position[0][1]) ** 2)) \
                    + x_coefficient * self.position[0][0] + y_coefficient * self.position[0][1]
                A.append([x_coefficient, y_coefficient])
                B.append([b])
        B = np.array(B)
        A_pseudo = np.linalg.pinv(A)
        self.result = np.dot(A_pseudo, B)
        result_x = self.result[0]
        result_y = self.result[1]
        # return x, y position
        return result_x, result_y