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

Lecture 4

Uploaded by

aldhiha1
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)
13 views

Lecture 4

Uploaded by

aldhiha1
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/ 13

Lecture 4

Circle Drawing Algorithms-


Overview
The basic equation for drawing a circle depends on each pixel having (x,
y, R) )R is the radius)

R2 = x2 + y2
this equation supposes that we will start in (0,0) but if we start in another
position such as xc,yc
then x= x-xc and y= y-yc
R2 = (x-xc)2 + (y-yc)2
Y = yc±√𝑟 2 + 𝑥 − 𝑥𝑐 2
‫ في الجزء الموجب والسالب‬y ‫هتخلينا نرسم‬x ‫ مرة موجب ومرة سالب وبالتالي زيادة‬y ‫ ليها قيمة من‬x ‫وبالتالي اصبحت كل‬

Example
"Draw a circle from the point (20, 20) with a radius of 10."
Then we will calculate the negative value for this

The drawbacks of this method include


 the use of numerical fractions, making calculations computationally
intensive.
 Additionally, mathematical operations involving square roots and
squares add complexity, potentially increasing the computational
load on the system.
Trigonometric formula
An alternative method for drawing a circle involves using trigonometric
formulas based on angles to calculate the values of x and y. The
parametric equations for a circle centered at (xc,yc) with radius R are given
by:
Example
"Draw a circle from the point (20, 20) with a radius of 10."
The drawbacks of this method include
 the use of numerical fractions, making calculations computationally
intensive.
 Additionally, mathematical operations involving sin – cos,
potentially increase the computational load on the system.

In computer graphics, popular algorithms used to generate circles are-

Mid-Point Circle Drawing Algorithm:

The Midpoint Circle Drawing Algorithm can be modified to work with


integer arithmetic and draw only one octant, which can then be mirrored
to complete the entire circle.
A circle is made up of 8 Equal Octets so we need to find only the
coordinates of any octet rest we can conclude using those coordinates.
We took octet-2. Where X and Y will represent the pixel. Let us make a
function Circle () with parameters coordinates of Centre (Xc, Yc) and
pixel point (X, Y) that will plot the pixel on a screen.

We will find pixels assuming that the Center is at Origin (0,0) then we will
add the coordinates of the center to the corresponding X and Y while drawing
a circle on the screen.
The points generation using the Mid-Point Circle Drawing Algorithm involves
the following steps-

Step-01:

Assign the starting point coordinates (X0, Y0) as


X0 = 0
Y0 = R
Step-02:

Calculate the value of the initial decision parameter P0 as

P0 = 1 – R
Step-03:
Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).
Follow the two cases

Example
Given the center point coordinates (0, 0) and radius as 10, generate all the
points to form a circle.
Solution
Centre Coordinates of Circle (X0, Y0) = (0, 0)
Radius of Circle = 10

Step-01:

Assign the starting point coordinates (X0, Y0) as-

 X0 = 0
 Y0 = R = 10
Step-02:

Calculate the value of initial decision parameter P0 as-


P0 = 1 – R
P0 = 1 – 10
P0 = -9

Step-03:

As Pinitial < 0, so case-01 is satisfied.

K p XK+1 Y k+1 2XK+1 2yK+1

0 -9 1 10 2 20

1 -6 2 10 4 20

2 -1 3 10 6 20

3 6 4 9 8 18

4 -3 5 9 10 18

5 8 6 8 12 16

6 5 7 7 14 14

Thus,
 Xk+1 = Xk + 1 = 0 + 1 = 1
 Yk+1 = Yk = 10
 Pk+1 = Pk + 2 x Xk+1 + 1 = -9 + (2 x 1) + 1 = -6
Circle (Xc,Yc,X,Y) {
Plot (Y+Xc , X+Yc) ……Octet-1
Plot (X+Xc , Y+Yc) ……Octet-2
Plot (-X+Xc , Y+Yc) ……Octet-3
Plot (-Y+Xc , X+Yc) …..Octet-4
Plot (-Y+Xc , -X+Yc) ……Octet-5
Plot (-X+Xc , -Y+Yc) ……Octet-6
Plot (X+Xc , -Y+Yc) ……Octet-7
Plot (Y+Xc , -X+Yc) ……Octet-8
}

// C++ program for implementing

// Mid-Point Circle Drawing Algorithm

#include<iostream>

using namespace std;

// Implementing Mid-Point Circle Drawing


Algorithm
void midPointCircleDraw(int x_centre, int
y_centre, int r)

int x = r, y = 0;

// Printing the initial point on the axes

// after translation

cout << "(" << x + x_centre << ", " << y +


y_centre << ") ";

// When radius is zero only a single

// point will be printed

if (r > 0)

cout << "(" << x + x_centre << ", " << -y


+ y_centre << ") ";

cout << "(" << y + x_centre << ", " << x


+ y_centre << ") ";

cout << "(" << -y + x_centre << ", " << x


+ y_centre << ")\n";

// Initialising the value of P


int P = 1 - r;

while (x > y)

y++;

// Mid-point is inside or on the


perimeter

if (P <= 0)

P = P + 2*y + 1;

// Mid-point is outside the perimeter

else

x--;

P = P + 2*y - 2*x + 1;

// All the perimeter points have already


been printed

if (x < y)

break;
// Printing the generated point and its
reflection

// in the other octants after


translation

cout << "(" << x + x_centre << ", " << y


+ y_centre << ") ";

cout << "(" << -x + x_centre << ", " << y


+ y_centre << ") ";

cout << "(" << x + x_centre << ", " << -y


+ y_centre << ") ";

cout << "(" << -x + x_centre << ", " << -


y + y_centre << ")\n";

// If the generated point is on the line


x = y then

// the perimeter points have already


been printed

if (x != y)

cout << "(" << y + x_centre << ", "


<< x + y_centre << ") ";

cout << "(" << -y + x_centre << ", "


<< x + y_centre << ") ";

cout << "(" << y + x_centre << ", "


<< -x + y_centre << ") ";

cout << "(" << -y + x_centre << ", "


<< -x + y_centre << ")\n";

}
}

// Driver code

int main()

// To draw a circle of radius 3 centered at


(0, 0)

midPointCircleDraw(0, 0, 3);

return 0;

Output:

(3, 0) (3, 0) (0, 3) (0, 3)


(3, 1) (-3, 1) (3, -1) (-3, -1)
(1, 3) (-1, 3) (1, -3) (-1, -3)
(2, 2) (-2, 2) (2, -2) (-2, -2)

You might also like