Aim/Problem statement: 

Write C++ program to draw a given pattern. Use DDA line and Bresenham’s circle drawing algorithm. Apply the concept of encapsulation.


DDA Line Drawing Algorithm:

Line is a basic element in graphics. To draw a line, you need two end points between which you can draw a line. Digital Differential Analyzer (DDA) line drawing algorithm is the simplest line drawing algorithm in computer graphics. It works on incremental method. It plots the points from starting point of line to end point of line by incrementing in X and Y direction in each iteration.

DDA line drawing algorithm works as follows:

Step 1: Get coordinates of both the end points (X1, Y1) and (X2, Y2) from user.

Step 2: Calculate the difference between two end points in X and Y direction.

Step 3: Based on the calculated difference in step-2, you need to identify the number of steps to put

pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.

if (absolute(dx) > absolute(dy))

Steps = absolute(dx);

else

Steps = absolute(dy);

Step 4: Calculate the increment in x coordinate and y coordinate.

Xincrement = dx / (float) steps;

Yincrement = dy / (float) steps;

Step 5: Plot the pixels by successfully incrementing x and y coordinates accordingly and complete

the drawing of the line.

for(int i=0; i < Steps; i++)

{

X1 = X1 + Xincrement;

Y1 = Y1 + Yincrement;

putpixel(Round(X1), Round(Y1), ColorName);

}


Bresenham’s Circle Drawing Algorithm:

Circle is an eight-way symmetric figure. The shape of circle is the same in all quadrants. In each quadrant, there are two octants. If the calculation of the point of one octant is done, then the other seven points can be calculated easily by using the concept of eight-way symmetry. Bresenham’s Circle Drawing Algorithm is a circle drawing algorithm that selects the nearest pixel position to complete the arc. The unique part of this algorithm is that is uses only integer arithmetic which makes it significantly faster than other algorithms using floating point arithmetic.

Bresenham’s Circle Drawing Algorithm

Step 1: Read the x and y coordinates of center: (centx, centy)

Step 2: Read the radius of circle: (r)

Step 3: Initialize, x = 0; y = r;

Step 4: Initialize decision parameter: p = 3 – (2 * r)

Step 5:

do {

putpixel(centx+

+x, centy-y, ColorName);

if (p<0)

p = p+(4*x)+6;

else

{

p=p+[4*(x-y)]+10;

y=y-1;

}

x=x+1;

} while(x<y)

Here in step 5, putpixel() function is used which will print Octant-1 of the circle with radius r after the completion of all the iterations of do-while loop. Because of the eight-way symmetry property of circle, we can draw other octants of the circle using following putpixel() functions:


Drawing Pattern using lines and circles:

This pattern is made up of one equilateral triangle and two concentric circles. To draw the triangle, we require coordinates of 3 vertices forming an equilateral triangle. To draw two concentric circles, we require coordinates of common center and radius of both the circles.

We will take coordinates of circle and radius of one of the circle from user. Then using the properties of an equilateral triangle, we can find all 3 vertices of equilateral triangle and radius of other circle. Once we get all these parameters, we can call DDA line drawing and Bresenham’s circle drawing algorithms by passing appropriate parameters to get required pattern.

Drawing Pattern using lines and circles

To draw this pattern, we require two rectangles and one circle. We can use suitable geometry to get coordinates to draw rectangles and circle. Then we can call DDA line drawing and Bresenham’s circle drawing algorithms by passing appropriate parameters to get required pattern.


Conclusion:

We are able to draw the given two patterns using Line and Circle generation algorithm with the help of C++ encapsulation.


Code: 

#include<graphics.h>

#include<stdio.h>

void drawline (int x1, int y1, int x2, int y2)
{
    int dx,dy,m,s;
    float xi,yi,x,y;
    dx=x2-x1;
    dy=y2-y1;
    if(abs(dx)>abs(dy))
        s=abs(dx);
    else
        s=abs(dy);
    xi=dx/(float)s;
    yi=dy/(float)s;
    x=x1;
    y=y1;
    putpixel(x1,y1,WHITE);
    for(m=0;m<s;m++)
    {
        x+=xi;
        y+=yi;
        putpixel(x,y,WHITE);
    }
{
x+=xi;
y+=yi;
putpixel(x,y,WHITE);
}
}
int main()
{
int gd = DETECT, gm = DETECT, gmode;
int x1,y1,x2,y2;
initgraph(&gd, &gm, NULL);
drawline(80,80,400,80);
drawline(400,80,400,400);
drawline(400,400,80,400);
drawline(80,400,80,80);
drawline(80,240,240,80);
drawline(240,80,400,240);
drawline(400,240,240,400);
drawline(80,240,240,400);
circle(240,240,113);
delay(300);
getch();
closegraph();
return 0;
}

Note


(After code heading it's code but it's not showing I will fix that thanks for your support)