C Program To Draw A Line In Dev C++

Posted : admin On 30.12.2020

Paint program in c:- This program can draw different shapes using mouse such as line, circle, pixel and many other shapes. You can also change the color, clear the screen. Code of paint program in C is given below:-

/* To understand the code see output below the code, it will help you in understanding the code. */

Qt provides cross-platform drawing libraries that will work on any of Linux, Windows or Mac. EDIT: Direct2D is another C option for Windows. It's fully hardware accelerated too which is cool. As for drawing on a fullscreen window, it's no different than drawing in a regular window. Sep 17, 2014 C graphics program to plot a pixel (point) at any coordinate position,Drawing a line in C and drawing a colorful circle using basic function of graphics.h Graphics programming made simple. Oct 24, 2018 Now let's learn to draw RECTANGLE in C/C Graphics. To draw a rectangle in C graphics, first, you have to initialize the graphics and also include the graphics.h file in your program. Have a look at the Rectangle drawing function prototype below and then we will look forward to how it is used.

C programming code

Dev-C is a free IDE for Windows that uses either MinGW or TDM-GCC as underlying compiler. Originally released by Bloodshed Software, but abandoned in 2006, it has recently been forked by Orwell, including a choice of more recent compilers. Arrays Basic Problems c language projects C Primer Plus C Projects C Structures C Tutorials C/C Shapes C Basic Problems C Books Solution C Functions C Language C Loops and Decisions C Program Examples C Programs c projects C Puzzles C Shapes Code C Structures C Tips and Tricks C Tutorials Control Structures CPP Enum. Jan 10, 2018 13.4 – Drawing basic shapes – Turbo C by subbu on January 10, 2018 Here in this session we are going to draw some basic shapes like horizontal line, vertical line, rectangle and splitting rectangle etc.

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

union REGS i, o;
int leftcolor[15];

int get_key()
{
union REGS i,o;

i.h.ah=0;
int86(22,&i,&o);

return( o.h.ah);
}

void draw_color_panel()
{
int left, top, c, color;

left =100;
top =436;

color = getcolor();
setcolor(GREEN);
rectangle(4,431,635,457);
setcolor(RED);
settextstyle(TRIPLEX_FONT,0,2);
outtextxy(10,431,'Colors : ');

for( c =1; c <=15; c++)
{
setfillstyle(SOLID_FILL, c);
bar(left, top, left+16, top+16);
leftcolor[c-1]= left;
left +=26;
}

setcolor(color);
}

void draw_shape_panel()
{
int left, top, c, color;

left =529;
top =45;

color = getcolor();
setcolor(GREEN);
rectangle(525,40,633,255);

for( c =1; c <=7; c++)
{
rectangle(left, top, left+100, top+25);
top +=30;
}
setcolor(RED);
outtextxy(530,45,'Bar');
outtextxy(530,75,'Line');
outtextxy(530,105,'Pixel');
outtextxy(530,135,'Ellipse');
outtextxy(530,165,'Freehand');
outtextxy(530,195,'Rectangle');
outtextxy(530,225,'Clear');
setcolor(color);
}

void change_color(int x,int y)
{
int c;

for( c =0; c <=13; c++)
{
if( x > leftcolor[c]&& x < leftcolor[c+1]&& y >437&& y <453)
setcolor(c+1);
if( x > leftcolor[14]&& x <505&& y >437&& y <453)
setcolor(WHITE);
}
}

char change_shape(int x,int y)
{
if( x >529&& x <625&& y >45&& y <70)
return'b';
elseif( x >529&& x <625&& y >75&& y <100)
return'l';
elseif( x >529&& x <625&& y >105&& y <130)
return'p';
elseif( x >529&& x <625&& y >135&& y <160)
return'e';
elseif( x >529&& x <625&& y >165&& y <190)
return'f';
elseif( x >529&& x <625&& y >195&& y <220)
return'r';
elseif( x >529&& x <625&& y >225&& y <250)
return'c';
}

void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

Draw A Line In Dev C++

void hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}

void restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx= x1;
i.x.dx= x2;
int86(0x33,&i,&o);Auto tune free download apk.

i.x.ax=8;
i.x.cx= y1;
i.x.dx= y2;
int86(0x33,&i,&o);
}

void getmousepos(int*button,int*x,int*y)
{
i.x.ax=3;
int86(0x33,&i,&o);

*button = o.x.bx;
*x = o.x.cx;
*y = o.x.dx;
}

main()
{
int gd = DETECT,gm;

int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;
char ch ='f';// default free-hand drawing

initgraph(&gd,&gm,'C:TCBGI');

maxx = getmaxx();
maxy = getmaxy();/auto-tune-online-free-for-singing.html.

Draw A Line Game

setcolor(BLUE);
rectangle(0,0,maxx,maxy);

setcolor(WHITE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(maxx/2-180,maxy-28,'<a href='http://www.programmingsimplified.com'>www.programmingsimplified.com'</a>);

draw_color_panel();
draw_shape_panel();

setviewport(1,1,maxx-1,maxy-1,1);

restrictmouseptr(1,1,maxx-1,maxy-1);
showmouseptr();
rectangle(2,2,518,427);
setviewport(1,1,519,428,1);

while(1)
{
if(kbhit())
{
key = get_key();

if( key 1)
{
closegraph();
exit(0);
}
}

getmousepos(&button,&x,&y);

if( button 1)
{
if( x >4&& x <635&& y >431&& y <457)
change_color( x, y );
elseif( x >529&& x <625&& y >40&& y <250)
ch = change_shape( x, y );

temp1 = x ;
temp2 = y ;

if( ch 'f')
{
hidemouseptr();
while( button 1)
{
line(temp1,temp2,x,y);
temp1 = x;
temp2 = y;
getmousepos(&button,&x,&y);
}
showmouseptr();
}

while( button 1)
getmousepos(&button,&x,&y);

/* to avoid interference of mouse while drawing */
hidemouseptr();

C Program To Draw A Line In Dev C Download

if( ch 'p')
putpixel(x,y,getcolor());

Draw

elseif( ch 'b')
{
setfillstyle(SOLID_FILL,getcolor());
bar(temp1,temp2,x,y);
}
elseif( ch 'l')
line(temp1,temp2,x,y);
elseif( ch 'e')
ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));
elseif( ch 'r')
rectangle(temp1,temp2,x,y);
elseif( ch 'c')
{
ch ='f';// setting to freehand drawing
clearviewport();
color = getcolor();
setcolor(WHITE);
rectangle(2,2,518,427);
setcolor(color);
}

showmouseptr();
}
}
}

Graphics programming in C used to drawing various geometrical shapes(rectangle, circle eclipse etc), use of mathematical function in drawing curves, coloring an object with different colors and patterns and simple animation programs like jumping ball and moving cars.

1. First graphics program (Draw a line)

2. Explanation of Code :

The first step in any graphics program is to include graphics.h header file. The graphics.h header file provides access to a simple graphics library that makes it possible to draw lines, rectangles, ovals, arcs, polygons, images, and strings on a graphical window.

The second step is initialize the graphics drivers on the computer using initgraph method of graphics.h library.

It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc, to their default values. Below is the description of input parameters of initgraph function.

  • graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It tells the compiler that what graphics driver to use or to automatically detect the drive. In all our programs we will use DETECT macro of graphics.h library that instruct compiler for auto detection of graphics driver.

  • graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If *gdriver is set to DETECT, then initgraph sets *gmode to the highest resolution available for the detected driver.

  • driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are located. If directory path is not provided, then it will search for driver files in current working directory directory. In all our sample graphics programs, you have to change path of BGI directory accordingly where you Turbo C++ compiler is installed.

We have declared variables so that we can keep track of starting and ending point.

No, We need to pass just 4 parameters to the line function.

line Function Draws Line From (x1,y1) to (x2,y2) .

Parameter Explanation

  • x1 - X Co-ordinate of First Point
  • y1 - Y Co-ordinate of First Point
  • x2 - X Co-ordinate of Second Point
  • y2 - Y Co-ordinate of Second Point

At the end of our graphics program, we have to unloads the graphics drivers and sets the screen back to text mode by calling closegraph function.

3. Colors in C Graphics Programming

There are 16 colors declared in graphics.h header file. We use colors to set the current drawing color, change the color of background, change the color of text, to color a closed shape etc (Foreground and Background Color). To specify a color, we can either use color constants like setcolor(RED), or their corresponding integer codes like setcolor(4). Below is the color code in increasing order.

ConstantValueBackground?Foreground?
BLACK0YesYes
BLUE1YesYes
GREEN2YesYes
CYAN3YesYes
RED4YesYes
MAGENTA5YesYes
BROWN6YesYes
LIGHTGRAY7YesYes
DARKGRAY8NOYes
LIGHTBLUE9NOYes
LIGHTGREEN10NOYes
LIGHTCYAN11NOYes
LIGHTRED12NOYes
LIGHTMAGENTA13NOYes
YELLOW14NOYes
WHITE15NOYes
BLINK128NO*

***** To display blinking characters in text mode, add BLINK to the foreground color. (Defined in conio.h)

4. Graphics example using color

5. Examples

Example Statement for Graphics in C Language
1. Drawing Line in Graphics Mode
2. Make Static Countdown
3. Draw Moving a Car
4. Press Me Button Game
5. Draw Smiling Face Animation
6. Make Traffic Light Simulation