| Author |
Topic  |
|
|
duci
Forum Admin
 172 Posts |
Posted - 08/07/2005 : 10:13:27
|
Exemplu WinBGI - graficul functiei sin(x)/x - limbajul C
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <winbgim.h>
/* O reprezentare grafica elementara a unei functii de o singura variabila */
double func(double x,double pi) { double z; z = 50.0 * sin ( x * x ) / 1000.0 * exp( - x / 2.0 ); return (z); } double MAX ( double a , double b ) { if ( a >= b ) return a ; else return b ; } double MIN(double a, double b) { if ( a <= b ) return a ; else return b ; }
void plot ( double v[] , double may , double miy) { int i,j; for ( i = 0 ; i < 640 ; i++ ) { j = 349 - 350 / (may-miy) * ( v[i] - miy );// Intreg !!! putpixel ( i , j , 15 );//Functia plotata cu puncte } }
int main()
{ double y,a,b,h,may,miy; double v[640];//vector continand esantionarea functiei int i,j,k; double pi=atan(1.0)*4.0; a = 0.0 ;//Intervalul de plotare [a,b] b = 6.0 ; h= ( b - a ) / 640 ;//Pasul in reprezentarea grafica //Calculam functia pe intervalul [a,b] cu pasul h for ( i = 0 ; i < 640 ; v[ i ] = func( a + i * h , pi ),i++); //********scalare may=v[0]; miy=v[0]; for ( i = 1 ; i < 640 ; i++ ) { may = MAX ( may , v[i] ); miy = MIN ( miy , v[i] ); } i = DETECT ;//Grafic BGI j = DETECT ; initgraph (&i,&j,"d:\\tc"); cleardevice (); plot ( v,may,miy);//Graficul propriu-zis closegraph ; system ( "pause" ); return 0 ; }
|
Prof.Dr. D. Ciurchea
|
|
|
duci
Forum Admin

172 Posts |
Posted - 08/07/2005 : 10:17:41
|
Exemplu WinBGI- graficul functiei sin(x)/x - limbajul Pascal Program grafic_elementar; (* O reprezentare grafica elementara a unei functii de o singura variabila *)
uses graph ; const npct = 640 ; type vec = array [1..npct] of real ;
var v : vec ; {vector continand esantionarea functiei} i,j :integer ; x,a,b,h,may,miy :real ;
function func (var x : real ) : real ; Begin func := 50.0 * sin ( x ) * 1000.0 ; End ; {**************************************************************}
function max(a,b:real):real; { Val. maxima intre 2 nr. reale } Begin if a>b then max:=a else max:=b; End ;
{**************************************************************}
function min(a,b:real):real; { Val. minima intre 2 nr. reale } Begin if a>b then min:=b else min:=a; End ;
BEGIN a := 0.0 ;{Intervalul de plotare [a,b]} b := 6.0 ; h := ( b - a ) / npct ;{Pasul in reprezentarea grafica} {Calculam functia pe intervalul [a,b] cu pasul h} for i := 1 to npct do begin x := a + i * h ; v[ i ] := func( x ); end ; {********scalare} may := v[ 1 ]; miy := v[ 1 ]; for i := 1 to npct do begin may := max ( may , v[ i ] ); miy := min ( miy , v[ i ] ); end ; {Grafic BGI } i := DETECT ; j := DETECT ; initgraph ( i , j , 'd:\tp\bgi'); cleardevice (); outtextxy ( 1 , 10 , 'Text in fereastra grafica'); {Graficul propriu-zis} for i := 1 to 640 do begin j := 349 - round (350.0 / ( may - miy ) * ( v[ i ] - miy ) );{ Intreg !!!} putpixel ( i , j , 15 ) ; {Functia plotata cu puncte} end ; writeln ('Gata. Apasa orice tasta in fereastra consola.'); readln ; closegraph ; END. |
Prof.Dr. D. Ciurchea
|
 |
|
| |
Topic  |
|
|
|