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

CGPresentation Week6 (MidpointLine&Circle)

The Midpoint Line Drawing algorithm uses simple arithmetic operations to accurately determine the points along a line segment between two coordinates. It calculates the initial decision parameter D and the change in D per step ΔD to determine whether the next point is directly above, below, left or right of the current point. This algorithm is efficient and easy to implement, but may not be suitable for complex graphics. Similarly, the Midpoint Circle Drawing algorithm uses the equation of a circle to determine the points around the circumference by calculating the decision parameter P and change in P per step ΔP. It generates points for one octant then uses symmetry to fill out the entire circle. However, the circle produced may not be smooth. Both algorithms take advantage of symmetric

Uploaded by

Ayesha Rahim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

CGPresentation Week6 (MidpointLine&Circle)

The Midpoint Line Drawing algorithm uses simple arithmetic operations to accurately determine the points along a line segment between two coordinates. It calculates the initial decision parameter D and the change in D per step ΔD to determine whether the next point is directly above, below, left or right of the current point. This algorithm is efficient and easy to implement, but may not be suitable for complex graphics. Similarly, the Midpoint Circle Drawing algorithm uses the equation of a circle to determine the points around the circumference by calculating the decision parameter P and change in P per step ΔP. It generates points for one octant then uses symmetry to fill out the entire circle. However, the circle produced may not be smooth. Both algorithms take advantage of symmetric

Uploaded by

Ayesha Rahim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Mid point Line Drawing

Algorithm
Two Cases:
Advantages of Mid Point Line Drawing Algorithm-

• The advantages of Mid Point Line Drawing Algorithm are-


• Accuracy of finding points is a key feature of this algorithm.
• It is simple to implement.
• It uses basic arithmetic operations.
• It takes less time for computation.
• The resulted line is smooth as compared to other line drawing
algorithms.
The disadvantages of Mid Point Line Drawing Algorithm are-

• This algorithm may not be an ideal choice for complex graphics and
images.
• In terms of accuracy of finding points, improvement is still needed.
• There is no any remarkable improvement made by this algorithm
Example
Calculate the points between the starting coordinates (20, 10) and
ending coordinates (30, 18).
• Solution-

• Starting coordinates = (X1, Y1) = (20, 10)


• Ending coordinates = (X2, Y2) = (30, 18)
Step-01:

• Calculate ΔX and ΔY from the given input.


• ΔX = X2 – X1 = 30 – 20 = 10
• ΔY =Y2 – Y1 = 18 – 10 = 8
Step-02:


• Calculate Dinitial and ΔD as-
• Dinitial = 2ΔY – ΔX = 2 x 8 – 10 = 6
• ΔD = 2(ΔY – ΔX) = 2 x (8 – 10) = -4
Step-03:

• As Dinitial >= 0, so case-02 is satisfied.

• Thus,
• Xk+1 = X1 + 1 = 20 + 1 = 21
• Yk+1 = Y1 + 1 = 10 + 1 = 11
• Dnew = Dinitial + ΔD = 6 + (-4) = 2

• Similarly, Step-03 is executed until the end point is reached.


Example: A line has a starting point (6,10) and ending point (13,17).
Apply the Mid-point Line Drawing algorithm to draw a line.
• Solution-

• Starting coordinates = (X1, Y1) = (6,10)


• Ending coordinates = (X2, Y2) = (13,17)
Step-01:

• Calculate ΔX and ΔY from the given input.


• ΔX = X2 – X1 = 13-6 = 7
• ΔY =Y2 – Y1 = 17-10 = 7
Step-02:


• Calculate Dinitial and ΔD as-
• Dinitial = 2ΔY – ΔX = 2 x 8 – 10 = 6
• ΔD = 2(ΔY – ΔX) = 2 x (8 – 10) = -4
Step-03:

• As Dinitial >= 0, so case-02 is satisfied.

• Thus,
• Xk+1 = X1 + 1 = 20 + 1 = 21
• Yk+1 = Y1 + 1 = 10 + 1 = 11
• Dnew = Dinitial + ΔD = 6 + (-4) = 2

• Similarly, Step-03 is executed until the end point is reached.


Home Practice:
Calculate the points between the starting coordinates (5, 9) and ending coordinates (12,
16).
Mid Point Circle Drawing
Algorithm
Mid Point Circle Drawing Algorithm
• Given the center point and radius of circle,
• Mid Point Circle Drawing Algorithm attempts to generate the points of
one octant.
• The points for other octants are generated using the eight symmetry
property.
• Symmetric Property
• The Symmetric Property states that for all real numbers x and
y,
if x=y , then y=x
Advantages of Mid Point Circle Drawing Algorithm
• The advantages of Mid Point Circle Drawing Algorithm are-
• It is a powerful and efficient algorithm.
• The entire algorithm is based on the simple equation of circle X2 + Y2 =
R2.
• It is easy to implement from the programmer’s perspective.
• This algorithm is used to generate curves on raster displays.
Disadvantages of Mid Point Circle Drawing Algorithm

• The disadvantages of Mid Point Circle Drawing Algorithm are-


• Accuracy of the generating points is an issue in this algorithm.
• The circle generated by this algorithm is not smooth.
• This algorithm is time consuming.
Important Points
• Circle drawing algorithms take the advantage of 8 symmetry property of circle.
• Every circle has 8 octants and the circle drawing algorithm generates all the
points for one octant.
• The points for other 7 octants are generated by changing the sign towards X
and Y coordinates.
• To take the advantage of 8 symmetry property, the circle must be formed
assuming that the center point coordinates is (0, 0).
• If the center coordinates are other than (0, 0), then we add the X and Y
coordinate values with each point of circle with the coordinate values
generated by assuming (0, 0) as center point.
To Draw a circle at Origin

• If we have to draw a circle at origin then


• Given,
• X=0
• Y=10 (any value)
• Y=R
• R is the radius of circle.
• It is also possible to have value of R
instead of Y because both a equal to each
other.
To draw a circle at center
• If we have to draw a circle at
center then
• Given,
• X=0
• Y=10 (any value)
• Y=R
• We also have (Xc,Yc)= (2,2) means
any value
Algorithm Procedure
• Given:
• Centre point of Circle = (X0, Y0)
• Radius of Circle = R

• The points generation using Mid Point Circle Drawing Algorithm


involves the some steps.
Step-01:
• Assign the starting point coordinates (X0, Y0) as:
• X0 = 0
• Y0 = R
Step-02: Decision Parameter
• Calculate the value of initial decision parameter Pk as:
• Pk = 1 – R
Step-03:
• Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).
• Find the next point of the first octant depending on the value of
decision parameter Pk.
Two Cases
Step-04:
• If the given centre point (X0, Y0) is not (0, 0), then do the following and
plot the point-
• Xplot = Xc + X0
• Yplot = Yc + Y0

• Here, (Xc, Yc) denotes the current value of X and Y coordinates.
Step-05:

Keep repeating Step-03 and Step-04 until Xplot >= Yplot.
Step-06:

• Step-05 generates all the


points for one octant.
• To find the points for other
seven octants, follow the eight
symmetry property of circle.
• This is depicted by the
following figure-
Given the center point coordinates (0, 0) and radius as 10, generate all the points to
form a circle.

• Solution-
• Given-
• 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 Pk as-
• Pk = 1 – R
• Pk = 1 – 10
• Pk = -9
Step-03:
• As P k < 0, so case-01 is satisfied.

• 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
Step-04:

• This step is not applicable here as the given center point coordinates
is (0, 0).
Step-05:

Pk Pk+1 (Xk+1, Yk+1)


• Step-03 is executed
similarly until (0, 10)
• Xk+1 >= Yk+1 as follows: -9 -6 (1, 10)
-6 -1 (2, 10)
-1 6 (3, 10)
6 -3 (4, 9)
-3 8 (5, 9)
8 5 (6, 8)
Algorithm Terminates after ( 7,7)
These are all points for Octant-1.
For Octant 2

Octant-1 Points Octant-2 Points

• Algorithm calculates all (0, 10) (7, 7)


the points of octant-1 (1, 10) (8, 6)
and terminates. (2, 10) (9, 5)
(3, 10) (9, 4)
• Now, the points of
(4, 9) (10, 3)
octant-2 are obtained
(5, 9) (10, 2)
using the mirror effect
(6, 8) (10, 1)
by swapping X and Y
(7, 7) (10, 0)
coordinates.
These are all points for Quadrant-1.
For 4 Quadrants
• Now, the points for rest of the part are generated by following the
signs of other quadrants.
• The other points can also be generated by calculating each octant
separately.
• Here, all the points have been generated with respect to quadrant-1-
Quadrant-1 (X,Y) Quadrant-2 (-X,Y) Quadrant-3 (-X,-Y) Quadrant-4 (X,-Y)

(0, 10) (0, 10) (0, -10) (0, -10)

(1, 10) (-1, 10) (-1, -10) (1, -10)

(2, 10) (-2, 10) (-2, -10) (2, -10)

(3, 10) (-3, 10) (-3, -10) (3, -10)

(4, 9) (-4, 9) (-4, -9) (4, -9)

(5, 9) (-5, 9) (-5, -9) (5, -9)

(6, 8) (-6, 8) (-6, -8) (6, -8)

(7, 7) (-7, 7) (-7, -7) (7, -7)

(7, 7) (-7, 7) (-7, -7) (7, -7)

(8, 6) (-8, 6) (-8, -6) (8, -6)

(9, 5) (-9, 5) (-9, -5) (9, -5)

(9, 4) (-9, 4) (-9, -4) (9, -4)

(10, 3) (-10, 3) (-10, -3) (10, -3)

(10, 2) (-10, 2) (-10, -2) (10, -2)

(10, 1) (-10, 1) (-10, -1) (10, -1)

(10, 0) (-10, 0) (-10, 0) (10, 0)


These are all points of the Circle.
Suppose the given centre of the circle is not (0,0) then apply Step 4 as following.
Given the centre point coordinates (4, -4) and radius as 10, generate all the points to form a circle.

• Solution:
• Centre Coordinates of Circle (X0, Y0) = (4, -4)
• Radius of Circle = 10

• As stated in the algorithm,
• We first calculate the points assuming the centre coordinates is (0, 0).
• At the end, we translate the circle.

• Step-01, Step-02 and Step-03 are already completed in Problem-01.
• Now, we find the values of Xplot and Yplot using the formula given in Step-04 of the main algorithm.
• The following table shows the generation of points for Quadrant-1-
• Xplot = Xc + X0 = 4 + X0
• Yplot = Yc + Y0 = 4 + Y0
(Xk+1, Yk+1) (Xplot, Yplot)

(0, 10) (4, 14)


(1, 10) (5, 14)
(2, 10) (6, 14)
(3, 10) (7, 14)
(4, 9) (8, 13)
(5, 9) (9, 13)
(6, 8) (10, 12)
(7, 7) (11, 11)
(8, 6) (12, 10)
(9, 5) (13, 9)
(9, 4) (13, 8)
(10, 3) (14, 7)
(10, 2) (14, 6)
(10, 1) (14, 5)
(10, 0) (14, 4)
These are all points for Quadrant-1.
Implementation in OpenGL
Midpoint Line Drawing:
#include<Windows.h>
Midpoint Line // first include Windows.h header file
which is required

Drawing #include<stdio.h>
#include<gl/GL.h> // GL.h header file
#include<gl/GLU.h> // GLU.h header file
#include<gl/glut.h> // glut.h header file
from freeglut\include\GL folder
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <iostream>
using namespace std;

void draw_pixel(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
if (dy <= dx) {

Midpoint Line // initial value of decision parameter d


int d = dy - (dx / 2);
int x = X1, y = Y1;

Drawing draw_pixel(x, y);


cout << x << "," << y << "\n";

void midline() { // iterate through value of X


glColor3f(1.0, 0.0, 0.0); while (x < X2)
glPointSize(2.0); {
int X1, Y1, X2, Y2; x++;
cout << "Enter starting point (x, y)\n";
// E or East is chosen
cout << "Enter x1: "; if (d < 0)
cin >> X1; d = d + dy;
cout << "Enter y1: ";
cin >> Y1; // NE or North East is chosen
cout << "Enter end point (x, y)\n "; else
cout << "Enter x2: "; {
cin >> X2; d += (dy - dx);
y++;
cout << "Enter y2: "; }
cin >> Y2;
int dx = X2 - X1; // Plot intermediate points
int dy = Y2 - Y1; // putpixel(x,y) is used to print pixel
// of line in graphics
cout << x << "," << y << "\n";
}
}
Midpoint Line Drawing else if (dx < dy)
{
// initial value of decision parameter d
int d = dx - (dy / 2);
int x = X1, y = Y1;
draw_pixel(x, y);
// Plot initial given point
// putpixel(x,y) can be used to print pixel
// of line in graphics
cout << x << "," << y << "\n";

// iterate through value of X


while (y < Y2)
{
y++;
// E or East is chosen
if (d < 0)
d = d + dx;
// NE or North East is chosen
// NE or North East is chosen
else
{
d += (dx - dy);
x++;
}
draw_pixel(x, y);
cout << x << "," << y << "\n";
}
}
}
Midpoint Line Drawing
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Midpoint Line Drawing OpenGL");

glClearColor(1.0, 1.0, 1.0, 1.0);


glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, 300, 0, 300);
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, 500, 500);

glutDisplayFunc(midline);
glutMainLoop();
return 0;
}
Midpoint Line Drawing OUTPUT
Implementation in OpenGL
Midpoint Circle Drawing:
#include <stdio.h>

MidPoint Circle #include <iostream>


#include <GL/glut.h>

Drawing
using namespace std;

int pntX1, pntY1, r;

void plot(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(x + pntX1, y + pntY1);
glEnd();
}

void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
MidPoint Circle void midPointCircleAlgo()
{

Drawing
int x = 0;
int y = r;
float decision = 5 / 4 - r;
plot(x, y);
while (y > x)
{
if (decision < 0)
{
x++;
decision += 2 * x + 1;
}
else
{
y--;
x++;
decision += 2 * (x - y) + 1;
}
plot(x, y);
plot(x, -y);
plot(-x, y);
plot(-x, -y);
plot(y, x);
plot(-y, x);
plot(y, -x);
plot(-y, -x);
}
}
MidPoint Circle void main(int argc, char** argv)
{
Drawing cout << "Enter the coordinates of the
center:\n\n" << endl;

cout << "X-coordinate : "; cin >> pntX1;


cout << "\nY-coordinate : "; cin >> pntY1;
cout << "\nEnter radius : "; cin >> r;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Circle Drawing
Alogrithms");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();

}
MidPoint Circle Drawing OUTPUT
THANKYOU 

You might also like