← Foro de C / C++

Ayuda en graficar una funcion Simple con putpixel

Hilo en el foro de C / C++ · 15 de Abril del 2005 · 4 respuestas
Edgar15 de Abril del 2005
Pregunta
hey!! estoy desarrollando un programa que grafique una funcion de segundo grado por ejemplo y(t)=at^2+bt+2.... esta debe ser graficada con ejes de coordenadas en los cuales tienen q tener valores en los ejes x......... hasta ahora tengo ya listo un programa es la grafica del sen x aki les dejo para ver si me dicen como colocar valores en las axis....

#include <graphics.h>
#include <math.h>
#include <conio.h>

void circulo(int x,int y,int radio)
{
int i,j,k;

for(i=1;i<=360;i++)
{
j=(radio*sin(i))+x;
k=(radio*cos(i))+y;

putpixel(j,k,WHITE);

}
}
/*************************/
main()
{
int gdriver=DETECT,gmode;
int maxx,maxy;

initgraph(&gdriver,&gmode,"");

maxx=getmaxx();
maxy=getmaxy();

circulo(maxx/2,maxy/2,150);
getch();
closegraph();
}

por favor necesito ayuda con la funcion de segundo grado por favor..........

Respuestas 4

Noel Solw15 de Abril del 2005
// program para.cpp
// dibujar una parabola en la pantalla.
// 14/4/2005 - written in borland c++ - ver 3.1

#include <graphics.h>
#include <conio.h>
#include <math.h>

const float MAX_ROW = 479; // graphic's screen dimention
const float MAX_COL = 639;
const float Xo = MAX_COL/2; // initial graphic's position
const float Yo = MAX_ROW/2; // " " "
const float A = 0.15; // parabola constantes
const float B = 5; // " "
const float C = -150; // " "
const float kx = 5;
const float ky = 1;

int Round(long double num) // rounds real number to integer
{
return int(num+0.5);
} // ROUND

int XtoCol(long double x)
{ // transform x value
return Round(Xo+x*kx); // to display column value
} // X TO COL

int YtoRow(long double y)
{ // transform y value
return Round(Yo-y*ky); // to display row value
} // Y TO ROW

long f(int x) // parabola's function
{
return(x*x*A + x*B + C);
} // f

void DibujarEjes()
{
setcolor(LIGHTCYAN);
line(XtoCol(-Xo),YtoRow(0),XtoCol(Xo),YtoRow(0));
line(XtoCol(0),YtoRow(-Yo),XtoCol(0),YtoRow(Yo));
} // DIBUJAR EJES

void DibujarParabola()
{
setcolor(LIGHTRED);
putpixel(XtoCol(-Xo),YtoRow(f(-Xo)),LIGHTRED);
for(int x = -Xo+1;x < Xo;x++)
lineto(XtoCol(x),YtoRow(f(x)));
} // DIBUJAR PARABOLA

void main()
{
int driver, mode;
detectgraph(&driver,&mode);
initgraph(&driver,&mode,"d:\borlandc\bgi");
DibujarEjes();
DibujarParabola();
getch();
closegraph();
} // MAIN
Noel Solw15 de Abril del 2005
Lo mismo que el programa anterior, usando putpixel, nada mas.
Como veras el grafico no sale tan bien.

// program para.cpp
// dibujar una parabola en la pantalla.
// 14/4/2005 - written in borland c++ - ver 3.1

#include <graphics.h>
#include <conio.h>
#include <math.h>

const float MAX_ROW = 479; // graphic's screen dimention
const float MAX_COL = 639;
const float Xo = MAX_COL/2; // initial graphic's position
const float Yo = MAX_ROW/2; // " " "
const float A = 0.15; // parabola constantes
const float B = 5; // " "
const float C = -150; // " "
const float kx = 5;
const float ky = 1;

int Round(long double num) // rounds real number to integer
{
return int(num+0.5);
} // ROUND

int XtoCol(long double x)
{ // transform x value
return Round(Xo+x*kx); // to display column value
} // X TO COL

int YtoRow(long double y)
{ // transform y value
return Round(Yo-y*ky); // to display row value
} // Y TO ROW

long f(int x) // parabola's function
{
return(x*x*A + x*B + C);
} // f

void DibujarEjes()
{
setcolor(LIGHTCYAN);
line(XtoCol(-Xo),YtoRow(0),XtoCol(Xo),YtoRow(0));
line(XtoCol(0),YtoRow(-Yo),XtoCol(0),YtoRow(Yo));
} // DIBUJAR EJES

void DibujarParabola()
{
// program para.cpp
// dibujar una parabola en la pantalla.
// 14/4/2005 - written in borland c++ - ver 3.1

#include <graphics.h>
#include <conio.h>
#include <math.h>

const float MAX_ROW = 479; // graphic's screen dimention
const float MAX_COL = 639;
const float Xo = MAX_COL/2; // initial graphic's position
const float Yo = MAX_ROW/2; // " " "
const float A = 0.15; // parabola constantes
const float B = 5; // " "
const float C = -150; // " "
const float kx = 5;
const float ky = 1;

int Round(long double num) // rounds real number to integer
{
return int(num+0.5);
} // ROUND

int XtoCol(long double x)
{ // transform x value
return Round(Xo+x*kx); // to display column value
} // X TO COL

int YtoRow(long double y)
{ // transform y value
return Round(Yo-y*ky); // to display row value
} // Y TO ROW

long f(int x) // parabola's function
{
return(x*x*A + x*B + C);
} // f

void DibujarEjes()
{
setcolor(LIGHTCYAN);
line(XtoCol(-Xo),YtoRow(0),XtoCol(Xo),YtoRow(0));
line(XtoCol(0),YtoRow(-Yo),XtoCol(0),YtoRow(Yo));
} // DIBUJAR EJES

void DibujarParabola()
{
for(int x = -Xo;x < Xo;x++)
putpixel(XtoCol(-Xo),YtoRow(f(-Xo)),LIGHTRED);
} // DIBUJAR PARABOLA

void main()
{
int driver, mode;
detectgraph(&driver,&mode);
initgraph(&driver,&mode,"d:\borlandc\bgi");
DibujarEjes();
DibujarParabola();
getch();
closegraph();
} // MAIN


void main()
{
int driver, mode;
detectgraph(&driver,&mode);
initgraph(&driver,&mode,"d:\borlandc\bgi");
DibujarEjes();
DibujarParabola();
getch();
closegraph();
} // MAIN
ferchoner fernandez26 de Septiembre del 2011
Hola Disculpa la molestia, Necesitaba saber por que al copilar me marca un error que dice "body has alredy been defined for function "main()" tu respuesta seria de mucha ayuda Gracias. Este error me lo marca en la linea 102:2 de tu codigo
Ricardo29 de Junio del 2010
Hola, siento molestar. Me gustaría saber donde puedo encontrar las librerías de este programa graphics.h, conio.h y math.h. Me creía que venian de base pero parece que no.
¿Buscas más hilos como este? Mira el foro de C / C++ o usa el buscador.