如何拟合方程的最近点? Python
基本上我有一些代码,它将找到一个平面的方程,然后如果一个点满足这个直线方程,则尝试将 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()
Basically I have some code for which it will find the equation of a plane and then attempt to place a 1 in a list if a point satisfies this equation of a line, otherwise a 0 is put in for the list.
Unfortunately, there has to be an increment amount, so how would you get a point that is closest to the equation so that an approximate plane can be made without a bunch of empty spaces?
Here is the code so far:
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)
Here is an example code for drawing a line that works with the closest points to the actual line being used:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是 Bresenham 算法。 BA 使用网格中的单元格绘制一条线,包括连接单元格,以便获得一条连续的线。也就是说,BA 不只是逐列索引并计算等效 x 值的正确单元格(如果线不完全垂直、水平或 45 度,则在点之间留下空格),BA 从一个单元格移动到另一个单元格,确定哪个相邻单元格最适合绘制的线性方程。
可以通过绘制 x 中的每个线性切片,然后再次绘制 y 中的每个切片来将其适应 3 维。实施留给OP作为练习。维基百科页面包含一些 n 维处理的链接。
Your problem is a 3-dimensional version of the 2-dimensional line-plotting done by Bresenham's Algorithm. B-A plots a line using cells in a grid, including connecting cells so that you get a continuous line. That is, instead of just indexing column by column and computing the proper cell for the equivalent x-value (which would leave spaces between points if the line is not exactly vertical, horizontal, or 45degrees), B-A moves from cell to cell, determining which adjacent cell is best to fit to the linear equation being plotted.
Adapting this to 3-dimensions might be done by plotting each linear slice in x, and then again for each slice in y. Implementation left as an exercise for the OP. The Wikipedia page includes some links for n-dimensional treatments.
如果我正确理解你的意图,你有一个由三个点(p0,p1,p2)定义的平面,然后想要评估其他点是否位于该平面上(或非常接近)。
使用矩阵最容易表达这一点,而不是通过操作上面列出的代码片段中的各个坐标分量。以下链接显示了如何使用矩阵来解决此问题及相关问题: http://paulbourke.net/geometry/planeeq / 。
看起来您的代码已经接近表示平面方程(您可以通过替换原始点来验证它,看看它的计算结果是否为零或接近零)。
接下来,替换候选点以查看其计算结果是否为零或接近零。
If I'm understanding your intent correctly, you have a plane defined by three points (p0, p1, p2) and then want to evaluate whether some other point lies in that plane (or very nearly so).
This is most easily expressed using matrices rather than by manipulating the individual coordinate components in the code snippet listed above. Here's a link showing how to use matrices to solve this and related problems: http://paulbourke.net/geometry/planeeq/ .
It looks like your code is already close to representing the equation of the plane (you can verify it by substituting in the original points to see if it evaluates to zero or near zero).
Next, substitute the candidate point to see whether it evaluates to zero or near zero.