[请教]如何 编写程序在屏幕上画一条正弦曲线?

时间:2008-05-09 19:07:14   来源:论坛整理  作者:  编辑:chinaitzhe
如题,编写程序在屏幕上画一条正弦曲线?

还请各位前辈指教~ :)

(请给出思路和源代码)
网友回复:y=sin(x);

在坐标(x, y)画点。
网友回复:#include "conio.h"
#include "math.h"
#include "graphics.h"

int main()
{
int x=0;
int y=239;
int i,j,h;
float m;
int driver,mode;
driver=VGA;
mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(9);
setcolor(BLUE);
line(317,0,317,479);
line(0,239,639,239);
for(j=0;j <640;j )
{
m=sin(j/50.5); /**************/
h=y-(int)(m*100);
putpixel(x j,h,RED);
delay(1000);
}
getch();
closegraph();
return 0;
}


网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include<stdio.h> 

#include<math.h> 

main(){ 

double y; 

int x,m,i; 

printf("y=sin(x) [0<x<2*pi]\n"); 

for(y=1;y>=-1;y-=0.1){ 

if(y>=0){ 

m=asin(y)*10; 

for(x=1;x<m;x  )printf(" "); 

printf("*",m); 

for(;x<31-m;x  )printf(" "); 

printf("*\n");} 

else{ 

m=-1*asin(y)*10; 

for(i=0;i<32;i  )printf(" "); 

for(x=1;x<m;x  )printf(" "); 

printf("*",m); 

for(;x<31-m;x  )printf(" "); 

printf("*\n",m); 

} 

} 

}




网友回复:以下代码来源于《Windows程序设计》(第五版)
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include   <windows.h>

#include   <math.h>  



#define   NUM   1000  

#define   TWOPI   6.283185  



TCHAR   szAppName[]   =   TEXT("SineWave");  

HWND   hwnd;    

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

BOOL   InitApplication(HINSTANCE);  

void   InitInstance(HINSTANCE,   int);  



int   WINAPI   WinMain(HINSTANCE   hInstance,   HINSTANCE   hPrevInstance,   LPSTR   lpCmdLine,   int   nShowCmd   )  

{  

        MSG   msg;           

        if   (!hPrevInstance)      

                if   (!InitApplication(hInstance))  

                        return   FALSE;            

        InitInstance(hInstance,   nShowCmd);                     

        while   (GetMessage(&msg,   NULL,   0,   0))                   

        {                                                                    

                TranslateMessage(&msg);                                            

                DispatchMessage(&msg);                                                       

        }                                                                                              

        return   msg.wParam;                                                                                 

}                                                                                                                  



BOOL   InitApplication(HINSTANCE   hinstance)                                                                        

{

        WNDCLASS   ws;                                                                                                    

        ws.style   =   CS_VREDRAW   |   CS_HREDRAW;

        ws.lpfnWndProc   =   WndProc;                                                                                       

        ws.cbWndExtra   =   0;                                                                                                    

        ws.cbClsExtra   =   0;

        ws.hInstance   =   hinstance;

        ws.hIcon   =   LoadIcon(NULL,   IDI_APPLICATION);

        ws.hCursor   =   LoadCursor(NULL,   IDC_ARROW);

        ws.hbrBackground   =   (HBRUSH)GetStockObject(WHITE_BRUSH);

        ws.lpszMenuName   =   NULL;

        ws.lpszClassName   =   szAppName;

        return   (RegisterClass(&ws));

}                     



void   InitInstance(HINSTANCE   hinstance,   int   nCmdShow)

{

        hwnd   =   CreateWindow(szAppName,   TEXT("Sine   Wave   Using   Polyline"),   WS_OVERLAPPEDWINDOW,   CW_USEDEFAULT,   CW_US

EDEFAULT,   CW_USEDEFAULT,   CW_USEDEFAULT,   NULL,   NULL,   hinstance,   NULL);

        ShowWindow(hwnd,   nCmdShow);   

        UpdateWindow(hwnd);

}



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

}




网友回复:for(x=0;x <X;x )
{
setpixel(x,Y0-Y*sin(6.28*x/X0));
}
X= 在屏幕上画多长的曲线
X0= 一个周期画多少个点
Y= 曲线的幅度,y轴高度
Y0= y轴位置
网友回复:是在Console界面下吗
1,你可以用matlab(假如不考虑语言)
2,另外也可使用MFC中的画图函数
3,可以使用Intel的开源openCV,可以在Console下画出来

要的话可以给你画画

网友回复:windows程序设计(第5版)

Sample5-2
网友回复:感谢 3楼 laolaoliu2002 前辈的指点~ :)
关键字:请教,编写程序,屏幕,一条,正弦曲线,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面