c语言画数学函数

sungj0917 2009-10-17 05:48:05
#include <windows.h>

#include <math.h>

#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 TCHAR szAppName[] = TEXT ("SineWave") ;

HWND hwnd ;

MSG msg ;

WNDCLASS 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 ;
if (!RegisterClass (&wndclass))

{

MessageBox ( NULL, TEXT ("Program requires Windows NT!"),

szAppName, MB_ICONERROR) ;

return 0 ;

}
hwnd = CreateWindow ( szAppName, TEXT ("Sine Wave Using Polyline"),

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 message, WPARAM wParam, LPARAM lParam)

{

static int cxClient, cyClient ;

HDC hdc ;

int i ;

PAINTSTRUCT ps ;

POINT apt [NUM] ;

switch (message)

{

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++)

{

apt[i].x = i * cxClient / NUM ;

apt[i].y = (int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) ;

}

Polyline (hdc, apt, NUM) ;

return 0 ;



case WM_DESTROY:

PostQuitMessage (0) ;

return 0 ;
}

return DefWindowProc (hwnd, message, wParam, lParam)

我在windows程序设计读到以上程序但是不太明白
apt[i].x = i * cxClient / NUM ;
apt[i].y = (int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) ;
就是画出y=sin(x)的函数呢???
...全文
194 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongpy 2009-10-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sungj0917 的回复:]
引用 9 楼 dongpy 的回复:
引用 2 楼 sungj0917 的回复:
我知道,可是我不明白,比如(int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) 是做什么的
不如我想画y=x*x 的曲线怎么画呢?
SetMapMode (hdc, MM_TWIPS);
          SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
  TextOut(hdc,0,100,"a",1);
          for (i = 0 ; i < 5000 ; i++)
          {
              apt[i].x = i ;
            apt[i].y = (int) (i*i)
          }
         
          Polyline (hdc, apt, 5000) ;
          return 0 ;

画出的曲线很奇怪??

应该这样画:
apt[i].x = i * cxClient / 5000;
apt[i].y = (int) (apt[i].x*apt[i].x);
//这时曲线坐标系与画布坐标系一致。
//5000是分辨率,就是x方向的等分数。


能否能从WM_PAINT开始给初完整的代码呢??包括坐标系的定义!!

[/Quote]
for (i = 0 ; i < 5000 ; i++)
{
apt[i].x = i * cxClient / 5000;
apt[i].y = (int)(cyClient-apt[i].x*apt[i].x);
}
//这样曲线坐标系(逻辑坐标系)是以客户区左下角为原点,x轴向右,y轴向上。
//画布坐标系(物理坐标系)是固定的,是以客户区左上角为原点,x轴向右,y轴向下。
//如果逻辑坐标系与物理坐标系不一致,则参考如上所述的转换方法。
sungj0917 2009-10-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dongpy 的回复:]
引用 2 楼 sungj0917 的回复:
我知道,可是我不明白,比如(int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) 是做什么的
不如我想画y=x*x 的曲线怎么画呢?
SetMapMode (hdc, MM_TWIPS);
          SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
  TextOut(hdc,0,100,"a",1);
          for (i = 0 ; i < 5000 ; i++)
          {
              apt[i].x = i ;
            apt[i].y = (int) (i*i)
          }
         
          Polyline (hdc, apt, 5000) ;
          return 0 ;

画出的曲线很奇怪??

应该这样画:
apt[i].x = i * cxClient / 5000;
apt[i].y = (int) (apt[i].x*apt[i].x);
//这时曲线坐标系与画布坐标系一致。
//5000是分辨率,就是x方向的等分数。
[/Quote]

能否能从WM_PAINT开始给初完整的代码呢??包括坐标系的定义!!
dongpy 2009-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sungj0917 的回复:]
我知道,可是我不明白,比如(int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) 是做什么的
不如我想画y=x*x 的曲线怎么画呢?
SetMapMode (hdc, MM_TWIPS);
          SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
  TextOut(hdc,0,100,"a",1);
          for (i = 0 ; i < 5000 ; i++)
          {
              apt[i].x = i ;
            apt[i].y = (int) (i*i)
          }
         
          Polyline (hdc, apt, 5000) ;
          return 0 ;

画出的曲线很奇怪??
[/Quote]
应该这样画:
apt[i].x = i * cxClient / 5000;
apt[i].y = (int) (apt[i].x*apt[i].x);
//这时曲线坐标系与画布坐标系一致。
//5000是分辨率,就是x方向的等分数。
dongpy 2009-10-17
  • 打赏
  • 举报
回复
apt[i].x = i * cxClient / NUM ;
//水平方向0~cxClient被NUM等分
apt[i].y = (int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) ;
//TWOPI * i / NUM角度从0~360度,"cyClient / 2"和"1 - sin"这些是由于客户区画布的坐标系和sin(x)的坐标系不一致,而加上的转换操作。

Polyline (hdc, apt, NUM) ;
//将NUM个有效点,用线段连接起来,组成一条分辨率为NUM的sin(x)曲线。
dongpy 2009-10-17
  • 打赏
  • 举报
回复
for (i = 0 ; i < NUM ; i++)

{

apt[i].x = i * cxClient / NUM ;

apt[i].y = (int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) ;

}

Polyline (hdc, apt, NUM) ;
===========================================================================
这是在客户区画sin(x),x在0~360度的图形,分辨率为NUM,即图形的有效点数为NUM。
Z782282738 2009-10-17
  • 打赏
  • 举报
回复
只差6分C/C++3就有3个小三角了。


结分给我6分。

至于答案我赞同楼上的
arong1234 2009-10-17
  • 打赏
  • 举报
回复
(cyClient / 2 * (1 - sin (TWOPI * i / NUM)))
其实就是a+a*sin(bx)么,这下懂了么?
arong1234 2009-10-17
  • 打赏
  • 举报
回复
你的那个程序步进单位是1,太大了这是为什么人家需要除以cxClient的原因
rivulettornado 2009-10-17
  • 打赏
  • 举报
回复
应该是
y = cyClient/2*(1-sin(2*pi*x/cxClient))
不是y= sin(x)
sungj0917 2009-10-17
  • 打赏
  • 举报
回复
我知道,可是我不明白,比如(int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) 是做什么的
不如我想画y=x*x 的曲线怎么画呢?
SetMapMode (hdc, MM_TWIPS);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
TextOut(hdc,0,100,"a",1);
for (i = 0 ; i < 5000 ; i++)
{
apt[i].x = i ;
apt[i].y = (int) (i*i)
}

Polyline (hdc, apt, 5000) ;
return 0 ;

画出的曲线很奇怪??
arong1234 2009-10-17
  • 打赏
  • 举报
回复
这不像是sin(x),而是y=cyClient/2(1-sin(x))
注意后面的Polyline

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧