Turbo C Graphics - line function
line function is used to draw a line in the graphics screen. It uses the color and style set by the setcolor and setlinestyle functions. getlinesettings is the function used to get the current line style settings and returns the struct linesettingstype.
line function will take 4 arguments - begx, begy and endx, endx.
Here is the sample code with using line and setlinestyle.
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
|