0% found this document useful (0 votes)
112 views14 pages

CG&V Ass2

The document describes the midpoint circle algorithm for drawing circles in computer graphics. It begins with an overview of the three midpoint algorithms: for circles, ellipses, and conic sections. It then provides details on the midpoint circle algorithm, including how it uses symmetry to plot points and determine the next point through incremental calculations and a decision parameter to choose whether to increase or decrease the y-value at each x-value. The algorithm plots one octant and then uses reflection to generate the full circle.

Uploaded by

abhinav
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)
112 views14 pages

CG&V Ass2

The document describes the midpoint circle algorithm for drawing circles in computer graphics. It begins with an overview of the three midpoint algorithms: for circles, ellipses, and conic sections. It then provides details on the midpoint circle algorithm, including how it uses symmetry to plot points and determine the next point through incremental calculations and a decision parameter to choose whether to increase or decrease the y-value at each x-value. The algorithm plots one octant and then uses reflection to generate the full circle.

Uploaded by

abhinav
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/ 14

MID POINT ALGORITHM

There are three Mid Point Algorithm in Computer Graphics.

1. Mid Point Circle drawing algorithm


2. Mid Point Ellipse drawing algorithm

Mid Point Circle drawing Algorithm

Introduction

In computer graphics, the midpoint circle algorithm is an algorithm used to determine


the points needed for rasterizing a circle. Bresenham's circle algorithm is derived from
the midpoint circle algorithm. The algorithm can be generalized to conic sections.

Fig. Division of Octants


We need to plot the perimeter points of a circle whose center co-ordinates and radius
are given using the Mid-Point Circle Drawing Algorithm.

We use the above algorithm to calculate all the perimeter points of the circle in the first
octant and then print them along with their mirror points in the other octants. This will
work only because a circle is symmetric about it’s centre.

Algorithm

The objective of the algorithm is to find a path through the pixel grid using pixels which
are as close as possible to solutions of . At each step, the path is extended by choosing
the adjacent pixel which satisfies but maximizes. Since the candidate pixels are adjacent,
the arithmetic to calculate the latter expression is simplified, requiring only bit shifts
and additions. But a simplification can be done in order to understand the bitshift. Keep
in mind that a left bitshift of a binary number is the same as multiplying with 2. Ergo, a
left bit shift of the radius only produces the diameter which is defined as radius times
two.

This algorithm starts with the circle equation. For simplicity, assume the center of the
circle is at . Consider first the first octant only, and draw a curve which starts at
point and proceeds counter clockwise, reaching the angle of 45. The fast direction here
(the basis vector with the greater increase in value) is the direction. The algorithm
always takes a step in the positive direction (upwards), and occasionally takes a step in
the slow direction (the negative direction).

From the circle equation is obtained the transformed equation, where is computed only
once during initialization.

Let the points on the circle be a sequence of coordinates of the vector to the point (in
the usual basis). Points are numbered according to the order in which drawn,
with assigned to the first point.

For each point, the following holds. This can be rearranged thus, and likewise for the
next point.
Since for the first octant the next point will always be at least 1 pixel higher than the
last, it is true that.

So, rework the next-point-equation into a recursive one by substituting :Because of the
continuity of a circle and because the maxima along both axes is the same, clearly it will
not be skipping x points as it advances in the sequence. Usually it stays on the same x
coordinate, and sometimes advances by one.

The resulting coordinate is then translated by adding midpoint coordinates. These


frequent integer additions do not limit the performance much, as those square (root)
computations can be spared in the inner loop in turn. Again, the zero in the transformed
circle equation is replaced by the error term.

The initialization of the error term is derived from an offset of ½ pixel at the start. Until
the intersection with the perpendicular line, this leads to an accumulated value of in the
error term, so that this value is used for initialization.

The frequent computations of squares in the circle equation, trigonometric expressions


and square roots can again be avoided by dissolving everything into single steps and
using recursive computation of the quadratic trems from the preceding iterations.

Variant with integer-based arithmetic

Just as with Bresenham’s circle Algorithm, this algorithm can be optimized for integer-
based math. Because of symmetry, if an algorithm can be found that only computes the
pixels for one octant, the pixels can be reflected to get the whole circle.

We start by defining the radius error as the difference between the exact representation
of the circle and the center point of each pixel (or any other arbitrary mathematical
point on the pixel, so long as it's consistent across all pixels). For any pixel with a center
at , the radius error is defined as.

For clarity, this formula for a circle is derived at the origin, but the algorithm can be
modified for any location. It is useful to start with the point on the positive X-axis.
Because the radius will be a whole number of pixels, clearly the radius error will be
zero. Because it starts in the first counter-clockwise positive octant, it will step in the
direction with the greatest travel, the Y direction, so it is clear that. Also, because it
concerns this octant only, the X values have only 2 options: to stay the same as the prior
iteration, or decrease by

1. A decision variable can be created that determines if the following is true:

If this inequality holds, then plot; if not, then plot . So, how to determine if this
inequality holds? Start with a definition of radius error. The absolute value function
does not help, so square both sides, since a square is always positive

Since x > 0, the term , so dividing get’s

Thus, the decision criterion changes from using floating-point operations to simple
integer addition, subtraction, and bit shifting (for the multiply by 2 operations). If, then
decrement the X value. If, then keep the same X value. Again, by reflecting these points
in all the octants, a full circle results.

Drawing incomplete octants

The implementations above always draw only complete octants or circles. To draw only
a certain arc from an angle to an angle , the algorithm needs first to calculate
the and coordinates of these end points, where it is necessary to resort to trigonometric
or square root computations . Then the Bresenham algorithm is run over the complete
octant or circle and sets the pixels only if they fall into the wanted interval. After
finishing this arc, the algorithm can be ended prematurely. The angles are given
as slopes, then no trigonometry or square roots are necessary: simply check that is
between the desired slopes.

Algorithm

1. Read the radius (r) of the circle.

2. Initialize starting position as

x= 0 y=r.

3. Calculate initial value or decision parameter as


P = 1.25 – r.

4. do

plot (x, y)

if (d < 0)

x=x+1

y=y

d = d + 2x + 1

else

x=x+1

y=y- 1

d = d + 2x + 2y + 1

while(x < y)

5. Determine symmetry points

6. Stop.
Explanation of the Algorithm

Fig. Position Of coordinates in Octants

A circle is a symmetrical figure. It has eight-way symmetry as shown in the figure above
Thus, any circle generating algorithm can take advantage of the circle symmetry to plot
eight points by calculating the coordinates of any one point. if Point A in the figure
above is calculated with a circle algorithm, seven more points could be found just by
reflection.

There are two standard methods of mathematically representing a circle centered at the
origin.

1. Polynomial Method
In this method circle is represented by a polynomial equation.

x2 + y2 = r2

where
x : the x coordinate

y : the y coordinate

r : radius of the circle

Here, polynomial equation can be used to find y coordinate for the known x coordinate.
Therefore, the scan converting circle using polynomial method is achieved by
stepping x from 0 to r √2. and each y coordinate is found by evaluating √(r^2- x^2
) for each step of x. This generates the 1/8 Portion (90° to 45°) of the circle. Remaining
part of the circle can be generated just by reflection.

The polynomial method of circle generation is an inefficient method. In this method for
each point both x and r must be squared and x2 must be subtracted from r2; then the
square root of the result must be found out.
2. Trigonometric Method :-

In this method. the circle is represented by use of trigonometric functions

x = rcos⁡θ and y=rsin⁡θ

where

θ : current angle

r : radius of the circle

x : the x coordinate

y : the y coordinate

The scan converting circle using trigonometric method is achieved by stepping 0 from 0
to л/4 radians. and each value of x and y is calculated. However, this method is more
inefficient than the polynomial method because the computation of the values of sinθ
and cosθ is even more time-consuming than the calculations required in the polynomial
method.

The midpoint circle drawing algorithm also uses the eight-way symmetry of the circle to
generate it. It plots 1/8 part of the circle, i.e. from 90° to 45°, as shown in the figure
below. As circle is drawn from 90° to 45°, the x moves in the positive direction and y
moves in the negative direction.

To draw a 1/8 part of a circle we take unit steps in the positive x direction and make use
of decision parameter to determine which of the two possible y positions is closer to the
circle path at each step.
The figure below shows the two possible y Positions (yi and yi + 1) at sampling position
xi + 1. Therefore, we have to determine whether the pixel at Position (xi + 1, yi - 1) is
closer to the circle. For this purpose decision parameter is used. It uses the circle
function (fcircle(x, y) = x2 + y2 - r2) evaluated at the midpoint between these two
pixels.

If di < 0, this midpoint is inside the circle and the pixel on the scan line yi is closer to the
circle boundary.

If di ≥ 0 , the midposition is outside or on the circle boundary, and yi – 1 is closer to the


circle boundary.

The incremental calculation can be determined to obtain the successive decision


parameters. we can obtain a recursive expression for the next decision parameter by
evaluating the circle function at sampling position xi+1 + 1 = xi + 2

Looking at equation ( 1 ) and ( 2 ) we can write

The initial value of decision parameter can be obtained by evaluating circle function at
the start Position (x0, y0) = (0, r).
Program on Mid Point Circle Drawing Algoritm

#include<stdio.h>

#include<graphics.h>

void drawcircle(int x0, int y0, int rad)

int x = rad;

int y = 0;

int err = 0;

while (x >= y)

putpixel(x0 + x, y0 + y, 7);

putpixel(x0 + y, y0 + x, 7);

putpixel(x0 - y, y0 + x, 7);

putpixel(x0 - x, y0 + y, 7);

putpixel(x0 - x, y0 - y, 7);

putpixel(x0 - y, y0 - x, 7);

putpixel(x0 + y, y0 - x, 7);

putpixel(x0 + x, y0 - y, 7);

if (err <= 0)

y += 1;

err += 2*y + 1;
}

if (err > 0)

x -= 1;

err -= 2*x + 1;

int main()

int gdriver=DETECT, gmode, error, x, y, r;

initgraph(&gdriver, &gmode, "");

printf("Enter radius of circle: ");

scanf("%d", &r);

printf("Enter co-ordinates of center(x and y): ");

scanf("%d%d", &x, &y);

drawcircle(x, y, r);

getch();

return 0;

}
Output –

References for Mid Point Algorithm

1. Burrus, D. (n.d.). The Internet of Things Is Far Bigger Than Anyone Realizes. [ONLINE]
WIRED. Available at: http://www.wired.com/insights/2014/11/the-internet-of-things-
bigger/ [Accessed: 1 May 2016].
2. Chen, C. DDoS Attack Detection & Mitigation in SDN, 2015
http://www.slideshare.net/chaochen5245961/cc3736-slides
3. D. Nadeau, T. and Gray, K. (2013). SDN - Software Defined Networks. Sebastopol, CA
4. Distributed ONOS - ONOS - Wiki. 2016. Distributed ONOS - ONOS - Wiki. [ONLINE]
Available at: https://wiki.onosproject.org/display/ONOS/Distributed+ONOS.
[Accessed: 04 September 2016].
5. Flowgrammable. 2016. Instructions & Actions. [ONLINE] Available at:
http://flowgrammable.org/sdn/openflow/actions. [Accessed: 13 April 2016].
Grady, J., Price, C., Christiansen, C. and Richmond, C. (2013). Worldwide DDoS Prevention
Products and Services 2013-2017 Forecst. 1st ed. Framingham, MA USA: International
Data Corporation, p.3.
6. Kreutz, Diego et al. Software-Defined Networking: A Comprehensive Survey.
Proceedings of the IEEE 103.1 (2015): 14-76. Web.
Krishnan, R., Durrani, M. and Phaal, P. (n.d.). Real-time SDN Analytics for DDoS
mitigation. Brocade Communications, p.2. Available at:
http://opennetsummit.org/archives/mar14/site/pdf/2014/sdn-idol/Brocade-SDN-
Idol-Proposal.pdf [Accessed: 1 May 2016].
Ma, Chloe. SDN Secrets Of Amazon And Google [ONLINE] InfoWorld (2014): Web.
7.Available at: http://www.infoworld.com/article/2608106/sdn/sdn-secrets-of-
amazon-and-google.html [Accessed: 2 Nov. 2016].

8.Moshref, Masoud; Bhargava, Apoorv; Gupta, Adhip; Yu, Minlan; Govindan, Ramesh,
Flow-Level State Transition As A New Switch Primitive For SDN, University of Southern
California, 2014. [ONLINE] Available at: http://dl.acm.org/citation.cfm?id=2631439
[Accessed: 25 September 2016]
9. OpenDayLight FAQ. 2016. OpenDayLight FAQ. [ONLINE] Available at:
https://www.opendaylight.org/faq. [Accessed: 22 September 2016].
OpenDaylight Project, OpenDaylight Controller:MD-SAL:Architecture:Clustering 2016
[ONLINE]Availableat:
https://wiki.opendaylight.org/view/OpenDaylight_Controller:MD-
10.SAL:Architecture:Clustering. [Accessed: 04 September 2016].
Ram (Ramki) Krishnan, Muhammad Durrani, Brocade Communications, Real-time.

You might also like