Вот, рисует y=x^2 Извини, что паскаль
Код | uses Graph,crt; label 3; var x,y,xd,yd:real; a,b:integer; m:integer; begin
a:=Vga; b:= VgaHi; initgraph(a, b, 'd:\pascal7\bin'); setbkcolor(15); setcolor(7); Line(10,240,630,240); moveto(620,235); lineto(630,240); lineto(620,245); Line(320,10,320,460); moveto(325,20); lineto(320,10); lineto(315,20); OutTextXY(620,250,'X'); OutTextXY(330,15,'Y'); x:=-100;{esli oshibka, to sdelat' -100000} setcolor(2); repeat moveto(round(xd),round(yd)); x:=x+00000000.1; y:=x*x; {------FUNCTION-------} xd:=x*25+320; {25-masshtab} yd:=-y*25+240; {25-masshtab} if (yd>0) and (yd<490) then begin putpixel(round(xd), round(yd),4); lineto(round(xd),round(yd)) end; until x>100; {esli oshibka, to sdelat' 100000} readln; end.
| Добавлено @ 23:04 Попробуй еще вот это. Лишнее удали. Но принцип построения все же лучше смотри в первом примере
Код | #include< graphics.h> #include< conio.h> #include< math.h> #include< stdio.h> #include< stdlib.h> const k=30,a=320,b=340; float xmin=-6., xmax=10., ymin=-6., ymax=6.,ky, kx,xc1,xc2,x0,y0; void Axes()//рисует оси координат { int xst, yst, x, y, x0, y0; char s[10]; setcolor(RED); kx=640/(xmax-xmin); ky=480/(ymax-ymin); line(a,0,a,479); line(0,b,639,b); xst=(int) xmin; if(xst// float F1(float x) // { //if(x!=2) // return 4/(2-x); // } void ShowPoint(float x,float y,int c)//строет графики по точкам
{ int x0,y0; x0=a+kx*x; y0=b-ky*y; if(x0> =0 && x0< 640 && y0> =0 && y0< 480 ) // x20> =0 && x20< 640 && y20> =0 && y20< 480) putpixel(x0,y0,c);
} float F1(float x) { //if(x!=2) return 4/(2-x); } void PlotF1() { float xmin,xmax,x,y; xmin=-a/kx; xmax=(640-a)/ky;
for(x=xmin;x< =xmax;x+=1/kx) { if(fabs(2-x)> 0.0001) { y=F1(x); ShowPoint(x,y,5); }
} }
float F2(float x) { // if(x> 4||x< -4) return sqrt(1-x*x/16)*2+3; }
float F3(float x) { // if(x> 4||x< -4) return -sqrt(1-x*x/16)*2+3; }
void PlotF2() { float xmin,xmax,x,y; xmin=-a/kx; xmax=(640-a)/ky; for(x=xmin;x< =xmax;x+=1/kx) { if(fabs(4*(1-x*x/16)> 0.0001)) { y=F2(x); ShowPoint(x,y,15); }
} } void PlotF3() { float xmin,xmax,x,y; xmin=-a/kx; xmax=(640-a)/ky;
for(x=xmin;x< =xmax;x+=1/kx) { if(fabs(4*(1-x*x/16)> 0.0001)) { y=F3(x); ShowPoint(x,y,15); } } } float SolveGood(float a,float b,float e) { float c,ga, gc; while(fabs(b-a)> e) { c=(a+b)/2.; ga=F1(a)-F2(a); gc=F1(c)-F2(c); if(ga*gc< 0)b=c; else a=c; } return(a+b)/2.; } void Cross() { char s[40]; xc2=SolveGood(0,3,0.0001); sprintf(s,"x=%f,y=%f",xc2,F2(xc2)); outtextxy(20,20,s); }
float SolveGood2(float a,float b,float e) { float c,ga, gc; while(fabs(b-a)> e) { c=(a+b)/2.; ga=F1(a)-F3(a); gc=F1(c)-F3(c); if(ga*gc< 0)b=c; else a=c; } return(a+b)/2.; }
void Cross2() { char s[40]; xc1=SolveGood2(-3,0,0.0001); sprintf(s,"x=%f,y=%f",xc1,F3(xc1)); outtextxy(20,50,s); }
float up(float x) {float x2=1.18; if(x< x2)return F1(x); else return F2(x); }
float down(float x) {float x3=4.; if(x< =x3)return F3(x); // else return F4(x); else return 0; } void Hatch() { float x,yup,ydown,h,x1=-1.5,x3=4; int N=10,x0,y0,y10,y20; h=(x3-x1)/(N+1); for(x=x1;x< =x3;x+=h) { yup=up(x); ydown=down(x); x0=a+kx*x; y10=b-ky*yup; y20=b-ky*ydown; line(x0,y10,x0,y20); } }
void Area() { float x1=-1.494,x2=1.185,x3=4.,x,h=0.01,S1=0,S2=0,S=0; for(x=x1;x< x2;x+=h) S1+=h*(F1(x+h/2)-F3(x+h/2)); for(x=x2;x< x3;x+=h) S2+=h*(F2(x+h/2)-F3(x+h/2)); S=S1+S2; printf("Area of hatched part of input figure is about %f \n",S); }
float Randf(float min,float max) { return min+(max-min)*rand()/RAND_MAX; }
int Inside(float x,float y) { if(y< =up(x) && y> =down(x)) return 1; else return 0; } void Area2() { float xmin=-1.494,ymin=1.144,xmax=4,ymax=4.91; long N=2000000L,i,M=0; //int i; float S,x,y; for(i=1;i< =N;i++) { x=Randf(xmin,xmax); y=Randf(ymin,ymax); if(Inside(x,y)) M++; } S=(xmax-xmin)*(ymax-ymin)*M/N; printf("Area-2 is about %f",S); }
void main() { int d=DETECT,m; initgraph(& d,& m,"C:\\BORLANDC\\BGI"); Axes(); PlotF1(); PlotF2(); PlotF3(); Cross(); Cross2(); Hatch(); Area(); Area2(); getch(); closegraph(); }
| |