Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Infinite While Loop inside a For Loop

I am trying to implement the Mid-Point Circle Drawing Algorithm on OpenGL and PyCharm. My aim is to draw eight circles inside a bigger circle. The eight inner circles each belong to a zone inside the bigger circle. An illustration of the figure I am trying to generate is shown in the image.

In my code, I have used one function to draw the bigger circle and a separate function to draw the eight inner circles using the Mid-Point Circle Drawing Algorithm. In the function for the eight inner circles, I have taken a list of the four zones (for now) and used a for loop to traverse the list. For each iteration, the mid-point circle algorithm will be executed and generate a circle for that respective zone. But my program is showing only one inner circle after running it. My while-loop is running infinitely, hence, the outer for loop is stuck in its first iteration. How can I address this and make my code work?

**NOTE: For simplicity, I am trying to make it work for four zones now. Hence, the length of my list is four for now. **

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

def circle_points(x, y, centre_x, centre_y,):

    glVertex2f(x + centre_x, y + centre_y)
    glVertex2f(x + centre_x, -y + centre_y)
    glVertex2f(-x + centre_x, y + centre_y)
    glVertex2f(-x + centre_x, -y + centre_y)
    glVertex2f(y + centre_x, x + centre_y)
    glVertex2f(y + centre_x, -x + centre_y)
    glVertex2f(-y + centre_x, -x + centre_y)
    glVertex2f(-y + centre_x, x + centre_y)



# function for bigger circle
def draw_circle(r):
    glPointSize(2)
    glBegin(GL_POINTS)
    glColor3f(1.0,1.0,1.0)

    x = 0
    y = r
    d = 1 - r  #decision parameter
    centre_x, centre_y = 250, 250  #taking centre of the circle


    while x <= y:
        circle_points(x, y, centre_x, centre_y)
        if d >= 0:
            d = d + 2*(x-y)+1
            x = x+1
            y = y-1
        else:
            d = d + 2*x+1
            x = x + 1



    glEnd()

# function for inner circles
def draw_inner_circles(r):
    glPointSize(2)
    glBegin(GL_POINTS)
    glColor3f(1.0, 1.0, 1.0)

    x = 0
    y = r
    d = 1 - r  # decision parameter



    zones = ['1','2','3','4']
    for i in zones:
        centre_x, centre_y = 250, 250
        if i == '1':
            centre_x = centre_x
            centre_y = centre_y + r
        elif i == '2':
            centre_x = centre_x
            centre_y = centre_y - r
        elif i == '3':
            centre_x = centre_x + r
            centre_y = centre_y

        elif i == '4':
            center_x = centre_x - r
            centre_y = centre_y
        while x <= y:
            circle_points(x, y, centre_x, centre_y)

            if d >= 0:
                d = d + 2 * (x - y) + 1
                x = x + 1
                y = y - 1
            else:
                d = d + 2 * x + 1
                x = x + 1



    glEnd()












def iterate():
    glViewport(0, 0, 500, 500)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()


def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(1.0, 1.0, 1.0)
    # funtion call 
    draw_circle(200)
    draw_inner_circles(100)

    glutSwapBuffers()






glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(500, 500) #window size
glutInitWindowPosition(0, 0)
wind = glutCreateWindow(b"OpenGL Coding Practice") #window name
glutDisplayFunc(showScreen)


glutMainLoop()

>Solution :

Are you sure the while loop is running infinitely? It seems like when the first inner circle is drawn, thus x > y, you never reset the x and y, so it never gets to run the while loop for the other iterations. Try with something like this:

    zones = ['1','2','3','4']
    for i in zones:
        centre_x, centre_y = 250, 250
        if i == '1':
            centre_x = centre_x
            centre_y = centre_y + r
        elif i == '2':
            centre_x = centre_x
            centre_y = centre_y - r
        elif i == '3':
            centre_x = centre_x + r
            centre_y = centre_y

        elif i == '4':
            center_x = centre_x - r
            centre_y = centre_y
       
        x = 0
        y = r
        d = 1 - r  # decision parameter
        while x <= y:
            circle_points(x, y, centre_x, centre_y)
            if d >= 0:
                d = d + 2 * (x - y) + 1
                x = x + 1
                y = y - 1
            else:
                d = d + 2 * x + 1
                x = x + 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading