基本上我有一些代码,它会找到一个平面的方程,然后尝试在列表中放置一个1,如果一个点满足这个方程的一条线,否则将0放入列表中。 不幸的是,必须有一个增量量,那么如何得到一个最接近方程的点,以便可以在没有一堆空白空间的情况下制作一个近似平面?如何拟合方程的最近点? Python
这里是到目前为止的代码:
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()
它不是很清楚这是一个编程问题还是一个数学问题 - 不管这里有很多代码,但没有很多上下文。我不确定没有更多信息我们可以帮助你。 – jedwards
问题是,怎样才能使得结果列表的表面尽可能接近实际平面,因为现在,如果平面不是水平的,垂直的或两个轴之间的关系是1, t是一个表面,而是1之间的散点图,0之间的散点图。 – joseph