Dda Circle Drawing Algorithm in Opengl

The mid-point circle drawing algorithm is an algorithm used to determine the points needed for rasterizing a circle.

We utilize the mid-point algorithm to summate all the perimeter points of the circumvolve in the first octant and then print them along with their mirror points in the other octants. This will work considering a circle is symmetric about its centre.

Circle octants

The algorithm is very similar to the Mid-Signal Line Generation Algorithm. Here, only the boundary condition is different.

For any given pixel (ten, y), the next pixel to be plotted is either (10, y+one) or (x-one, y+1). This can be decided by following the steps beneath.

  1. Observe the mid-indicate p of the two possible pixels i.e (x-0.5, y+1)
  2. If p lies inside or on the circumvolve perimeter, we plot the pixel (x, y+1), otherwise if it's outside we plot the pixel (x-1, y+1)

Boundary Condition : Whether the mid-bespeak lies inside or outside the circumvolve can exist decided by using the formula:-

Given a circle centered at (0,0) and radius r and a signal p(x,y)
F(p) = x2 + ytwo – r2
if F(p)<0, the signal is inside the circle
F(p)=0, the betoken is on the perimeter
F(p)>0, the bespeak is exterior the circle

example

In our program, nosotros denote F(p) with P. The value of P is calculated at the mid-bespeak of the two contending pixels i.e. (ten-0.v, y+i). Each pixel is described with a subscript chiliad.

Pk = (Xk — 0.5)2 + (yk + ane)ii – r2
Now,
xk+1 = 10one thousand or xk-i , yk+ane= yk +1
∴ Pm+1 = (10chiliad+1 – 0.5)ii + (yk+1 +one)2 – r2
= (10k+1 – 0.five)2 + [(yyard +i) + ane]two – rtwo
= (xk+1 – 0.5)ii + (ythousand +1)two + two(yk + ane) + one – rtwo
= (tenk+1 – 0.5)2 + [ – (xyard – 0.5)2 +(xthou – 0.5)two ] + (yk + 1)2 – r2 + 2(y1000 + 1) + ane
= Pgrand + (xk+1 – 0.5)two – (xk – 0.five)2 + ii(yg + ane) + one
= Pk + (102 k+1 – 102 thousand) – (xthou+1 – 10k) + 2(ym + 1) + 1
= Pthou + 2(ygrand +1) + i, when Pyard <=0 i.e the midpoint is inside the circle
(xk+1 = tenk)
Pyard + 2(ym +1) – 2(xyard – ane) + i, when Pk>0 I.e the mid point is outside the circle(tenk+1 = xchiliad-1)

The commencement point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated as follows:-

P1 = (r – 0.v)2 + (0+i)2 – r2
= one.25 – r
= i -r (When rounded off)

Examples:

          Input :          Centre -> (0, 0), Radius -> iii          Output :          (3, 0) (3, 0) (0, three) (0, iii)          (3, 1) (-3, one) (iii, -1) (-three, -1)          (ane, iii) (-ane, 3) (1, -3) (-1, -three)          (2, 2) (-two, 2) (2, -two) (-2, -2)

first point to be plotted

        
          Input :          Centre -> (iv, 4), Radius -> 2          Output :          (6, four) (6, four) (4, 6) (iv, half-dozen)          (6, 5) (2, 5) (vi, 3) (two, iii)          (5, 6) (three, 6) (5, 2) (3, 2)

CPP

#include<iostream>

using namespace std;

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

{

int x = r, y = 0;

cout << "(" << 10 + x_centre << ", " << y + y_centre << ") " ;

if (r > 0)

{

cout << "(" << x + x_centre << ", " << -y + y_centre << ") " ;

cout << "(" << y + x_centre << ", " << x + y_centre << ") " ;

cout << "(" << -y + x_centre << ", " << ten + y_centre << ")\north" ;

}

int P = i - r;

while (10 > y)

{

y++;

if (P <= 0)

P = P + 2*y + 1;

else

{

x--;

P = P + ii*y - 2*10 + 1;

}

if (x < y)

suspension ;

cout << "(" << x + x_centre << ", " << y + y_centre << ") " ;

cout << "(" << -x + x_centre << ", " << y + y_centre << ") " ;

cout << "(" << x + x_centre << ", " << -y + y_centre << ") " ;

cout << "(" << -ten + x_centre << ", " << -y + y_centre << ")\n" ;

if (x != y)

{

cout << "(" << y + x_centre << ", " << ten + y_centre << ") " ;

cout << "(" << -y + x_centre << ", " << 10 + y_centre << ") " ;

cout << "(" << y + x_centre << ", " << -x + y_centre << ") " ;

cout << "(" << -y + x_centre << ", " << -ten + y_centre << ")\n" ;

}

}

}

int master()

{

midPointCircleDraw(0, 0, three);

return 0;

}

C

#include<stdio.h>

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

{

int ten = r, y = 0;

printf ( "(%d, %d) " , ten + x_centre, y + y_centre);

if (r > 0)

{

printf ( "(%d, %d) " , x + x_centre, -y + y_centre);

printf ( "(%d, %d) " , y + x_centre, x + y_centre);

printf ( "(%d, %d)\n" , -y + x_centre, x + y_centre);

}

int P = ane - r;

while (x > y)

{

y++;

if (P <= 0)

P = P + two*y + 1;

else

{

x--;

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

}

if (x < y)

break ;

printf ( "(%d, %d) " , x + x_centre, y + y_centre);

printf ( "(%d, %d) " , -x + x_centre, y + y_centre);

printf ( "(%d, %d) " , 10 + x_centre, -y + y_centre);

printf ( "(%d, %d)\n" , -x + x_centre, -y + y_centre);

if (x != y)

{

printf ( "(%d, %d) " , y + x_centre, x + y_centre);

printf ( "(%d, %d) " , -y + x_centre, ten + y_centre);

printf ( "(%d, %d) " , y + x_centre, -10 + y_centre);

printf ( "(%d, %d)\northward" , -y + x_centre, -x + y_centre);

}

}

}

int main()

{

midPointCircleDraw(0, 0, 3);

return 0;

}

Java

form GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int 10 = r, y = 0 ;

System.out.print( "(" + (x + x_centre)

+ ", " + (y + y_centre) + ")" );

if (r > 0 ) {

System.out.print( "(" + (10 + x_centre)

+ ", " + (-y + y_centre) + ")" );

System.out.print( "(" + (y + x_centre)

+ ", " + (x + y_centre) + ")" );

System.out.println( "(" + (-y + x_centre)

+ ", " + (x + y_centre) + ")" );

}

int P = 1 - r;

while (x > y) {

y++;

if (P <= 0 )

P = P + 2 * y + i ;

else {

x--;

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

}

if (10 < y)

suspension ;

Organization.out.print( "(" + (x + x_centre)

+ ", " + (y + y_centre) + ")" );

System.out.print( "(" + (-x + x_centre)

+ ", " + (y + y_centre) + ")" );

Organization.out.impress( "(" + (x + x_centre) +

", " + (-y + y_centre) + ")" );

System.out.println( "(" + (-10 + x_centre)

+ ", " + (-y + y_centre) + ")" );

if (ten != y) {

System.out.impress( "(" + (y + x_centre)

+ ", " + (ten + y_centre) + ")" );

Arrangement.out.impress( "(" + (-y + x_centre)

+ ", " + (ten + y_centre) + ")" );

System.out.impress( "(" + (y + x_centre)

+ ", " + (-ten + y_centre) + ")" );

System.out.println( "(" + (-y + x_centre)

+ ", " + (-x + y_centre) + ")" );

}

}

}

public static void main(String[] args) {

midPointCircleDraw( 0 , 0 , iii );

}

}

Python3

def midPointCircleDraw(x_centre, y_centre, r):

x = r

y = 0

impress ( "(" , 10 + x_centre, ", " ,

y + y_centre, ")" ,

sep = " ", end = " ")

if (r > 0 ) :

print ( "(" , 10 + x_centre, ", " ,

- y + y_centre, ")" ,

sep = " ", end = " ")

print ( "(" , y + x_centre, ", " ,

x + y_centre, ")" ,

sep = " ", end = " ")

impress ( "(" , - y + x_centre, ", " ,

ten + y_centre, ")" , sep = "")

P = 1 - r

while x > y:

y + = 1

if P < = 0 :

P = P + 2 * y + 1

else :

ten - = one

P = P + 2 * y - 2 * x + one

if (x < y):

break

impress ( "(" , x + x_centre, ", " , y + y_centre,

")" , sep = " ", stop = " ")

print ( "(" , - ten + x_centre, ", " , y + y_centre,

")" , sep = " ", end = " ")

impress ( "(" , x + x_centre, ", " , - y + y_centre,

")" , sep = " ", end = " ")

impress ( "(" , - ten + x_centre, ", " , - y + y_centre,

")" , sep = "")

if x ! = y:

print ( "(" , y + x_centre, ", " , x + y_centre,

")" , sep = " ", stop = " ")

impress ( "(" , - y + x_centre, ", " , x + y_centre,

")" , sep = " ", end = " ")

print ( "(" , y + x_centre, ", " , - x + y_centre,

")" , sep = " ", end = " ")

print ( "(" , - y + x_centre, ", " , - x + y_centre,

")" , sep = "")

if __name__ = = '__main__' :

midPointCircleDraw( 0 , 0 , three )

C#

using System;

class GFG {

static void midPointCircleDraw( int x_centre,

int y_centre, int r)

{

int ten = r, y = 0;

Console.Write( "(" + (x + x_centre)

+ ", " + (y + y_centre) + ")" );

if (r > 0)

{

Console.Write( "(" + (ten + x_centre)

+ ", " + (-y + y_centre) + ")" );

Console.Write( "(" + (y + x_centre)

+ ", " + (10 + y_centre) + ")" );

Console.WriteLine( "(" + (-y + x_centre)

+ ", " + (x + y_centre) + ")" );

}

int P = 1 - r;

while (x > y)

{

y++;

if (P <= 0)

P = P + ii * y + ane;

else

{

ten--;

P = P + ii * y - 2 * 10 + 1;

}

if (x < y)

pause ;

Console.Write( "(" + (10 + x_centre)

+ ", " + (y + y_centre) + ")" );

Console.Write( "(" + (-10 + x_centre)

+ ", " + (y + y_centre) + ")" );

Console.Write( "(" + (10 + x_centre) +

", " + (-y + y_centre) + ")" );

Console.WriteLine( "(" + (-x + x_centre)

+ ", " + (-y + y_centre) + ")" );

if (x != y)

{

Console.Write( "(" + (y + x_centre)

+ ", " + (ten + y_centre) + ")" );

Panel.Write( "(" + (-y + x_centre)

+ ", " + (10 + y_centre) + ")" );

Panel.Write( "(" + (y + x_centre)

+ ", " + (-x + y_centre) + ")" );

Console.WriteLine( "(" + (-y + x_centre)

+ ", " + (-10 + y_centre) + ")" );

}

}

}

public static void Main()

{

midPointCircleDraw(0, 0, 3);

}

}

PHP

<?php

office midPointCircleDraw( $x_centre ,

$y_centre ,

$r )

{

$10 = $r ;

$y = 0;

repeat "(" , $x + $x_centre , "," , $y + $y_centre , ")" ;

if ( $r > 0)

{

echo "(" , $10 + $x_centre , "," , - $y + $y_centre , ")" ;

echo "(" , $y + $x_centre , "," , $ten + $y_centre , ")" ;

echo "(" ,- $y + $x_centre , "," , $10 + $y_centre , ")" , "\due north" ;

}

$P = 1 - $r ;

while ( $x > $y )

{

$y ++;

if ( $P <= 0)

$P = $P + 2 * $y + ane;

else

{

$x --;

$P = $P + ii * $y -

2 * $x + i;

}

if ( $x < $y )

interruption ;

echo "(" , $x + $x_centre , "," , $y + $y_centre , ")" ;

echo "(" ,- $x + $x_centre , "," , $y + $y_centre , ")" ;

repeat "(" , $x + $x_centre , "," , - $y + $y_centre , ")" ;

echo "(" ,- $10 + $x_centre , "," , - $y + $y_centre , ")" , "\due north" ;

if ( $ten != $y )

{

echo "(" , $y + $x_centre , "," , $10 + $y_centre , ")" ;

echo "(" ,- $y + $x_centre , "," , $x + $y_centre , ")" ;

echo "(" , $y + $x_centre , "," , - $x + $y_centre , ")" ;

repeat "(" ,- $y + $x_centre , "," , - $x + $y_centre , ")" , "\northward" ;

}

}

}

midPointCircleDraw(0, 0, 3);

?>

Javascript

<script>

role midPointCircleDraw(x_centre , y_centre , r) {

var x = r, y = 0;

certificate.write( "(" + (x + x_centre) + ", " + (y + y_centre) + ")" );

if (r > 0) {

document.write( "(" + (ten + x_centre) + ", " + (-y + y_centre) + ")" );

document.write( "(" + (y + x_centre) + ", " + (x + y_centre) + ")" );

document.write( "(" + (-y + x_centre) + ", " + (ten + y_centre) + ")<br/>" );

}

var P = one - r;

while (10 > y) {

y++;

if (P <= 0)

P = P + 2 * y + 1;

else {

x--;

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

}

if (x < y)

interruption ;

document.write( "(" + (x + x_centre) + ", " + (y + y_centre) + ")" );

document.write( "(" + (-10 + x_centre) + ", " + (y + y_centre) + ")" );

certificate.write( "(" + (x + x_centre) + ", " + (-y + y_centre) + ")" );

document.write( "(" + (-x + x_centre) + ", " + (-y + y_centre) + ")<br/>" );

if (x != y) {

certificate.write( "(" + (y + x_centre) + ", " + (10 + y_centre) + ")" );

document.write( "(" + (-y + x_centre) + ", " + (10 + y_centre) + ")" );

document.write( "(" + (y + x_centre) + ", " + (-x + y_centre) + ")" );

certificate.write( "(" + (-y + x_centre) + ", " + (-10 + y_centre) + ")<br/>" );

}

}

}

midPointCircleDraw(0, 0, 3);

</script>

Output:

(3, 0) (3, 0) (0, 3) (0, 3) (3, 1) (-3, 1) (three, -1) (-three, -ane) (1, 3) (-1, iii) (1, -3) (-1, -three) (ii, ii) (-ii, two) (ii, -ii) (-2, -ii)

Time Complication: O(ten – y)
Auxiliary Space: O(1)
References : Midpoint Circle Algorithm
Epitome References : Octants of a circle, Rasterised Circle, the other images were created for this article by the geek
Thanks Tuhina Singh and Teva Zanker for improving this article.
This article is contributed by Nabaneet Roy. If you like GeeksforGeeks and would like to contribute, y'all can also write an commodity using write.geeksforgeeks.org or mail your commodity to review-squad@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Delight write comments if you notice annihilation incorrect, or you desire to share more data about the topic discussed above.


doylehasheivates38.blogspot.com

Source: https://www.geeksforgeeks.org/mid-point-circle-drawing-algorithm/

0 Response to "Dda Circle Drawing Algorithm in Opengl"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel