📄 viewactionimagetool.cpp
字号:
// ViewActionImageTool.cpp: implementation of the CViewActionImageTool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "iplab.h"
#include "ViewActionImageTool.h"
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CViewActionImageTool::CViewActionImageTool()
{
m_nNum=0;
m_fCenterX=100;
m_fCenterY=100;
m_fRadius=50;
}
CViewActionImageTool::~CViewActionImageTool()
{
}
void CViewActionImageTool::SetEditToolOption(ImageToolOption option)
{
m_nImageToolOption = option;
}
ImageToolOption CViewActionImageTool::GetEditToolOption()
{
return m_nImageToolOption;
}
void CViewActionImageTool::OnLButtonUp(UINT nFlags, CPoint point)
{
switch(m_nImageToolOption) {
case TOOLEASTSQUARESFITTING:
//SelectRectLBUp(nFlags,point);
if (m_nNum>=MAXPOINTS)
{
return;
}
m_points[m_nNum] = point;
m_nNum++;
if (m_nNum>2)
{
LeastSquaresFitting();
}
this->GetParent()->Invalidate();
break;
default:
;
}
return;
}
void CViewActionImageTool::OnRButtonDown(UINT nFlags, CPoint point)
{
switch(m_nImageToolOption) {
case TOOLEASTSQUARESFITTING:
m_nNum=0;
m_fCenterX = 0;
m_fCenterY = 0;
m_fRadius = 50;
this->GetParent()->Invalidate();
break;
default:
;
}
return;
}
void CViewActionImageTool::Draw(CDC* pDC)
{
int i=0;
int lineWidth=3;
Graphics g(pDC->GetSafeHdc());
Color outlilneColor(220, 10, 10, 60);
Pen outlinePen1(outlilneColor,lineWidth);
Pen outlinePen2(Color(150,255,0,0),1);
if (m_fRadius>1)
{
g.DrawEllipse(&outlinePen2
,(int)(m_fCenterX-m_fRadius+0.5)
,(int)(m_fCenterY-m_fRadius+0.5)
,(int)(2*m_fRadius+0.5)
,(int)(2*m_fRadius+0.5)
);
}
for (i=0;i<m_nNum;i++)
{
g.DrawLine (&outlinePen1,Point(m_points[i].x,m_points[i].y),Point(m_points[i].x+3,m_points[i].y));
}
}
void CViewActionImageTool::LeastSquaresFitting()
{
// m_nNum = 4;
// m_points[0].x = 5;
// m_points[0].y = 0;
// m_points[1].x = 0;
// m_points[1].y = 5;
// m_points[2].x = -7;
// m_points[2].y = 0;
// m_points[3].x = 0;
// m_points[3].y = -5;
if (m_nNum<3)
{
return;
}
int i=0;
double X1=0;
double Y1=0;
double X2=0;
double Y2=0;
double X3=0;
double Y3=0;
double X1Y1=0;
double X1Y2=0;
double X2Y1=0;
for (i=0;i<m_nNum;i++)
{
X1 = X1 + m_points[i].x;
Y1 = Y1 + m_points[i].y;
X2 = X2 + m_points[i].x*m_points[i].x;
Y2 = Y2 + m_points[i].y*m_points[i].y;
X3 = X3 + m_points[i].x*m_points[i].x*m_points[i].x;
Y3 = Y3 + m_points[i].y*m_points[i].y*m_points[i].y;
X1Y1 = X1Y1 + m_points[i].x*m_points[i].y;
X1Y2 = X1Y2 + m_points[i].x*m_points[i].y*m_points[i].y;
X2Y1 = X2Y1 + m_points[i].x*m_points[i].x*m_points[i].y;
}
double C,D,E,G,H,N;
double a,b,c;
N = m_nNum;
C = N*X2 - X1*X1;
D = N*X1Y1 - X1*Y1;
E = N*X3 + N*X1Y2 - (X2+Y2)*X1;
G = N*Y2 - Y1*Y1;
H = N*X2Y1 + N*Y3 - (X2+Y2)*Y1;
a = (H*D-E*G)/(C*G-D*D);
b = (H*C-E*D)/(D*D-G*C);
c = -(a*X1 + b*Y1 + X2 + Y2)/N;
double A,B,R;
A = a/(-2);
B = b/(-2);
R = sqrt(a*a+b*b-4*c)/2;
/*1111*/
/*
double a,b,c,d,e,n;
double A,B,C,R;
n = m_nNum;
a = 2*n*X2-4*X1*X1;
b = 2*n*X1*Y1-4*X1*Y1;
c = n*(X3+X1Y2)-2*X1*(X2+Y2);
d = 2*n*Y2-4*Y1*Y1;
e = n*(X2Y1+Y3)-2*Y1*(X2+Y2);
A = (c*d-e*b)/(a*d-b*b);
B = (c*b-e*a)/(b*b-d*a);
C = (X2+Y2-A*2*X1-B*2*Y1)/n;
R = sqrt (A*A+B*B+C);
*/
m_fCenterX = A;
m_fCenterY = B;
m_fRadius = R;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -