0

基本上我有一些代码,它会找到一个平面的方程,然后如果一个点满足这条线的方程,然后尝试将 1 放入列表中,否则将 0 放入列表中。不幸的是,必须有一个增量,那么你如何得到一个最接近方程的点,以便可以在没有一堆空白的情况下制作一个近似平面?

这是到目前为止的代码:

def plane(self):
    p1 = self.first_pos
    p2 = self.second_pos
    p3 = self.third_pos
    x1,y1,z1 = self.fourth_pos
    x2,y2,z2 = self.fifth_pos
    a = (p2[0] - p1[0],p2[1] - p1[1],p2[2] - p1[2])
    b = (p3[0] - p1[0],p3[1] - p1[1],p3[2] - p1[2])
    abc = ((a[1] * b[2]) - (a[2] * b[1]),(a[2] * b[0]) - (a[0] * b[2]), (a[0] * b[1]) - (a[1] * b[0]))
    constant = (p1[0] *abc[0] * -1) - (p1[1] * abc[1]) - (p1[2] * abc[2])
    lx = []
    lxy = []
    axyz = []
    if x1 > x2 : x1, x2 = x2, x1
    if y1 > y2 : y1, y2 = y2, y1
    if z1 > z2 : z1, z2 = z2, z1
    for z in range(z1, z2+1):
        for y in range(y1,y2+1):
            for x in range(x1,x2+1):
                if int(round(((abc[1] *y) + (abc[2] *z) + constant + 0.6 ) / (-1 * abc[0]))) == x:
                    lx.append(1)
                else:
                    lx.append(0)
                if x == x2:
                    lxy.append(lx)
                    lx = []
            if y == y2:
                axyz.append(lxy)
                lxy = []
    self.first_pos = self.fourth_pos
    self.second_pos = self.fifth_pos
    self.buildMatrix(axyz)
    self.BuildCuboid(axyz)

这是一个示例代码,用于绘制一条线,该线与正在使用的实际线的最近点一起工作:

def DrawLine(self):
    self.bot.sendMessage("Drawing line.",ignorable=True)
    fp = self.first_pos
    sp = self.second_pos
    ## This is the vector from pt 1 to pt 2
    x,y,z = sp[0] - fp[0], sp[1] - fp[1], sp[2] - fp[2]

    ## magnitude of that vector
    dist = self.bot.dist3d(fp[0], fp[1], fp[2], sp[0], sp[1], sp[2] )

    ## unit vector
    n_x, n_y, n_z = x/dist, y/dist, z/dist

    ## stepping a dist of 1 in the direction of the unit vector, find the
    ## whole coordinate and place a block at that location
    coords = []
    for d in xrange(0, int(dist)):
        self.blocks.append( (
                       self.block_type,
                       int(round(fp[0] + (n_x * d))),
                       int(round(fp[1] + (n_y * d))),
                       int(round(fp[2] + (n_z * d)))
                       ) )
    self.DrawBlocks()
4

2 回答 2

0

如果我正确理解您的意图,您有一个由三个点(p0、p1、p2)定义的平面,然后想要评估其他点是否位于该平面内(或非常接近)。

这最容易使用矩阵来表达,而不是通过操纵上面列出的代码片段中的各个坐标分量。这是一个链接,展示了如何使用矩阵来解决这个问题和相关问题: http: //paulbourke.net/geometry/planeeq/

看起来您的代码已经接近于表示平面方程(您可以通过代入原始点来验证它的计算结果是否为零或接近零)。

接下来,替换候选点以查看它的计算结果是零还是接近零。

于 2011-11-05T23:54:31.533 回答
0

您的问题是Bresenham's Algorithm完成的二维线图的 3 维版本。BA 使用网格中的单元格绘制一条线,包括连接单元格,以便获得一条连续的线。也就是说,BA 不只是逐列索引并计算等效 x 值的适当单元格(如果线不完全垂直、水平或 45 度,这将在点之间留下空间),BA 从一个单元格移动到另一个单元格,确定哪个相邻单元最适合绘制的线性方程。

可以通过在 x 中绘制每个线性切片,然后再为 y 中的每个切片绘制来使其适应 3 维。实施留作 OP 的练习。Wikipedia 页面包含一些用于 n 维处理的链接。

于 2011-11-06T08:46:22.257 回答