0% found this document useful (0 votes)
170 views

Program To Draw A Triangle and A Line

The document contains C++ code for several graphics programs that draw different shapes using functions from the graphics.h library. The programs draw triangles, lines, squares, rectangles, circles, arcs, sectors, ellipses and implement functions like putpixel, getpixel, setcolor, floodfill, setlinestyle and setfillstyle. The last program implements Bresenham's line drawing algorithm to draw a line between two points.

Uploaded by

Rahul Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

Program To Draw A Triangle and A Line

The document contains C++ code for several graphics programs that draw different shapes using functions from the graphics.h library. The programs draw triangles, lines, squares, rectangles, circles, arcs, sectors, ellipses and implement functions like putpixel, getpixel, setcolor, floodfill, setlinestyle and setfillstyle. The last program implements Bresenham's line drawing algorithm to draw a line between two points.

Uploaded by

Rahul Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROGRAM TO DRAW A TRIANGLE AND A LINE

#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "g:\\tc\\bgi"); line(200,100,100,200); line(100,200,300,200); line(300,200,200,100); outtextxy(175,160,"triangle"); line(400,200,500,200); outtextxy(435,185,"line"); getch(); }

PROGRAM TO DRAW A SQUARE & A RECTANGLE


#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "g:\\tc\\bgi"); rectangle(100,100,200,200); outtextxy(120,150,"square"); rectangle(300,100,600,200); outtextxy(400,150,"rectangle"); getch(); }

PROGRAM TO DRAW RECTANGLE, LINE , LINETO

#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int g=DETECT, gmode; initgraph(&g, &gmode,"c:tc\lib"); rectangle(150,70,30,150); line(20,20,70,20); lineto(90,50); getch(); closegraph(); }

PROGRAM TO DRAW CIRCLE, ARC, SECTOR


#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int g=DETECT, gmode; initgraph(&g, &gmode,"c:tc\lib"); circle(100,100,50); arc(35,35,90,270,35); sector(180,180,50,130,90,360); getch(); closegraph(); }

PROGRAM TO IMPLEMENT ELLIPSE, PUTPIXEL, GETPIXEL


#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int g=DETECT, gmode; initgraph(&g, &gmode,"c:tc\bin"); setcolor(RED); ellipse(150,150,0,360,70,35); putpixel(150,150,GREEN); getpixel(150,150); getch(); closegraph(); }

PROGRAM TO IMPLEMENT SETCOLOR & FLOODFILL #include <graphics.h> #include <conio.h> void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:/tc/bgi"); setcolor(RED); setfillstyle(SOLID_FILL, WHITE); rectangle(0,0, 300, 400); elipse(150,150,0,360,70,35); floodfill(62,62,RED); getch(); closegraph(); }

PROGRAM TO IMPLEMENT SETLINESTYLE, SETFILLSTYLE


#include<graphics.h> #include<conio.h> #include<iostream.h> void main() { clrscr(); int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, ""); setlinestyle(1,0,2); line(40,40,110,40); setlinestyle(2,0,2); line(50,50,110,50); setfillstyle(2,2); line(60,60,110,60); getch(); closegraph(); }

PROGRAM TO DISPLAY COLOURED BACKGROUND & TEXT


#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int g=DETECT, gmode; initgraph(&g, &gmode,"c:tc\bin"); setcolor(4); rectangle(400,80,5,300); setbkcolor(4); setcolor(1); outtextxy(130,140,"Welcome to the world of graphics"); getch(); closegraph(); }

PROGRAM FOR BRESENHAMS LINE DRAWING ALGORITHM


#include<iostream.h> #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd = DETECT, gm; int dx, dy, e, end; float x1, x2, y1, y2, x, y; initgraph(&gd, &gm, "c:\\tc\\bgi"); cout<<"Enter Value of X1: "; cin>>x1; cout<<"\n Enter Value of Y1: "; cin>>y1; cout<<"\n Enter Value of X2: "; cin>>x2; cout<<"\n Enter Value of Y2: "; cin>>y2; dx = abs(x1 - x2); dy = abs(y1 - y2); e = 2 * dy - dx; if(x1 > x2) { x = x2; y = y2; end = x1; } else { x = x1; y = y1; end = x2; } putpixel(x, y,RED); while(x < end) { x = x + 1; if(e < 0)

{ e = e + 2 * dy; } else { y = y + 1; e = e + 2 * (dy - dx); } putpixel(x, y,RED); } getch(); closegraph(); }

You might also like