/************************************
名 称:Sin函数曲线
作 者:freewind
版 本:v1.0
时 间:2006-08
Email:freewind22@163.com
*************************************/
#include <stdio.h>
#include <math.h>
#include <graphics.h>
#define PI 3.1415926
int CenterX=320,CenterY=240;
int LineColor=10;
void Init_Graph(){
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver, &gmode, "G://turboc2//");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s/n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /*terminate with an error code */
}
}
void DrawXY(int l,int t,int r,int b)
{
int x,y;
CenterX-=(r-l)/2;
CenterY-=(b-t)/2;
x=CenterX;
y=CenterY;
setcolor(15);
/* left ,down */
line(x,y,x-l,y);
line(x,y,x,y+b);
/* right */
line(x,y,x+r,y);
line(x+r,y,x+r-3,y-2);
line(x+r,y,x+r-3,y+2);
/* up */
line(x,y,x,y-t);
line(x,y-t,x+2,y-t+3);
line(x,y-t,x-2,y-t+3);
/* draw o, x , y */
settextjustify(1,1);
outtextxy(x-6,y+6,"o");
outtextxy(x+r-6,y+8,"x");
outtextxy(x-8,y-t+6,"y");
}
void Sin()
{
int n=-360,size=3;
int x,y,prev_y=100;
char buf[5];
setcolor(LineColor);
settextjustify(1,1);
while(n<=360)
{
x=CenterX+n/size;
y=CenterY-sin(n*PI/180)*90/size/(PI/2);
putpixel(x,y,LineColor);
if(prev_y!=100)
line(x-1,prev_y,x,y);
prev_y=y;
n+=size;
}
}
void main()
{
Init_Graph();
DrawXY(150,110,150,110);
Sin();
getch();
}