基本上我有一些代码,它会找到一个平面的方程,然后尝试在一个列表中放置一个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()
发布于 2011-11-06 08:46:22
您的问题是由Bresenham's Algorithm完成的二维线绘制的三维版本。B-A使用网格中的单元格绘制一条线,包括连接单元格,以便获得一条连续的线。也就是说,B-A从一个单元格移动到另一个单元格,确定哪个相邻单元格最适合所绘制的线性方程,而不是逐列索引并计算等效x值的适当单元格(如果直线不是完全垂直、水平或45度,则会在点之间留出空格)。
通过绘制x中的每个线性切片,然后为y中的每个切片再次绘制,可以将其调整为3维。左边的实现作为OP的练习。维基百科的页面包括一些n维治疗的链接。
发布于 2011-11-05 23:54:32
如果我正确理解了您的意图,您有一个由三个点(p0,p1,p2)定义的平面,然后想要评估是否有其他点在该平面上(或非常接近于此)。
这最容易使用矩阵来表示,而不是通过操作上面列出的代码片段中的各个坐标组件来表示。这里有一个链接,展示了如何使用矩阵来解决这个问题和相关问题:http://paulbourke.net/geometry/planeeq/。
看起来您的代码已经接近于表示平面的方程(您可以通过替换原始点来验证它,看看它的计算结果是零还是接近零)。
接下来,替换候选点,看看它的计算结果是零还是接近零。
https://stackoverflow.com/questions/8024166
复制