Draw a Circle on a Polar Graph

In this article, nosotros will discuss how to draw circles using Bresenham Equation and Polar Equation.

Circle Drawing Algorithm

Information technology is non easy to brandish a continuous shine arc on the computer screen as our computer screen is fabricated of pixels organized in matrix form. So, to draw a circle on a computer screen it should always cull the nearest pixels from a printed pixel so as they could form an arc .

  • Consider circles centered at the origin with integer radii.
  • Can apply translations to get non-origin centered circles.
  • Equation of circle is given past:

x2 + y2 =  Rii
y = +/-sqrt(R2-x2)

  • The given equation tin can exist written as:

F(10, y)= x2+ y2-R2=0
five.

  • Use of Symmetry: But need to calculate one octant. One tin become points in the other vii octants as follows:
    • Plotpoint(x, y)
    • Plotpoint(y, x)
    • Plotpoint(10, -y)
    • Plotpoint(-y, 10)
    • Plotpoint(-x, -y)
    • Plotpoint(-y, -x)
    • Plotpoint(-ten, y)
    • Plotpoint(-y, x)

Circle Drawing Using Bresenham Equation

Bresenham Equation uses the primal feature of a circumvolve that is highly symmetric. And so, for the whole 360 degrees circumvolve, divide it into 8-parts each octant of 45 degrees. In order to that, the idea is to apply Bresenham's Circle Algorithm for the calculation of the locations of the pixels in the commencement octant of 45 degrees. It assumes that the circumvolve is centered on the origin. So for every pixel (x, y) draw a pixel in each of the viii octants of the circle as shown below:

In Bresenham's Algorithm at any indicate (x, y) we have two options either to choose the side by side pixel in the east i.e., (x + 1, y) or in the south-e i.e., (10 + 1, y – 1). And this tin exist decided by using the decision parameter d as:

  • If d > 0, then (ten + ane, y – 1) is to be chosen as the next pixel as it volition be closer to the arc.
  • Else (x + 1, y) is to exist chosen equally the next pixel.

Below is the algorithm for Bresenham Equation:

  • F(ten, y) = x2 + y2 = 0 Signal lies on the circle.
  • F(x, y) > 0 Point lies exterior the circle.
  • F(ten, y) < 0 Point lies inside the circumvolve.
  • If d >= 0 and then update x as (x + 1) and y = (y – 1) which gives new d
  • If d < 0 then update x as (10 + i) which gives the new value of d

C

#include <GL/gl.h>

#include <GL/glut.h>

#include <math.h>

#include <stdio.h>

int xo, yo, r;

void Brandish( void )

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(ane, 0, 0);

glPointSize(2);

int x = 0;

int y = r;

float p = 5 / 4 - r;

glColor3f(ane, 0, 0);

glBegin(GL_POINTS);

while (y > x) {

if (p < 0) {

x++;

p = p + 2 * x + 1;

}

else {

y--;

x++;

p = p + two * (x - y) + 1;

}

glVertex2d(ten + xo, y + yo);

glVertex2d(-x + xo, y + yo);

glVertex2d(ten + xo, -y + yo);

glVertex2d(-x + xo, -y + yo);

glVertex2d(y + yo, x + xo);

glVertex2d(-y + yo, x + xo);

glVertex2d(y + yo, -ten + xo);

glVertex2d(-y + yo, -x + xo);

}

glEnd();

glFlush();

}

int main( int argc, char ** argv)

{

printf ( "10-coordinate Y-coordinate radius:" );

scanf ( "%d %d %d" , &xo, &yo, &r);

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(g, 1000);

glutInitWindowPosition(100, 100);

glutCreateWindow( "GeeksforGeeks" );

glClearColor(ane, 1, 1, 1);

gluOrtho2D(-500, 500, -500, 500);

glutDisplayFunc(Display);

glutMainLoop();

return 0;

}

Output:

Time Complexity: O(N)
Auxiliary Space: O(ane)

Circumvolve Using Polar Equation

In the Polar Equation system, the idea is to think of a clock with one paw. Movement out a distance r , sometimes called the modulus, along with the manus from the origin, then rotate the paw upwards (counterclockwise) by an bending θ to achieve the point. Below is the algorithm for the Polar Equation:

  1. Initialize the variables rad, center(x0, y0), index value or increase value i, and define a circle using polar coordinates θ_end = 100.
  2. If θ_end < θ, then exit from the loop.
  3. Detect the value of ten equally rad*cos(angle) and y as rad*sin(angle).
  4. Plot the eight points, found by symmetry i.due east., the center (x0, y0) at the current (x, y) coordinates.
    •  Plot (x + xo, y + yo)
    • Plot (-x + xo, -y + yo)
    • Plot (y + xo, x + yo)
    • Plot (-y + xo, -ten + yo)
    • Plot (-y + xo, 10 + yo)
    • Plot (y + xo, -x + yo)
    • Plot (-ten + xo, y + yo)
    • Plot (ten + xo, -y + yo)
  5. Increment the angle by i*2*(M_PI/100).

Below is the program to implement the above arroyo:

C

#include <GL/overabundance.h>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

float xo, yo, rad;

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(one, one, one);

float bending;

glBegin(GL_POLYGON);

for ( int i = 0; i < 100; i++) {

angle = i * 2 * (M_PI / 100);

glVertex2f(xo + ( cos (angle) * rad),

yo + ( sin (angle) * rad));

}

glEnd();

glFlush();

}

int main( int argc, char ** argv)

{

glutInit(&argc, argv);

printf ( "Enter x y radius " );

scanf ( "%f %f %f" , &xo, &yo, &rad);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500, 500);

glutInitWindowPosition(200, 200);

glutCreateWindow( "GeeksforGeeks" );

glClearColor(0, i, 0, one);

gluOrtho2D(-500, 500, -500, 500);

glutDisplayFunc(Brandish);

glutMainLoop();

render 0;

}

Output:

Time Complexity: O(N)
Auxiliary Space: O(one)


webberwassurs.blogspot.com

Source: https://www.geeksforgeeks.org/draw-circle-using-polar-equation-and-bresenhams-equation/

0 Response to "Draw a Circle on a Polar Graph"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel