Turbo C Graphics - moverel function
moveto and moverel functions are used to move the current position in the graphics screen. They do not draw anything on the screen but sets the current position. These values can be used by the functions lineto, linerel and moverel function. Apart from these functions, outtext also uses the current position coordinates to display the text.
moverel function takes the end coordinates (offsetx, offsety) in relative term. The begin position (begx, begy) is last position set by either moveto or lineto function. The end position is calculated by internally by,
endx = begx + offsetx;
endy = endx + offsety;
Here is the sample code with using moverel and linerel for drawing a rectangle. Note that all the coordinates specfied by moverel and linerel functions are relative values. To use the absolue coordinates, refer to the functions moveto and lineto. The following code draws 50 pixels square starting from left + 20, to + 20.
//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);
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
|