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

CyanLeaf 2008-05-05 12:01:10
如题,编写程序在屏幕上画一条正弦曲线?

还请各位前辈指教~ :)

(请给出思路和源代码)
...全文
500 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
沙漠虫行 2012-08-05
  • 打赏
  • 举报
回复
呵呵,都有方法
  • 打赏
  • 举报
回复
尊重网上道德。
CyanLeaf 2008-05-09
  • 打赏
  • 举报
回复
感谢 3楼 laolaoliu2002 前辈的指点~ :)
simo110 2008-05-05
  • 打赏
  • 举报
回复
是在Console界面下吗
1,你可以用matlab(如果不考虑语言)
2,另外也可使用MFC中的画图函数
3,可以使用Intel的开源openCV,可以在Console下画出来

要的话可以给你画画
laolaoliu2002 2008-05-05
  • 打赏
  • 举报
回复
for(x=0;x<X;x++)
{
setpixel(x,Y0-Y*sin(6.28*x/X0));
}
X= 在屏幕上画多长的曲线
X0= 一个周期画多少个点
Y= 曲线的幅度,y轴高度
Y0= y轴位置
独孤过儿 2008-05-05
  • 打赏
  • 举报
回复
以下代码来源于《Windows程序设计》(第五版)

#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);
}
laolaoliu2002 2008-05-05
  • 打赏
  • 举报
回复

#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);
}
}
}
laolaoliu2002 2008-05-05
  • 打赏
  • 举报
回复
#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;
}

BluntBlade 2008-05-05
  • 打赏
  • 举报
回复
y=sin(x);

在坐标(x, y)画点。
guanqb 2008-05-05
  • 打赏
  • 举报
回复
windows程序设计(第5版)

Sample5-2

69,373

社区成员

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

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