hi,

I m making a graph with all (x,y) printed on graph line..like from +x=1,2,3....so on and same with -x,+y,-y.

below I tried u can try this too but I want clean graph with nos. on it.
can someone solve it?

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   
   int gdriver = DETECT, gmode, errorcode;
   char ch;
   
   initgraph(&gdriver, &gmode, "c:/tc/bgi");

   
   errorcode = graphresult();

   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }

   
  // line(0, 0, getmaxx(), getmaxy());
   line(getmaxx()/2,0,getmaxx()/2,getmaxy());
   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
  // ch = '0';
	outtextxy(getmaxx()/2,getmaxy()/2,"(0,0)");

     getch();
   closegraph();
   return 0;
}

You're using a slightly modified graph function as to what is posted from atleast one college on-line.

Figure out what you want your scale to be, draw your cross ticks and place your coordinates. You're using max limits each call. You should have something like:

int   cx,cy;


main()
{  // calculate center points
cx = getmaxx() >> 1;
cy = getmaxy() >> 1;
:
:
}

Everytime you go to graph/plot/etc. Use the center displacement {cx,cy}

void PlotLine( int x1, int y1,    int x2, y2 )
{    // Assuming in API (+Y) is down, we flip the y so (+Y) is up!
  line( cx + x1, cy - y1,    cx + x2, cy - y2 );
}

   PlotLine( 5,10,     8,10 );

Also, Do a scale and so do your graph in floating point but scale it to fit your screen and plot in integer in the PlotLine function.

If your display is 1024 x 960 then cx,cy = {512,480}
If you want your graph to say be25 points wide, by 25 points high,
then use the shorter (the height) for your scale. You'll need a little space around the graph so 480.0 but we want a little extra room around the graph, say 20 pixels? so 480-20 = 460. Thus 460 / 25.0 = 18.4 pixels per grid tick. So our scale factor is 18.4.

int   cx,cy;
float fScale;

main()
{
cx = getmaxx() >> 1;
cy = getmaxy() >> 1;
    // 20 = spacing outside graph area, 25 is resolution of graph.
fScale = (float)(getmaxy() - 20 ) / (float)25;

And so we need a floating-point graph line function.

void FPlotLine( float x1, float y1,    float x2, float y2 )
{    // Assuming in API (+Y) is down, we flip the y so (+Y) is up!
  line( cx + (int)(fScale * x1), cy - (int)(fScale * y1),
          cx + (int)(fScale * x2), cy - (int)(fScale * y2)  );
}

So now you can use floating-point coordinates and display them on your integer screen!

I've got to run, but that should get you rolling!

This article has been dead for over six months. Start a new discussion instead.