📄 pldistancedlg.cpp
字号:
// PLDistanceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PLDistance.h"
#include "PLDistanceDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPLDistanceDlg dialog
CPLDistanceDlg::CPLDistanceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPLDistanceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPLDistanceDlg)
m_dXofPoint = 0.0;
m_dYofPoint = 0.0;
m_dZofPoint = 0.0;
m_dNA = 0.0;
m_dNB = 0.0;
m_dNC = 0.0;
m_dDistance = 0.0;
m_dX1 = 0.0;
m_dY1 = 0.0;
m_dZ1 = 0.0;
m_dX2 = 0.0;
m_dY2 = 0.0;
m_dZ2 = 0.0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPLDistanceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPLDistanceDlg)
DDX_Text(pDX, IDC_EDIT5, m_dXofPoint);
DDX_Text(pDX, IDC_EDIT6, m_dYofPoint);
DDX_Text(pDX, IDC_EDIT7, m_dZofPoint);
DDX_Text(pDX, IDC_EDIT8, m_dNA);
DDX_Text(pDX, IDC_EDIT9, m_dNB);
DDX_Text(pDX, IDC_EDIT10, m_dNC);
DDX_Text(pDX, IDC_EDIT12, m_dDistance);
DDX_Text(pDX, IDC_EDIT1, m_dX1);
DDX_Text(pDX, IDC_EDIT2, m_dY1);
DDX_Text(pDX, IDC_EDIT3, m_dZ1);
DDX_Text(pDX, IDC_EDIT4, m_dX2);
DDX_Text(pDX, IDC_EDIT13, m_dY2);
DDX_Text(pDX, IDC_EDIT14, m_dZ2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPLDistanceDlg, CDialog)
//{{AFX_MSG_MAP(CPLDistanceDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_COMPUTE, OnCompute)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPLDistanceDlg message handlers
BOOL CPLDistanceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPLDistanceDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPLDistanceDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPLDistanceDlg::OnCompute()
{
UpdateData();
//1,先求直线的方向矢量l=(x2,y2,z2)-(x1,y1,z1)
m_dlx=m_dX2-m_dX1;
m_dly=m_dY2-m_dY1;
m_dlz=m_dZ2-m_dZ1;
//已知直线的方向矢量归一
double ds=m_dlx*m_dlx+m_dly*m_dly+m_dlz*m_dlz;
ds=sqrt(ds);
if(ds==0)
{
m_dlx=0;
m_dly=0;
m_dlz=0;
}
else
{
m_dlx=m_dlx/ds;
m_dly=m_dly/ds;
m_dlz=m_dlz/ds;
}
//2,求d
double dk=((m_dXofPoint-m_dX1)*m_dly-(m_dYofPoint-m_dY1)*m_dlx);
double di=((m_dYofPoint-m_dY1)*m_dlz-(m_dZofPoint-m_dZ1)*m_dly);
double dj=((m_dZofPoint-m_dZ1)*m_dlx-(m_dXofPoint-m_dX1)*m_dlz);
m_dDistance=sqrt(dk*dk+di*di+dj*dj)/ds;
//3,过已知点和距离的平面与已知直线的交点,该平面的法向就是已知直线的方向矢量
//平面:A(x-x0)+B(y-y0)+C(z-z0)=0 ,
//方向矢量为 {A,B,C}={m_dlx,m_dly,m_dlz}
//过点M{x0,y0,z0}={m_dXofPoint,m_dYofPoint,m_dZofPoint}
//直线:x=x0+lt;y=y0+mt;z=z0+nt;
//{x0,y0,z0}={m_dX1,m_dY1,m_dZ1}
//{l,m,n}={m_dlx,m_dly,m_dlz}
//所求交点的参数t为
double t=0;
t = (m_dlx*(m_dXofPoint-m_dX1)+m_dly*(m_dYofPoint-m_dY1)+m_dlz*(m_dZofPoint-m_dZ1))/(ds*ds);
//4,得到点T的坐标,则连接T和M,为距离直线的方向
//T:{m_dX1+m_dlx*t,m_dY1+m_dly*t,m_dZ1+m_dlz*t}
//M:{m_dXofPoint,m_dYofPoint,m_dZofPoint}
m_dNA=m_dXofPoint-m_dX1+m_dlx*t;
m_dNB=m_dYofPoint-m_dY1+m_dly*t;
m_dNC=m_dZofPoint-m_dZ1+m_dlz*t;
//所求向量归一
ds=m_dNA*m_dNA+m_dNB*m_dNB+m_dNC*m_dNC;
ds=sqrt(ds);
if(ds==0)
{
m_dNA=0;
m_dNB=0;
m_dNC=0;
}
else
{
m_dNA=m_dNA/ds;
m_dNB=m_dNB/ds;
m_dNC=m_dNC/ds;
}
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -