如何用c语言画函数图像

2024-11-27 18:30:20
推荐回答(4个)
回答1:

用GDI绘图吧,比较简单。绘图的思想是让x以固定的值在区间内持续增长,比如x=0.1,0.2,0.3.....,以计算出的y值来确定y坐标。用线连接所有的点就行了。MoveTo(),LineTo()函数你用得着,具体情况请自行查看MSDN。

回答2:

hehe
首先这个程序只能输出这种
不像正弦
也不象余弦的曲线。
因为你的程序的算法是每行有2个星星
其次,15+45=60,约等于2*pi
就是说,前面的15是pi/2,后面的45是pi/2*3,
输出的是1个周期的。sin(pi/2)=1。。这回懂了么?

回答3:

/*-----------------------------------------
正弦曲线
-----------------------------------------*/

#include
#include

#define NUM 1000
#define TWOPI (2 * 3.14159)

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "SineWave" ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;

注意:用VC++6.0或其他Windows平台的编译器编译
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = 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 = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;

RegisterClassEx (&wndclass) ;

hwnd = CreateWindow (szAppName, "正弦曲线",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient ;
HDC hdc ;
int i ;
PAINTSTRUCT ps ;
POINT pt [NUM] ;

switch (iMsg)
{
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

MoveToEx (hdc, 0, cyClient / 2, NULL) ;
LineTo (hdc, cxClient, cyClient / 2) ;

for (i = 0 ; i < NUM ; i++)
{
pt[i].x = i * cxClient / NUM ;
pt[i].y = (int) (cyClient / 2 *
(1 - sin (TWOPI * i / NUM))) ;
}

Polyline (hdc, pt, NUM) ;

return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}

return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

回答4:

运用画点函数进行画