CGPresentation Week6 (MidpointLine&Circle)
CGPresentation Week6 (MidpointLine&Circle)
Algorithm
Two Cases:
Advantages of Mid Point Line Drawing Algorithm-
• 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-
•
• 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:
• Thus,
• Xk+1 = X1 + 1 = 20 + 1 = 21
• Yk+1 = Y1 + 1 = 10 + 1 = 11
• Dnew = Dinitial + ΔD = 6 + (-4) = 2
•
• 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:
• Thus,
• Xk+1 = X1 + 1 = 20 + 1 = 21
• Yk+1 = Y1 + 1 = 10 + 1 = 11
• Dnew = Dinitial + ΔD = 6 + (-4) = 2
• 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:
• 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)
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;
glutDisplayFunc(midline);
glutMainLoop();
return 0;
}
Midpoint Line Drawing OUTPUT
Implementation in OpenGL
Midpoint Circle Drawing:
#include <stdio.h>
Drawing
using namespace std;
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;
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