
Graficar funciones trigonométricas
Pascal/Turbo Pascal
Publicado el 31 de Octubre del 2002 por Alejandro Cespedes
27.580 visualizaciones desde el 31 de Octubre del 2002
Programa para graficar funciones trigonométricas. Realizado en Turbo Pascal 7.0
{**************************************
* GRAFICAS TRIGONOMETRICAS *
* AUTOR: ALEJANDRO CESPEDES D. *
* UNIVERSIDAD DE PLAYA ANCHA *
**************************************}
program graficas_funciones;
uses crt,graph;
var
amplitud : integer; {valor que establece cada cuantos pixeles
se considera una unidad}
{========================================================================}
procedure iniciar_graficos;
var
codigo_error,
modograf,
drvgraf : integer;
begin
drvgraf:=detect;
initgraph(drvgraf,modograf,'c:\tp\bgi');
codigo_error := graphresult;
if codigo_error <> 0 then
begin
writeln('Error de graficos: ',grapherrormsg(codigo_error));
readkey;
halt(1);
end;
end;
{==========================================================================}
procedure numeros_en_pantalla(uni:integer);
{dibuja las lineas que hacen de numeros, recibe el en x la amplitud}
var
inf,
sup : integer; {variables que establecen cuantos "numeros"
se dibujaran}
begin
inf := -15;
sup := 15;
while inf < sup do
begin
setcolor(yellow);
line((getmaxx div 2)+inf*uni,(getmaxy div 2)-2,(getmaxx div 2)+inf*uni,(getmaxy div 2)+2);
line((getmaxx div 2)-2,(getmaxy div 2)+inf*uni,(getmaxx div 2)+2,(getmaxy div 2)+inf*uni);
inc(inf);
end;
end;
{=========================================================================}
procedure pantalla_grafica(x:integer);
{dibuja un eje x,y en pantalla, traspasa x (amplitud) al procedimiento
numeros_en_pantalla}
begin
cleardevice;
setcolor(blue);
setbkcolor(lightblue);
{lineas en la pantalla}
line(getmaxx div 2,0, getmaxx div 2, getmaxy);
line(0, getmaxy div 2, getmaxx, getmaxy div 2);
setcolor(red);
outtextxy((getmaxx div 2)-20,10,'Y');
outtextxy(getmaxx-20,(getmaxy div 2)+5,'X');
numeros_en_pantalla(x);
end;
{========================================================================}
procedure menu;
begin
cleardevice;
setbkcolor(blue);
setcolor(red);
rectangle(100,50,520,400);
setcolor(white);
floodfill(110,60,red);
settextstyle(1,0,2);
SETCOLOR(BLACK);
outTextxy(180,60,'SELECCIONE UNA GRAFICA');
setcolor(red);
line(180,85,430,85);
settextstyle(0,0,1);
setcolor(red);
outtextxy(120,150,'1.- ');
setcolor(blue);
outtextxy(150,150,'Sen(x)');
setcolor(red);
outtextxy(120,180,'2.- ');
setcolor(blue);
outtextxy(150,180,'Cos(x)');
setcolor(red);
outtextxy(120,210,'3.- ');
setcolor(blue);
outtextxy(150,210,'Tg(x)');
setcolor(red);
outtextxy(120,240,'4.- ');
setcolor(blue);
outtextxy(150,240,'ln(x)');
setcolor(red);
outtextxy(230,150,'5.- ');
setcolor(blue);
outtextxy(260,150,'raiz(x)');
setcolor(red);
outtextxy(230,180,'6.- ');
setcolor(blue);
outtextxy(260,180,'X2');
setcolor(red);
outtextxy(230,210,'7.- ');
setcolor(blue);
outtextxy(260,210,'csc(x)');
setcolor(red);
outtextxy(230,240,'8.- ');
setcolor(blue);
outtextxy(260,240,'ctg(x)');
setcolor(red);
outtextxy(330,150,'9.- ');
setcolor(blue);
outtextxy(360,150,'sec(x)');
setcolor(blink);
outtextxy(200,350,'PRESIONE <esc> PARA SALIR....');
end;
{=====================================================================}
procedure salida;
begin
cleardevice;
setbkcolor(blue);
setcolor(white);
settextstyle(3,1,4);
outtextxy(50,50,'GRAFICAS DE FUNCIONES');
DELAY(500);
setcolor(yellow);
settextstyle(4,0,2);
outtextxy(220,50,'C');
delay(50);
outtextxy(240,50,'R');
delay(50);
outtextxy(260,50,'E');
delay(50);
outtextxy(280,50,'A');
delay(50);
outtextxy(300,50,'D');
delay(50);
outtextxy(320,50,'O');
delay(50);
outtextxy(380,50,'P');
delay(50);
outtextxy(400,50,'O');
delay(50);
outtextxy(420,50,'R');
delay(600);
setcolor(red);
rectangle(160,100,500,400);
setcolor(white);
floodfill(170,140,red);
setcolor(red);
settextstyle(11,0,1);
delay(50);
outtextxy(180,130,'Alejandro D. C‚spedes D.');
delay(200);
outtextxy(180,180,'Alumno Ingenier¡a en Inform tica.');
delay(200);
outtextxy(180,240,'Universidad de Playa Ancha.');
delay(200);
outtextxy(180,290,'Taller de Programaci¢n.');
delay(200);
setcolor(green);
outtextxy(180,380,'2002 - Todos los derechos reservados');
readkey;
halt(1);
end;
{=======================================================================}
procedure entrada;
begin
cleardevice;
setbkcolor(blue);
setcolor(white);
settextstyle(3,1,4);
outtextxy(50,50,'GRAFICAS DE FUNCIONES');
DELAY(500);
setcolor(GREEN);
settextstyle(2,0,10);
outtextxy(220,50,'I');
delay(50);
outtextxy(240,50,'N');
delay(50);
outtextxy(260,50,'S');
delay(50);
outtextxy(280,50,'T');
delay(50);
outtextxy(300,50,'R');
delay(50);
outtextxy(320,50,'U');
delay(50);
outtextxy(340,50,'C');
delay(50);
outtextxy(360,50,'C');
delay(50);
outtextxy(380,50,'I');
delay(50);
outtextxy(400,50,'O');
delay(50);
outtextxy(420,50,'N');
delay(50);
outtextxy(440,50,'E');
delay(50);
outtextxy(460,50,'S');
delay(50);
delay(600);
setcolor(red);
rectangle(120,100,600,300);
setcolor(white);
floodfill(130,140,red);
setcolor(red);
settextstyle(2,0,5);
delay(50);
outtextxy(150,130,'* utilice las tecla numericas para seleccionar una funcion');
delay(200);
outtextxy(150,150,'* Ingrese los limites de acuerdo a la funcion');
delay(200);
outtextxy(150,170,'* Utilice + y - para hacer zoom.');
delay(200);
outtextxy(150,190,'* presione <esc> para volver al menu.');
delay(200);
outtextxy(150,210,'* Antes de presionar <esc> presione cualquier tecla');
outtextxy(150,220,' hasta ver las opciones zoom.');
delay(200);
setcolor(green);
outtextxy(180,380,'2002 - Todos los derechos reservados');
readkey;
end;
{========================================================================}
procedure seno(inf,sup:real;uni:integer);
var
x,{variable para calcular el valor de la funcion}
y {variable auxiliar para achurar la grafica} : real;
begin
y:=inf;
while inf<=sup do
begin
x:=sin(inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
end;
delay(600);
setcolor(red);
while y<sup do
begin
x:=sin(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
end;
setcolor(yellow);
outtextxy(50,50,'SENO DE X');
end;
{======================================================================}
procedure cosecante(inf,sup:real;uni:integer);
var
x,z,{variable para calcular el valor de la funcion}
y {variable auxiliar para achurar la grafica} : real;
begin
y:=inf;
repeat
x:=1/sin(inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)+trunc(x*uni),white);
inf:=inf+0.01;
delay(5);
until inf>-0.01;
z:=0.01;
repeat
x:=1/sin(z);
putpixel(trunc(getmaxx div 2+(z*uni)),(getmaxy div 2)+trunc(x*uni),white);
z:=z+0.01;
delay(5);
until z>=sup;
delay(600);
setcolor(red);
repeat
x:=1/sin(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)+round(x*uni));
y:=y+0.01;
delay(5);
until y>-0.01;
y:=0.01;
repeat
x:=1/sin(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)+round(x*uni));
y:=y+0.01;
delay(5);
until y>sup;
setcolor(yellow);
outtextxy(50,50,'COSECANTE DE X');
end;
{==========================================================================}
procedure coseno(inf,sup:real;uni:integer);
var
x,{variable para calcular el valor de la funcion}
y {variable auxiliar para achurar la grafica} : real;
begin
y:=inf;
while inf<=sup do
begin
x:=cos(inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
end;
delay(600);
setcolor(red);
while y<sup do
begin
x:=COS(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
end;
setcolor(yellow);
outtextxy(50,50,'COSENO DE X');
end;
{========================================================================}
procedure secante(inf,sup:real;uni:integer);
var
x,{variable para calcular el valor de la funcion}
y {variable auxiliar para achurar la grafica} : real;
begin
y:=inf;
while inf<=sup do
begin
x:=1/cos(inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
end;
delay(600);
setcolor(red);
while y<sup do
begin
x:=1/COS(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
end;
setcolor(yellow);
outtextxy(50,50,'SECANTE DE X');
end;
{==========================================================================}
procedure tangente(inf,sup:real;uni:integer);
var
x,{variable para calcular la funcion}
y:{variable para achurar la figura}real;
begin
y:=inf;
while inf<=sup do
begin
x:=(sin(inf))/cos(inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
end;
delay(600);
setcolor(red);
while y<sup do
begin
x:=(sin(y))/cos(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
end;
setcolor(yellow);
outtextxy(50,50,'TANGENTE DE X');
end;
{=======================================================================}
procedure cotangente(inf,sup:real;uni:integer);
var
x,z,{variable para calcular la funcion}
y:{variable para achurar la figura}real;
begin
y:=inf;
repeat
x:=1/(sin(inf)/cos(inf));
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
until inf>-0.01;
z:=0.01;
repeat
x:=1/(sin(z)/cos(z));
putpixel(trunc(getmaxx div 2+(z*uni)),(getmaxy div 2)-round(x*uni),white);
z:=z+0.01;
delay(5);
until z>sup;
delay(600);
setcolor(red);
repeat
x:=1/(sin(y)/cos(y));
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
until y>-0.01;
y:=0.01;
repeat
x:=1/(sin(y)/cos(y));
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
until y>sup;
setcolor(yellow);
outtextxy(50,50,'COTANGENTE DE X');
end;
{===========================================================================}
procedure cuadrado(inf,sup:real;uni:integer);
var
x,y:real;
begin
y:=inf;
repeat
x:=(inf*inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-trunc(x*uni),white);
inf:=inf+0.01;
delay(5);
until inf>sup;
delay(600);
setcolor(red);
while y<sup do
begin
x:=y*y;
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
end;
setcolor(yellow);
outtextxy(50,50,'CUADRADO DE X');
end;
{==========================================================================}
procedure raiz(inf,sup:real;uni:integer);
var
x,y:real;
begin
y:=inf;
while inf<=sup do
begin
x:=sqrt((inf));
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
end;
delay(600);
setcolor(red);
while y<sup do
begin
x:=sqrt(y);
line(trunc(getmaxx div 2+(y*uni)),getmaxy div 2,trunc(getmaxx div 2+(y*uni)),(getmaxy div 2)-round(x*uni));
y:=y+0.01;
delay(5);
end;
setcolor(yellow);
outtextxy(50,50,'RAIZ DE X');
end;
{===========================================================================}
procedure logaritmo_n(sup:real;uni:integer);
var
x,{variable para calcular el valor de la funcion}
inf,{variable a la que se le asignara el valor inicial para comnezar a graficar la funcion}
z {variable auxiliar para achurar la grafica}:real;
begin
inf:=0.01;
Z:=INF;
repeat
x:=LN(inf);
putpixel(trunc(getmaxx div 2+(inf*uni)),(getmaxy div 2)-round(x*uni),white);
inf:=inf+0.01;
delay(5);
until inf>sup;
delay(600);
setcolor(red);
repeat
x:=ln(z);
line(trunc(getmaxx div 2+(z*uni)),getmaxy div 2,trunc(getmaxx div 2+(z*uni)),
(getmaxy div 2)-round(x*uni));
z:=z+0.01;
delay(5);
until z>sup;
readkey;
setcolor(yellow);
outtextxy(50,50,'LOGARITMO NATURAL DE X');
end;
{==========================================================================}
procedure graficar_funciones;
var
zoom,
a,
b : integer;
begin
cleardevice;
menu;
window(10,30,20,31);
zoom:=0;
case readkey of
'1':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
seno(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
seno(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
seno(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'2':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
coseno(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
coseno(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
coseno(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'3':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
tangente(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
tangente(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
tangente(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'4':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite superior: ');
window(27,2,30,2);
readln(b);
until b>0;
pantalla_grafica(amplitud);
logaritmo_n(b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
logaritmo_n(b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
logaritmo_n(b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'5':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros positivos');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until (a>=0) and (b>a);
pantalla_grafica(amplitud);
raiz(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
raiz(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
raiz(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'6':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros ');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
cuadrado(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
cuadrado(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
cuadrado(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'7':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
cosecante(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
cosecante(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
cosecante(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'8':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
cotangente(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
cotangente(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
cotangente(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
'9':begin
cleardevice;
setcolor(white);
repeat
outtextxy(1,1,'solo numeros enteros');
outtextxy(1,20,'ingrese limite inferior: ');
window(27,2,30,2);
readln(a);
outtextxy(1,30,'Ingrese limite superior: ');
window(27,3,30,3);
readln(b);
until a<b;
pantalla_grafica(amplitud);
secante(a,b,amplitud);
repeat
outtextxy(50,60,'+ zoom in - zoom out');
case readkey of
'+':begin
zoom:=zoom+10;
pantalla_grafica(amplitud+zoom);
secante(a,b,amplitud+zoom);
end;
'-':begin
pantalla_grafica((amplitud+zoom)-10);
secante(a,b,(amplitud+zoom)-10);
zoom:=zoom-10;
end;
#27:graficar_funciones;
end;
until readkey=#27;
end;
#27:salida;
end;
end;
{===================== programa principal ===================}
begin
amplitud:=30;
iniciar_graficos;
ENTRADA;
repeat
graficar_funciones;
until readkey=#27;
salida;
end.
Comentarios sobre la versión: Versión 1 (7)
Maldito Gilipollo......
gracias