0% found this document useful (0 votes)
18 views10 pages

Program 1

The document contains five programs implementing different line and circle drawing algorithms in C++ using graphics. The algorithms include DDA, Bresenham, and Midpoint methods for lines and circles. Each program initializes a graphics window, takes input coordinates, and draws the respective shapes on the screen.

Uploaded by

Roushan Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Program 1

The document contains five programs implementing different line and circle drawing algorithms in C++ using graphics. The algorithms include DDA, Bresenham, and Midpoint methods for lines and circles. Each program initializes a graphics window, takes input coordinates, and draws the respective shapes on the screen.

Uploaded by

Roushan Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROGRAM 1:- DDA LINE DRAWING ALGORITHM

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

int round(float num)


{ return (int)(num + 0.5);}

void DDA(int x1, int y1, int x2, int y2)


{
int dx = x2 - x1;
int dy = y2 - y1;
int steps;
if (abs(x1) > abs(y1))
steps = abs(x1);
else
steps = abs(y1);

float x_incr = dx / (float)steps;


float y_incr = dy / (float)steps;
float x = x1;
float y = y1;
for (int i = 0; i < steps; i++)
{
putpixel(round(x), round(y), WHITE);
x += x_incr;
y += y_incr;
delay(1);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, ("C:\\TURBOC3\\BGI"));
int l = 150;
int xc = 320 - l / 2;
int yc = 240 - l / 2;
DDA(xc, yc, xc, yc + l);
DDA(xc, yc + l, xc + l, yc + l);
DDA(xc + l, yc + l, xc + l, yc);
DDA(xc + l, yc, xc, yc);
getch();
closegraph();
return 0;
}

PROGRAM 2 :- BRESENHAM LINE DRAWING ALGORITHM


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

void bresenham(int x1, int y1, int x2, int y2)


{

int dx = x2 - x1, dy = y2 - y1;


int pk = 2 * dy - dx;
if (abs(dy) > abs(dx))
cout << "|m|>1" << endl;
else
{
int x = x1, y = y1;

while (x != x2 || y != y2)
{

putpixel(x, y, 15);
if (pk < 0)
{
x++;
pk += 2 * dy;
}
else
{
x++;
y++;
pk = pk + 2 * dy - 2 * dx;
}
delay(1);
}
}
}

int main()
{

int gd = DETECT, gm;


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
int x1, x2, y1, y2;

cout << (X1, Y1) << endl;


cin >> x1 >> y1;
cout << (X2, Y2) << endl;
cin >> x2 >> y2;

bresenham(x1 + 20, y1, x2 + 20, y2);


bresenham(x1, y1, x2, y2);
bresenham(x1 - 20, y1, x2 - 20, y2);

getch();
closegraph();
return 0;
}

PROGRAM 3: - MID POINT LINE DRAWING ALGORITHM

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

int round(float n){ return (int)( n + 0.5); }

void midLine(int x1, int y1, int x2, int y2){


int dx = x2 - x1, dy = y2 - y1;
float di = dy - dx/2;
if ( abs(dy) > abs (dx) ){cout<<"|M|>1"<<endl;}
else {
putpixel(x1, y1, 15);
float x = x1, y = y1;
while( x != x2 && y != y2){
if( di < 0 ){
x++;
di += dy;
}
else{
x++;
y++;
di = di + dy - dx;
}
putpixel(round(x), round(y), 15);
delay(1);
}
}

main(){

int gd = DETECT, gm;


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
int x1,y1,x2,y2;

cout<<"ENTER (X1,Y1)"<<endl;
cin>>x1>>y1;

cout<<"ENTER (X2,Y2)"<<endl;
cin>>x2>>y2;

midLine(x1,y1,x2,y2);

getch();
closegraph();

return 0;
}

PROGRAM 4:- BRESENHEM CIRCLE DRAWING ALGORITHM

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

void drawCircle(int x, int y, int a, int b)


{
putpixel(x + a, y + b, 4);
putpixel(x + a, y - b, 4);
putpixel(x - a, y + b, 4);
putpixel(x - a, y - b, 4);
putpixel(x + b, y + a, 4);
putpixel(x + b, y - a, 4);
putpixel(x - b, y + a, 4);
putpixel(x - b, y - a, 4);
delay(10);
}

void bresenhamCircle(int x, int y, int r)


{

int a = 0, b = r;
int d = 3 - 2 * r;

while (a <= b)
{

drawCircle(x, y, a, b);
if (d < 0)
{
d += 4 * a + 6;
}
else
{
d += 4 * (a - b) + 10;
b--;
}
a++;
delay(1);
}
}

main()
{

int gd = DETECT, gm;


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

bresenhamCircle(320, 240, 200);


bresenhamCircle(320, 240, 175);
bresenhamCircle(320, 240, 150);

getch();
closegraph();
return 0;
}

PROGRAM 5 :- MID POINT CIRCLE DRAWING ALGORITHM

#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>
void drawCirclePoints(int xc, int yc, int x, int y, int color = 15)
{
putpixel(xc + x, yc + y, color);
putpixel(xc - x, yc + y, color);
putpixel(xc + x, yc - y, color);
putpixel(xc - x, yc - y, color);
putpixel(xc + y, yc + x, color);
putpixel(xc - y, yc + x, color);
putpixel(xc + y, yc - x, color);
putpixel(xc - y, yc - x, color);
}

void midpointCircle(int xc, int yc, int r)


{
int x = 0;
int y = r;
int p = 1 - r;

drawCirclePoints(xc, yc, x, y);


while (x < y)
{
x++;
if (p < 0)
{
p += 2 * x + 1;
}
else
{
y--;
p += 2 * (x - y) + 1;
}
drawCirclePoints(xc, yc, x, y);
delay(1);
}
}

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

int xc = 320, yc = 240, r = 100;


midpointCircle(xc, yc, r);

getch();
closegraph();
return 0;
}

You might also like