Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
106 views19 pages

C++ Pattern Drawing Assignment

The document describes a C++ program that uses concepts like encapsulation, DDA line drawing algorithm, and Bresenham's circle drawing algorithm to draw patterns. The program defines classes like dcircle and dline to encapsulate the properties and behaviors for drawing circles and lines. It takes user input for the coordinates of circles and lines, draws the circles using the circle drawing algorithm, and draws the lines using the DDA line algorithm.

Uploaded by

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

C++ Pattern Drawing Assignment

The document describes a C++ program that uses concepts like encapsulation, DDA line drawing algorithm, and Bresenham's circle drawing algorithm to draw patterns. The program defines classes like dcircle and dline to encapsulate the properties and behaviors for drawing circles and lines. It takes user input for the coordinates of circles and lines, draws the circles using the circle drawing algorithm, and draws the lines using the DDA line algorithm.

Uploaded by

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

1

2
3
4
5
6
7
8
9
10
Name: Sonu Shriram Vishwakarma
Roll No. : SE63

Assignment No. 1
Write C++ program to draw the following pattern. Use DDA line and Bresenham’s circle
drawing algorithm. Apply the concept of encapsulation

Source Code:

#include <iostream>
# include <graphics.h>
# include <stdlib.h>
using namespace std;
class dcircle
{
private: int x0, y0;
public:
dcircle()
{
x0=0;
y0=0;
}
void setoff(int xx, int yy)
{
x0=xx;
y0=yy;
}
void drawc(int x1, int y1, int r)
{
float d;
int x,y;
x=0;
y=r;
d=3-2*r;
do
{
putpixel(x1+x0+x, y0+y-y1, 15);
putpixel(x1+x0+y, y0+x-y1,15);
putpixel(x1+x0+y, y0-x-y1,15);
putpixel(x1+x0+x,y0-y-y1,15);
putpixel(x1+x0-x,y0-y-y1,15);
putpixel(x1+x0-y, y0-x-y1,15);
putpixel(x1+x0-y, y0+x-y1,15);
putpixel(x1+x0-x, y0+y-y1,15);
if (d<=0)
{
d = d+4*x+6;
}
else
{
d=d+4*(x-y)+10;
y=y-1;
}
x=x+1;
}
while(x<y);
}
};
class pt
{
protected: int xco, yco,color;
public:
pt()
{
xco=0,yco=0,color=15;
}
void setco(int x, int y)
{
xco=x;
yco=y;
}
void setcolor(int c)
{
color=c;
}
void draw()
{
putpixel(xco,yco,color);
}
};
class dline:public pt
{
private: int x2, y2;
public:
dline():pt()
{
x2=0;
y2=0;
}
void setline(int x, int y, int xx, int yy)
{
pt::setco(x,y);
x2=xx;
y2=yy;
}
void drawl( int colour)
{
float x,y,dx,dy,length;
int i;
pt::setcolor(colour);
dx= abs(x2-xco);
dy=abs(y2-yco);
if(dx>=dy)
{
length= dx;
}
else
{
length= dy;
}
dx=(x2-xco)/length;
dy=(y2-yco)/length;
x=xco+0.5;
y=yco+0.5;
i=1;
while(i<=length)
{
pt::setco(x,y);
pt::draw();
x=x+dx;
y=y+dy;
i=i+1;
}
pt::setco(x,y);
pt::draw();
}
};
int main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm, NULL);
int x,y,r, x1, x2, y1, y2, xmax, ymax, xmid, ymid, n, i;
dcircle c;
cout<<"\nenter coordinates of centre of circle : ";
cout<<"\n enter the value of x : ";
cin>>x;
cout<<"\nenter the value of y : ";
cin>>y;
cout<<"\nenter the value of radius : ";
cin>>r;
xmax= getmaxx();
ymax=getmaxy();
xmid=xmax/2;
ymid=ymax/2;
setcolor(1);
c.setoff(xmid,ymid);
line(xmid, 0, xmid, ymax);
line(0,ymid,xmax,ymid);
setcolor(15);
c.drawc(x,y,r);
pt p1;
p1.setco(100,100);
p1.setcolor(14);
dline l;
l.setline(x1+xmid, ymid-y1, x2+xmid, ymid-y2);
cout<<"Enter Total Number of lines : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter co-ordinates of point x1 : ";
cin>>x1;
cout<<"enter coordinates of point y1 : ";
cin>>y1;
cout<<"Enter co-ordinates of point x2 : ";
cin>>x2;
cout<<"enter coordinates of point y2 : ";
cin>>y2;
l.setline(x1+xmid, ymid-y1, x2+xmid, ymid-y2);
l.drawl(15);
}
cout<<"\nEnter coordinates of centre of circle : ";
cout<<"\n Enter the value of x : ";
cin>>x;
cout<<"\nEnter the value of y : ";
cin>>y;
cout<<"\nEnter the value of radius : ";
cin>>r;
setcolor(5);
c.drawc(x,y,r);
getch();
delay(200);
closegraph();
return 0;
}

Input :

For circle 1 (internal circle):


Value Of X : 100
Value Of Y : 70
Value Of R : 30
Total Number of lines: 3

Line No.1:
Value of X1: 40
Value of Y1: 40
Value of X2: 100
Value of Y2: 124

Line No.2:
Value of X1: 40
Value of Y1: 40
Value of X2: 160
Value of Y2: 40

Line No.3:
Value of X1: 40
Value of Y1: 40
Value of X2: 100
Value of Y2: 124

For Circle No.2 (External circle):


Value of X: 100
Value of Y: 62
Value of R: 60

Output 1:
Input :

Input for circle 1:

Value of x: 150
Value of y: 100
Value of radius: 20

Total no of lines: 14

Line no. 1:
value of x1: 70
value of y1: 45
value of x2: 200
value of y2: 45

Line no. 2:
value of x1: 200
value of y1: 45
value of x2: 200
value of y2: 140

Line no. 3:
value of x1: 200
value of y1: 140
value of x2: 70
value of y2: 140

Line no. 4:
value of x1: 70
value of y1: 140
value of x2: 70
value of y2: 45

Line no. 5:
value of x1: 70
value of y1: 45
value of x2: 20
value of y2: 45

Line no. 6:
value of x1: 20
value of y1: 45
value of x2: 20
value of y2: 140

Line no. 7:
value of x1: 20
value of y1: 140
value of x2: 70
value of y2: 140

Line no. 8:
value of x1: 70
value of y1: 140
value of x2: 45
value of y2: 190

Line no. 9:
value of x1: 45
value of y1: 190
value of x2: 20
value of y2: 140

Line no. 10:


value of x1: 200
value of y1: 140
value of x2: 175
value of y2: 190

Line no. 11:


value of x1: 175
value of y1: 190
value of x2: 45
value of y2: 190

Line no. 12:


value of x1: 55
value of y1: 45
value of x2: 55
value of y2: 90

Line no. 13:


value of x1: 55
value of y1: 90
value of x2: 35
value of y2: 90

Line no. 14:


value of x1: 35
value of y1: 90
value of x2:
value of y2: 45

Input for circle 2:


Value of x: 45
Value of y: 161
Value of radius: 7
Output 2:

You might also like