19,468
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <winuser.h>
#include <math.h>
#define Pi 3.1415926
const int r = 200;
POINT pts[12];
int num = 7;
double max, min;
int color[12][12];
int Ellipse_color[60][60];
int rgb[20][3];
double divide[20];
int flag;
double arr[144]={
0,0.85,0.55,0.68,0.69,0.88,1.00,1.11,1.36,1.38,1.22,0.89,
0.85,0,0.57,0.45,0.51,0.70,0.80,0.89,1.09,1.18,1.09,0.85,
0.55,0.57,0,0.17,0.20,0.37,0.49,0.54,0.63,0.69,0.69,0.57,
0.68,0.45,0.17,0,0.21,0.49,0.61,0.69,0.82,0.90,0.91,0.77,
0.69,0.51,0.20,0.21,0,0.44,0.53,0.62,0.79,0.91,0.85,0.72,
0.88,0.70,0.37,0.49,0.44,0,0.58,0.92,1.02,1.15,1.13,0.95,
1.00,0.80,0.49,0.61,0.53,0.58,0,1.22,1.19,1.24,1.21,1.08,
1.11,0.89,0.54,0.69,0.62,0.92,1.22,0,0.64,1.43,1.32,1.19,
1.36,1.09,0.63,0.82,0.79,1.02,1.19,0.64,0,1.67,1.51,1.36,
1.38,1.18,0.69,0.90,0.91,1.15,1.24,1.43,1.67,0,1.27,1.40,
1.22,1.09,0.69,0.91,0.85,1.13,1.21,1.32,1.51,1.27,0,1.50,
0.89,0.85,0.57,0.77,0.72,0.95,1.08,1.19,1.36,1.40,1.50,0};
void Findmax_min (double* arr);
void Determine_color (double* arr);
void Determine_Ellcolor ();
void Determine_divide ();
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[] = "窗口"; //窗口类名
char lpszTitle[] = "My_Windows"; //窗口标题名
//窗口类的定义
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;//窗口类型为默认类型
wndclass.lpfnWndProc = WndProc; //窗口处理函数为WndProc
wndclass.cbClsExtra = 0; //窗口类无扩展
wndclass.cbWndExtra = 0; //窗口实例无扩展
wndclass.hInstance = hInstance; //当前实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //窗口最小化图标为默认图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //窗口采用箭头光标
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); //窗口背景为白色
wndclass.lpszMenuName = NULL; //窗口中无菜单
wndclass.lpszClassName = lpszClassName; //窗口类名为"窗口示例"
//窗口类的注册
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd = CreateWindow (
lpszClassName, //窗口类名
lpszTitle, //窗口实例的标题名
WS_OVERLAPPEDWINDOW,//窗口的风格
CW_USEDEFAULT, //默认窗口初始x坐标
CW_USEDEFAULT, //默认窗口初始y坐标
CW_USEDEFAULT, //默认窗口初始x大小
CW_USEDEFAULT, //默认窗口初始y大小
NULL, //窗口无父窗口
NULL, //窗口无主菜单
hInstance, //创建此窗口应用程序的当前句柄
NULL //不适用该值
);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&Msg, NULL, 0, 0))//如果函数取得WM_QUIT之外的其他消息,返回非零值。如果函数取得WM_QUIT,返回零
{
TranslateMessage(&Msg);//讲按键消息转换为字符消息
DispatchMessage(&Msg);//以此消息为参数转发给相应的窗口消息处理程序
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i, j;
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush; //定义指向画刷的句柄
HPEN hPen; //定义指向画笔的句柄
RECT rect;
POINT pt;
SIZE size;
TEXTMETRIC tm;
char a[255];
switch (message)
{
case WM_CREATE:
Findmax_min(arr);
Determine_divide();
Determine_color(arr);
//Determine_Ellcolor();
hdc = GetDC(hwnd);
GetClientRect (hwnd, &rect);
pts[0].x = rect.right / 4;
pts[0].y = rect.bottom / 2 - r;
pts[1].x = rect.right / 4 + r / 2;
pts[1].y = rect.bottom / 2 - r * 0.86;
pts[2].x = rect.right / 4 + r * 0.86;
pts[2].y = rect.bottom / 2 - r / 2;
pts[3].x = rect.right / 4 + r;
pts[3].y = rect.bottom / 2;
pts[4].x = rect.right / 4 + r * 0.86;
pts[4].y = rect.bottom / 2 + r / 2;
pts[5].x = rect.right / 4 + r / 2;
pts[5].y = rect.bottom / 2 + r * 0.86;
pts[6].x = rect.right / 4;
pts[6].y = rect.bottom / 2 + r;
pts[7].x = rect.right / 4 - r / 2;
pts[7].y = rect.bottom / 2 + r * 0.86;
pts[8].x = rect.right / 4 - r * 0.86;
pts[8].y = rect.bottom / 2 + r / 2;
pts[9].x = rect.right / 4 - r;
pts[9].y = rect.bottom / 2;
pts[10].x = rect.right / 4 - r * 0.86;
pts[10].y = rect.bottom / 2 - r / 2;
pts[11].x = rect.right / 4 - r / 2;
pts[11].y = rect.bottom / 2 - r * 0.86;
break;
case WM_MOUSEMOVE:
break;
case WM_LBUTTONDOWN:
break;
case WM_KEYDOWN:
break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect (hwnd, &rect);
hPen = CreatePen(PS_SOLID, 1, RGB(211, 211, 211));
SelectObject(hdc, hPen);
hBrush = CreateSolidBrush(RGB(211, 211, 211));
SelectObject(hdc, hBrush);
Ellipse(hdc, rect.right / 4 - r, rect.bottom / 2 - r,
rect.right / 4 + r, rect.bottom / 2 + r);
DeleteObject(hPen);
//hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
//SelectObject(hdc, hPen);
//Polyline(hdc, pts, 12);
sprintf(a, "max = %lf, min = %lf", max, min);
TextOut(hdc, 0, 0, a, strlen(a));
for (i = 0; i < num; i++)
{
sprintf(a, "divide[%d] = %lf", i, divide[i]);
TextOut(hdc, rect.right / 2 - 60, 40 * (i + 1), a, strlen(a));
}
for (i = 0; i < 12; i++)
{
for (j = i + 1; j < 12; j++)
{
if (color[i][j] == 0)
flag = 0;
if (color[i][j] == 1)
flag = 1;
if (color[i][j] == 2)
flag = 2;
if (color[i][j] == 3)
flag = 3;
if (color[i][j] == 4)
flag = 4;
if (color[i][j] == 5)
flag = 5;
if (color[i][j] == 6)
flag = 6;
if (color[i][j] == 7)
flag = 7;
if (color[i][j] == 8)
flag = 8;
hPen = CreatePen(PS_SOLID, 2, RGB(rgb[flag][0], rgb[flag][1], rgb[flag][2]));
SelectObject(hdc, hPen);
MoveToEx(hdc, pts[i].x, pts[i].y, &pt);
LineTo(hdc, pts[j].x, pts[j].y);
DeleteObject(hPen);
}
}
hPen = CreatePen(PS_SOLID, 1, RGB(211, 211, 211));
SelectObject(hdc, hPen);
Ellipse(hdc, rect.right / 4 * 3 - r, rect.bottom / 2 - r,
rect.right / 4 * 3 + r, rect.bottom / 2 + r);
DeleteObject(hBrush);
EndPaint (hwnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage (0);//发出WM_QUIT消息,终止消息循环
return 0;
}
//缺省采用系统消息默认处理函数
return DefWindowProc (hwnd, message, wParam, lParam);
}
void Findmax_min (double* arr)
{
int i, j;
char a[255];
max = arr[1];
min = arr[1];
for (i = 1; i < 144; i++)
{
if (max < arr[i])
max = arr[i];
if (min > arr[i] && arr[i] != 0)
min = arr[i];
}
}
void Determine_divide ()
{
double t;
int i;
t = (max - min) / 9;
for (i = 1; i <= num; i++)
{
divide[i - 1] = min + t * i;
}
}
void Determine_color (double* arr)
{
int i, j;
rgb[0][0] = 255;
rgb[0][1] = 0;
rgb[0][2] = 0;
rgb[1][0] = 255;
rgb[1][1] = 69;
rgb[1][2] = 0;
rgb[2][0] = 255;
rgb[2][1] = 169;
rgb[2][2] = 71;
rgb[3][0] = 250;
rgb[3][1] = 140;
rgb[3][2] = 15;
rgb[4][0] = 250;
rgb[4][1] = 165;
rgb[4][2] = 0;
rgb[5][0] = 255;
rgb[5][1] = 255;
rgb[5][2] = 0;
rgb[6][0] = 173;
rgb[6][1] = 255;
rgb[6][2] = 47;
rgb[7][0] = 0;
rgb[7][1] = 255;
rgb[7][2] = 127;
rgb[8][0] = 0;
rgb[8][1] = 255;
rgb[8][2] = 0;
for (i = 0; i < 12; i++)
{
for (j = 0; j < 12; j++)
{
if (arr[i * 12 + j] < divide[0])
color[i][j] = 0;
else if (arr[i * 12 + j] < divide[1])
color[i][j] = 1;
else if (arr[i * 12 + j] < divide[2])
color[i][j] = 2;
else if (arr[i * 12 + j] < divide[3])
color[i][j] = 3;
else if (arr[i * 12 + j] < divide[4])
color[i][j] = 4;
else if (arr[i * 12 + j] < divide[5])
color[i][j] = 5;
else if (arr[i * 12 + j] < divide[6])
color[i][j] = 6;
else if (arr[i * 12 + j] < divide[7])
color[i][j] = 7;
else
color[i][j] = 8;
}
}
}
void Determine_Ellcolor ()
{
}
void Determine_line(RECT rect)//计算线的k,b
{
int i, j;
for (i = 0; i < 12; i++)
{
for (j = i + 1; j < 12; j++)
{
if (pts_r[i].y - pts_r[j].y == 0)
{
line[cnt].k = 0;
line[cnt].b = pts_r[i].y;
line[cnt++].col = color[i][j];
continue;
}
if (pts_r[i].x - pts_r[j].x == 0)
{
line[cnt].k = MAX;
line[cnt].b = pts_r[i].x;
line[cnt++].col = color[i][j];
continue;
}
line[cnt].k = (pts_r[i].y - pts_r[j].y) * 1.0 / (pts_r[i].x - pts_r[j].x);
line[cnt].b = ((pts_r[i].y - line[cnt].k * pts_r[i].x) + (pts_r[j].y - line[cnt].k * pts_r[j].x)) / 2;
line[cnt++].col = color[i][j];
}
}
}
void Determine_Ellcolor (RECT rect)
{
int i, j, t;
//初始化mpts,以圆的中心为坐标原点
for (i = 0; i < 200; i++)
{
for (j = 0; j < 200; j++)
{
mpts[i][j].p.x = - r + j * 2;
mpts[i][j].p.y = r - i * 2;
mpts[i][j].col = -1;
}
}
for (i = 0; i < 200; i++)//太菜了,就直接三重循环了
{
for (j = 0; j < 200; j++)
{
if (mpts[i][j].p.x * mpts[i][j].p.x + mpts[i][j].p.y + mpts[i][j].p.y > r * r)//不在圆中就跳过
continue;
else
mpts[i][j].col = 0;
int term_num = 1;
for (t = 0; t < cnt; t++)//判断线是不是经过小方格,是的话就加上线的颜色,最好平均,得出方块的颜色
{
if (line[t].k == MAX)
{
if (mpts[i][j].p.x == line[t].b)
{
term_num++;
mpts[i][j].col += line[t].col;
}
}
else if (line[t].k == 0)
{
if (mpts[i][j].p.y == line[t].b)
{
term_num++;
mpts[i][j].col += line[t].col;
}
}
else
{
double t1 = ((mpts[i][j].p.x +1) * line[t].k - mpts[i][j].p.y - 1 + line[t].b) * ((mpts[i][j].p.x +1) * line[t].k - mpts[i][j].p.y - 1 + line[t].b);
double t2 = line[t].k * line[t].k + 1;
if (t1 / t2 < 50)//判断线是不是经过小方格的条件,初步是点到直线的距离的平方是否大于50
{
term_num++;
mpts[i][j].col += line[t].col;
}
}
}
mpts[i][j].col = mpts[i][j].col / term_num;//平均
}
}
for (i = 1; i < 199; i++)//对于可能没有经过的方格,要进行处理。初步是加上周围方格的颜色,然后平均
{
for (j = 1; j < 199; j++)
{
if (mpts[i][j].col == 0)
{
mpts[i][j].col = mpts[i][j + 1].col + mpts[i][j - 1].col + mpts[i + 1][j].col + mpts[i - 1][j].col +
mpts[i + 1][j + 1].col + mpts[i + 1][j - 1].col + mpts[i - 1][j - 1].col + mpts[i - 1][j + 1].col;
mpts[i][j].col = mpts[i][j].col / 8;
}
}
}
//坐标变换,换回窗口坐标系
for (i = 0; i < 200; i++)
{
for (j = 0; j < 200; j++)
{
mpts[i][j].p.x = rect.right / 4 * 3 - r + 2 * j;
mpts[i][j].p.y = rect.bottom / 2 - r + 2 * i;
}
}
}