const float pi = 3.1415926;
/***********************************************************************************************
* 添加坐标系及刻度 *
*=============================================================================================*/
void XXXXXXXX::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
//绘制坐标系
CDC *pdc = GetDC();
pdc->MoveTo(40, 0);
pdc->LineTo(40, 360);
pdc->MoveTo(40, 360);
pdc->LineTo(640, 360);
//绘制坐标刻度
for (int i = 1; i < 18; i++) {
pdc->MoveTo(40, 0 + i * 20);
pdc->LineTo(45, 0 + i * 20);
}
for (int i = 1; i < 30; i++) {
pdc->MoveTo(40 + i * 20, 360);
pdc->LineTo(40 + i * 20, 355);
}
ReleaseDC(pdc);
}
/***********************************************************************************************
* 坐标转换函数 *
*=============================================================================================*/
int XXXXXXXX::X_to_X(int x) //X轴坐标转换
{
x = x + 40;
return x;
}
int XXXXXXXX::Y_to_Y(int y) //Y轴坐标转换
{
y = 360 - y;
return y;
}
/***********************************************************************************************
* 角度画弧函数,输入:圆心X坐标,Y坐标,半径,起始角度,终止角度逆时针画圆 *
*=============================================================================================*/
void XXXXXXXX::angle_arc(float RX, float RY, float radius, float angle_start, float angle_end)
{
CDC *pdc = GetDC();
float rad_start = angle_start * pi / 180; //起始角度转化为弧度rad_start
float rad_end = angle_end * pi / 180; //终止角度转化为弧度rad_end
float x1 = radius * cos(rad_start) + RX; //起始点横坐标x1
float y1 = radius * sin(rad_start) + RY; //起始点纵坐标y1
float x2 = radius * cos(rad_end) + RX; //终止点横坐标x2
float y2 = radius * sin(rad_end) + RY; //终止点纵坐标y2
pdc->Arc(X_to_X(RX - radius), Y_to_Y(RY + radius), X_to_X(RX + radius), Y_to_Y(RY - radius), X_to_X(x1), Y_to_Y(y1), X_to_X(x2), Y_to_Y(y2));
ReleaseDC(pdc);
}
/***********************************************************************************************
* 三点画圆弧函数,输入三点坐标进行画弧 *
*=============================================================================================*/
void XXXXXXXX::three_point_to_arc(int x1, int y1, int x2, int y2, int x3, int y3)
{
CDC *pdc = GetDC();
float a = x1 - x2;
float b = y1 - y2;
float c = x1 - x3;
float d = y1 - y3;
float e = 1.0*((x1 * x1 - x2 * x2) + (y1 * y1 - y2 * y2)) / 2.0;
float f = 1.0*((x1 * x1 - x3 * x3) + (y1 * y1 - y3 * y3)) / 2.0;
float det = b * c - a * d;
float x0 = -1.0*(d * e - b * f) / det;
float y0 = -1.0*(a * f - c * e) / det;
float RADIUS = sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0));
pdc->Arc(x0 - RADIUS, y0 + RADIUS, x0 + RADIUS, y0 - RADIUS, x3, y3, x1, y1);
ReleaseDC(pdc);
}
MFC画弧函数(三点画弧,角度画弧)
最新推荐文章于 2024-09-25 16:50:55 发布