CODE vẽ đồ thị sin chuyển đổi quan sát

00:14 , 1 Comments

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>
using namespace std;
int  xv1,yv1,xv2,yv2;
float xw1,xw2,yw1,yw2; // cac toa cua cua cua so va khung nhin
float tlx,tly; // ty le

void cuaso(float x1,float y1,float x2,float y2)
{
xw1=x1; xw2=x2; yw1=y1;yw2=y2;  // khoi tao cua so
}
void khungnhin(float x1,float y1,float x2,float y2)
{
  xv1=x1;yv1=y1; xv2=x2; yv2=y2;          // khung nhin
  tlx=(float)(xv2-xv1)/(xw2-xw1);                  // tinh ty le x
  tly=(float)(yv2-yv1)/(yw2-yw1);                  // tinh ty le y
}

void chuyenden(float x,float y)
{
  int xm,ym;
  xm=(int)(tlx*(x-xw1)+xv1+0.5);
  ym=(int)(tly*(yw2-y)+yv1+0.5);
  moveto(xm,ym);
}

void veden(float x,float y)
{
  int xm,ym;
  xm=(int)(tlx*(x-xw1)+xv1+0.5);
  ym=(int)(tly*(yw2-y)+yv1+0.5);
  lineto(xm,ym);
}

void vehetruc()
{
setcolor(4);
float x0=0,y0=0,dy=0.01;
chuyenden(x0,y0);
outtextxy(10,10,"Do thi hinh sin coppyright 2015 by NguyenTan");
for(int i=0;i<1000;i++){
x0=x0+dy;
veden(x0,y0);
}
x0=0;y0=0;
chuyenden(x0,y0);
for(int i=0;i<500;i++){
y0=y0+dy;
veden(x0,y0);
}
x0=0,y0=0;
chuyenden(x0,y0);
for(int i=0;i<500;i++){
x0=x0-dy;
veden(x0,y0);
}
x0=0;y0=0;
chuyenden(x0,y0);
for(int i=0;i<500;i++){
y0=y0-dy;
veden(x0,y0);
}

}

void vedothi()
{
setcolor(2);
float dx=0.01;
float x=xw1;
float y=sin(x);
chuyenden(x,y);
while(x<=xw2){
x=x+dx;
y=sin(x);
veden(x,y);
}
}
int main()
{
int gr=0,gm;
cuaso(-M_PI,-1.5,3*M_PI,1.5);
khungnhin(150,150,300,250);
initgraph(&gr,&gm,"");
vehetruc();
vedothi();
getch();
closegraph();
return 0;
}

1 nhận xét: