A. 怎样用C++编程绘制y=sinx 的一段曲线
与你拥有的软件有关。绘曲线的关键是计算出曲线的x,y坐标,然后两点连一线。
下面是用MS VC++ API 写的绘曲线程序,画时序曲线,可缩放,画座标刻度..,现删去了一些功能(未删净),供参考。
编译时若有警告信息,可以或略。
设x0=0;dx=0.01;1000点。
#include <afxwin.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Winuser.h>
#pragma comment (lib, "LIBCMT.LIB")
#define DEBUG 0
HWND hWndMain;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
void OnDisplayMessage(char *str);
void set_n_scale_y(int *ndiv, float ymin, float ymax, float *yu, float *yd, float *dy);
void set_n_scale_x(int *ndiv, float t0, float te, float *td, float *tu,float *draw_dt );
static float *yt,rx,ry,y_min,y_max,y_ran,y_shift;
static float draw_yu,draw_yd,draw_dy,draw_min,draw_max;
static float t0=0.0,dt=1.0,draw_tu,draw_td,draw_dt;
int flag_t0=0;
float bt_scale_1=1.0;
int draw_n=4,draw_nt=6;
char pname[40],namein[50],str[120],para2[10],para3[10],para4[10];;
char one_line[80];
int len,NN;
LPTSTR argv;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
MSG Msg;
int i;
//get cmd line
argv = GetCommandLine();
len = strlen(argv);
sprintf(one_line,"%s",argv);
i = sscanf(one_line,"%s %s %s %s %s",&pname[0],&namein[0],¶2[0],¶3[0],¶4[0]);
///===========
t0 = 0;
dt = 0.02;
draw_nt=6;
NN = 1000;
yt = (float *) malloc(NN * sizeof(float));
for (i=0;i<NN;i++) yt[i] = sin(t0+dt*i);
y_min=y_max=yt[0];
for (i=0;i<NN;i++){
if (yt[i] < y_min) y_min=yt[i];
if (yt[i] > y_max) y_max=yt[i];
};
y_ran = y_max - y_min;
ry = 150.0 / y_ran;
for (i=0;i<NN;i++) yt[i]= yt[i] - y_min;
y_shift = 180 ;
draw_max = y_max;
draw_min = y_min;
(void) set_n_scale_y(&draw_n, draw_min, draw_max, &draw_yu, &draw_yd, &draw_dy);
if (DEBUG == 1){
sprintf(str,"yd=%f yu=%f dy=%f y_max=%f y_min=%f n=%d", draw_yd,draw_yu,draw_dy,y_max,y_min,draw_n);
OnDisplayMessage(str);
};
(void) set_n_scale_x(&draw_nt, t0, t0+ dt*(NN), &draw_td, &draw_tu,&draw_dt);
if (DEBUG == 1){
sprintf(str,"td=%f tu=%f dt=%f n=%d", draw_td,draw_tu,draw_dt,draw_nt);
OnDisplayMessage(str);
};
rx = 900.0 / (draw_tu - draw_td);
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static long nXChar,nYChar;
HDC hdc;
short x;
TEXTMETRIC tm;
PAINTSTRUCT ps;
COLORREF color;
HFONT font;
HPEN hP1; // pen
// CPoint aP,mousePos;
int i;
switch(message)
{
case WM_LBUTTONDOWN:
bt_scale_1 = bt_scale_1 + 0.05;
ry=ry*bt_scale_1;
y_shift = y_shift * bt_scale_1;
ShowWindow(hwnd, SW_HIDE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return 0;
case WM_RBUTTONDOWN:
ry=ry/bt_scale_1;
y_shift = y_shift / bt_scale_1;
ShowWindow(hwnd, SW_HIDE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return 0;
case WM_CREATE:
ScrollWindow(hwnd, 0, -800, NULL, NULL);
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
nXChar=tm.tmAveCharWidth;
nYChar=tm.tmHeight;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps); // begin paint
SetWindowExtEx(hdc,1200,800,NULL);
SetViewportExtEx(hdc,1200,800*bt_scale_1,NULL);
SetViewportOrgEx(hdc,100,150,NULL);
// draw grids
color=RGB(0,128,128);
hP1=CreatePen(PS_SOLID,0,color);
SelectObject(hdc,hP1);
MoveToEx( hdc,0, y_shift-(draw_yu-y_min)*ry, NULL );
LineTo(hdc,900,y_shift-(draw_yu-y_min)*ry);
LineTo(hdc,900,y_shift-(draw_yd-y_min)*ry);
LineTo(hdc,0,y_shift-(draw_yd-y_min)*ry);
LineTo(hdc,0,y_shift-(draw_yu-y_min)*ry);
for (i=0;i<=draw_n;i++)
{
MoveToEx( hdc, 0, y_shift-(draw_yd -y_min + i* draw_dy)*ry, NULL );
LineTo( hdc, 900, y_shift-(draw_yd -y_min + i * draw_dy)*ry );
}
for (i=1;i<draw_nt;i++) {
MoveToEx( hdc, 900 * i /draw_nt, y_shift-(draw_yd -y_min)*ry, NULL );
LineTo( hdc, 900 * i /draw_nt, y_shift-(draw_yu -y_min)*ry );
}
// draw curve
color=RGB(255,0,0);
hP1=CreatePen(PS_SOLID,0,color);
SelectObject(hdc,hP1);
MoveToEx( hdc, (t0-draw_td)* rx, y_shift-yt[0]*ry, NULL );
for (i=1;i<NN ;i++){
LineTo(hdc,(t0+i*dt-draw_td)*rx, y_shift-yt[i]*ry);
}
font=CreateFont(
24,10,0,0, FW_NORMAL,0,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,NULL,"myfont"
);
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
nYChar=tm.tmHeight;
color=RGB(0,0,0);
for (i=0;i<=draw_n;i++)
{
sprintf(one_line,"%.2f",draw_yd +i * draw_dy);
TextOut(hdc,-60,y_shift-(draw_yd-y_min + i* draw_dy)*ry-5,one_line,strlen(one_line));
}
for (i=0;i<=draw_nt;i++)
{
sprintf(one_line,"%.1f",draw_td +i * draw_dt);
TextOut(hdc,900 * i /draw_nt-30,y_shift-(draw_yd -y_min)*ry+20, one_line,strlen(one_line));
}
sprintf(one_line,"%s N=%d",namein,NN);
TextOut(hdc,20,y_shift-(draw_yu-y_min )*ry-40,one_line,strlen(one_line));
EndPaint(hwnd,&ps); // end paint
return 0L;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
// wndclass.hIcon=LoadIcon(NULL,"END");
wndclass.hIcon=LoadIcon(hInstance,"uni03.ico");
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName="Windows Fill";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
return(RegisterClass(&wndclass));
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
char str[100];
sprintf(str,"%s %s",pname,namein);
hWnd=CreateWindow(
"Windows Fill",
str,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
if(!hWnd)
return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void OnDisplayMessage(char *str)
{
MessageBox(NULL,str,"msg",MB_OK | MB_ICONINFORMATION);
}
void set_n_scale_y(int *ndiv, float ymin, float ymax, float *yu, float *yd, float *dy)
{
int n ;
float yuu,ydd;
float yr;
if (*ndiv <= 0) {n = 2;} else n = *ndiv;
yr = ymax - ymin;
if ( yr >= 0.0001 && yr < 0.001) {
yuu = (float) ((int) (ymax * 10000.0)) / 10000.0f + 0.0001f;
ydd = (float) ((int) (ymin * 10000.0)) / 10000.0f - 0.0001f;
}
else if (yr >= 0.001 && yr < 0.01) {
yuu = (float) ((int) (ymax * 1000.0)) / 1000.0f + 0.001f;
ydd = (float) ((int) (ymin * 1000.0)) / 1000.0f - 0.001f;
}
else if ( yr >= 0.01 && yr < 0.1) {
yuu = (float) ((int) (ymax * 100.0)) / 100.0f + 0.01f;
ydd = (float) ((int) (ymin * 100.0)) / 100.0f - 0.01f;
}
else if ( yr >= 0.1 && yr < 1.0) {
yuu = (float) ((int) (ymax * 10.0)) / 10.0f + 0.1f;
ydd = (float) ((int) (ymin * 10.0)) / 10.0f - 0.1f;
}
else if ( yr >= 1.0 && yr < 10.0) {
yuu = (float) ((int) (ymax)) + 1.0f;
ydd = (float) ((int) (ymin)) - 1.0f;
}
else if ( yr >= 10.0 && yr < 100.0) {
yuu = (float) ((int) (ymax * 0.1)) / 0.1f + 10.0f;
ydd = (float) ((int) (ymin * 0.1)) / 0.1f - 10.0f;
}
else if ( yr >= 100.0 && yr < 1000.0) {
yuu = (float) ((int) (ymax * 0.01)) / 0.01f + 100.0f;
ydd = (float) ((int) (ymin * 0.01)) / 0.01f - 100.0f;
}
else if ( yr >= 1000.0 && yr < 10000.0) {
yuu = (float) ((int) (ymax * 0.001)) / 0.001f + 1000.0f;
ydd = (float) ((int) (ymin * 0.001)) / 0.001f - 1000.0f;
}
else if ( yr >= 10000.0 && yr < 100000.0) {
yuu = (float) ((int) (ymax * 0.0001)) / 0.0001f + 10000.0f;
ydd = (float) ((int) (ymin * 0.0001)) / 0.0001f - 10000.0f;
} else {
yuu = ymax; ydd = ymin;
};
if (yuu == ydd) {
yuu = ydd * 1.1f;
ydd = ydd * 0.9f;
};
*yu = yuu; *yd = ydd;
*dy = (yuu - ydd) / n;
if (*dy > 1.0) *dy = (float) ((int) *dy );
*ndiv = (yuu - ydd)/ (*dy) ;
}
void set_n_scale_x(int *ndiv, float t0, float te, float *td, float *tu,float *draw_dt )
{
int i;
int n ;
double tr,dt,d;
if (*ndiv <= 0) {n = 6;} else n = *ndiv;
tr = (double) (te - t0);
dt = tr / (double) n;
if (dt >= 1.0 ){d = (double) ((int) (dt + 0.9));}
else { d = dt;};
i = (int)( (double) t0 / d);
if (t0 < 0) i=i-1;
// if ( (float)(d * i) < t0) i = i + 1;
*td = (float) (d * i);
*ndiv = (int) ((te - *td) / d);
if ( *ndiv * d < (te - *td) ) *ndiv= *ndiv+1;
*tu = *td + *ndiv * d;
*draw_dt = (float) d;
}