- 直线参数方程
如果是在二维坐标系下,已知两个点p1(x1, y1), p2(x2, y2), 很容易求得两点之间的斜率,然后使用y = kx + b计算出k,b值得到直线方程。同理在三维空间下仍然可以使用直线参数方程。
x = x1 + dx * t,
y = y1 + dy * t,
z = z1 + dz * t
例如:
已知p1(x1, y1, z1), p2(x2, y2, z2)个点,可以求得p1p2两个点的方向向量d。
d = p2 - p1 = ((x2 - x1), (y2 - y1), (z2 - z1))。
可以求得t = |d| / √d。
- c++代码求解方程
a.首先定义点的结构
struct Points3D
{
float x;
float y;
float z;
};
b . 计算两个点的方向向量
void LinearEquation::GetDirection(float * Direction)
{Direction[0] = m_p2.x - m_p1.x;
Direction[1] = m_p2.y - m_p1.y;
Direction[2] = m_p2.z - m_p1.z;
}
c.然后计算向量模长
float GetLength(float* Matrix)
{
float x = pow(Matrix[0], 2);
float y = pow(Matrix[1], 2);
float z = pow(Matrix[2], 2);
return sqrt(x + y + z);
}
d.得到参数t以及dx,dy,dz,主要代码如下
void GetEquationParameters(float& dx, float& dy, float& dz, float& t)
{
float Direction[3];
GetDirection(Direction);
///得到两点的方向向量 float L_D = GetLength(Direction);
//得到方向向量的模长 float std_D[3];
memcpy(std_D, Direction, sizeof(Direction));
GetNormVector(std_D);
//得到单位向量 dx = std_D[0];
dy = std_D[1];
dz = std_D[2];
float L_std_D = GetLength(std_D);
t = L_D / L_std_D;
//t = |d| / √d;
}
e. 得到两点之间的所有点,interval代表采点的间隔。
void GetLinearPoints(float interval, std::vector& points)
{
float dx, dy, dz, t;
GetEquationParameters(dx, dy, dz, t);
for (int i = 0;
i < int(m_NumPoints / interval);
i++){
float x = m_p1.x + dx * t * interval * i;
float y = m_p1.y + dy * t * interval * i;
float z = m_p1.z + dz * t * interval * i;
float dis = sqrt(pow(m_p2.x - x, 2) + pow(m_p2.y - y, 2) + pow(m_p2.z - z, 2));
if (dis < 1.0)
break;
Points3D pt;
pt.x = x;
pt.y = y;
pt.z = z;
points.push_back(pt);
}}
- 使用VTK进行点绘制显示
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL#include
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif//不加这5行会报错#include "vtkPoints.h"
#include "vtkSmartPointer.h"
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"void VtkDrawPoints(vector pt)
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer vertices = vtkSmartPointer::New();
vtkSmartPointer polydata = https://www.it610.com/article/vtkSmartPointer::New();
vtkSmartPointer mapper = vtkSmartPointer::New();
vtkSmartPointer actor = vtkSmartPointer::New();
vtkSmartPointer render = vtkSmartPointer::New();
vtkSmartPointer window = vtkSmartPointer::New();
vtkSmartPointer win_render = vtkSmartPointer::New();
int Num = pt.size();
for (int i = 0;
i < Num;
i++) {
vtkIdType id = points->InsertNextPoint(pt[i].x, pt[i].y, pt[i].z);
vertices->InsertNextCell(1);
vertices->InsertCellPoint(id);
}
polydata->SetPoints(points);
polydata->SetVerts(vertices);
mapper->SetInputData(polydata);
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(255, 0, 0);
actor->GetProperty()->SetPointSize(2);
render->AddActor(actor);
render->SetBackground(0, 0, 0);
window->AddRenderer(render);
window->SetSize(600, 600);
win_render->SetRenderWindow(window);
win_render->Initialize();
win_render->Start();
}
【c++|(c++)已知空间三维两个点坐标,得到直线方程以及两点之间所有的点,使用VTK进行绘制显示】完整代码可以访问此链接c++求空间直线方程并使用vtk进行绘制显示。
推荐阅读