Software & Finance





Turbo C Graphics - getx function





getx and gety functions are used to get the current cursor position on the graphics screen. The current positions values can be set by moveto, moverel, linerel and moverel functions.

 

You can notice the outcome from the getx and gety functions are 100, 100 which is set by moveto function in the given example.

 

      moveto(100, 100);

      sprintf(msg, "linestyle: %d thickness: %d getx : %d, gety: %d",       lstype.linestyle, lstype.thickness, getx(), gety());

 

 

Back to Turbo C Graphics Index



Source Code


 

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#include <dos.h>

#include <stdlib.h>

 

int InitGraphics()

{

      int grd, grm;

      int gresult;

      // Detect the graphics driver and mode

      detectgraph(&grd,&grm);

      // initialize the graphics mode with initgraph

      initgraph(&grd, &grm, "");

 

      gresult = graphresult();

      if(gresult != grOk)

      {

            printf(grapherrormsg(gresult));

            getch();

            return -1;

      }

 

      // set the background color

      setbkcolor(RED);

      // set the foreground color

      setcolor(WHITE);

      // draw a white color border with rectangle

      rectangle(0,0,getmaxx(),getmaxy());

      return 1;

}

 

void main()

{

      int i, x, y, w;

      int left,top,right,bottom;

      int margin, width, height;

      int cx, cy;

      char msg[512];

      unsigned char patternbuffer[8];

      unsigned char upattern_solid[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

      unsigned char upattern[8] = {  0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };

      struct fillsettingstype fstype;

      struct linesettingstype lstype;

 

      if(InitGraphics() == -1)

            return;

 

      margin = 100; // 100 pixel margin

      left = margin;

      top = margin;

      bottom = getmaxy() - margin;

      right = getmaxx() - margin;

 

      // draw a bar with user defined solid fill on blue color

      // use rectangle to have a white color border

      setfillstyle(SOLID_FILL, WHITE);

      setfillpattern(upattern_solid, BLUE);

      getfillpattern(patternbuffer);

      getfillsettings(&fstype);

      sprintf(msg, "pattern id: %d color: %d buffer: %d", fstype.pattern, fstype.color, patternbuffer[0]);

      bar(left, top, right, bottom);

      rectangle(left, top, right, bottom);

 

      //another inner rectangle drawn with the use of moveto and lineto functions

      setcolor(YELLOW);

      moveto(left + 5, top + 5);

      lineto(right - 5, top + 5);

      lineto(right - 5, bottom - 5);

      lineto(left + 5, bottom - 5);

      lineto(left + 5, top + 5);

 

      //another inner square drawn with the use of moverel and linerel functions

      setcolor(WHITE);

      moveto(left, top);

      moverel(20, 20);

      linerel(50, 0);

      linerel(0, 50);

      linerel(-50, 0);

      linerel(0, -50);

 

      // draw two lines by crossing above square

      // SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE

      setlinestyle(DASHED_LINE, 1, 1);

      line(left + 20, top + 20, left + 70, top + 70);

      setlinestyle(DOTTED_LINE, 1, 3);

      line(left + 70, top + 20, left + 20, top + 70);

      getlinesettings(&lstype);

 

      // find the text width and text height

      width = textwidth(msg);

      height = textheight(msg);

      // calculate the right and bottom with margin and getmaxx and getmaxy function

 

      // change the text foreground color to yellow

      setcolor(YELLOW);

      cx = (right + left - width) / 2;

      cy = (bottom + top - height) / 2;

      settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);

      outtextxy(cx,cy,msg);

 

      setcolor(WHITE);

      moveto(100, 100);

      sprintf(msg, "linestyle: %d thickness: %d getx : %d, gety: %d", lstype.linestyle, lstype.thickness, getx(), gety());

      width = textwidth(msg);

      cx = (right + left - width) / 2;

      cy += textheight(msg) + 10;;

 

      // use moveto and outtext function instead of outtextxy

      moveto(cx, cy);

      outtext(msg);

 

      getch();

      closegraph();

}

 

Output