pythonpygame

How to draw a 1/4 of a circle in Pygame?


I'm using Python 3.7.4 and Pygame 1.9.6. I know about the pygame.draw.arc and pygame.draw.circle. I want to draw a quarter circle like here: enter image description here

It's the quarter circle of the soccer field.

My attempts:

pygame.draw.arc(screen, (255, 255, 255), (75, 25, 50, 50), 90, 120, 5)

enter image description here

pygame.draw.arc(screen, (255, 255, 255), (75, 25, 50, 50), 80, 100, 5)

Changing the starting angle and ending angle doesn't change a thing it seems.

enter image description here

So it must be the coordinates of drawing the boundaries. When I change the 75 in this (75, 25, 50, 50) it just makes the circle go to the right more. If the change the 25 in (75, 25, 50, 50) it makes it go up or down more. If I change the first 50 in (75, 25, 50, 50) it makes it wider width bigger or smaller depending on changing the numbers. If I change the second 50 in (75, 25, 50, 50) it makes the height bigger or smaller. I've just tried decimals in the angles, because the coordinates have to be integers. When I do pygame.draw.arc(screen, (255, 255, 255), (75, 25, 100, 50), 95.5, 100.5, 5) look at the decimals.

I get:

enter image description here

So maybe the key it changing the starting and ending angles I'll keep updating what I find. I found the right one with help from @bashBedlam in the comments:

pygame.draw.arc(screen, (255, 255, 255), (60, 13, 50, 50), 17, 13, 5)

enter image description here

I changed the coordinates but keep the same starting and ending angle. Which is pretty much perfect though I do hate the little black dots though. The screen is 600 x 600 I can't get to right angle or the circle itself. I'm not allowed to use decimals. Only allows integers. So I don't know how to properly make a quarter circle. I appreciate the advice and thanks.

I just wanted to say for people helping me I've created my own soccer field. I drew all the lines out, but then I just turned it into a png so it wouldn't lag me out of loading all 27 lines with the background constantly. Here is the finished result with everyone's help: enter image description here


Solution

  • The issue with your code is that pygame.draw.arc takes angles in radians as stated in the documentation. Also angles between 90 and 120 would not draw and arc like you need, 90 to -90 will.

    import pygame
    import math
    pygame.init()
    
    d = pygame.display.set_mode((1200, 600))
    
    while True:
        pygame.event.get()
        d.fill((255, 255, 255))
    
        pygame.draw.arc(d, (0, 0, 0), [900, 300, 100, 100], math.radians(90), math.radians(-90), 5)
        pygame.draw.arc(d, (0, 0, 0), [300, 300, 100, 100], math.radians(-90), math.radians(90), 5)
    
        pygame.display.update()
    

    Edit:

    So i was looking online for ways for pygame.draw.arc to draw arc without dots without using floating point numbers, but couldn't any, so i wrote this function for you. If you consider this function as not being a part of the code you wrote (like pygame.draw.arc) then technically, you are not using decimal because the decimal is only used inside of the function(finessed the system :)). Here's the function:

    def drawArc(display, startAngle, endAngle, distance, pos, color, thickness=1):
        if startAngle > endAngle:
            theta = endAngle
            bigger = startAngle
        else:
            theta = startAngle
            bigger = endAngle
            
        while theta < bigger: 
            for t in range(thickness):
                x = round((cos(radians(theta)) * (distance-t)) + pos[0])
                y = round((-sin(radians(theta)) * (distance-t)) + pos[1])
                display.set_at((x, y), color)
                theta += 0.01
    

    Think of this function like a compass, it draws an arc in-between the angle you specify (in degrees). Argument pos being the centre of the compass and distance being the distance of the arc from the centre. So now just draw the quarter circles in 4 different corners.

    import pygame
    from math import radians, sin, cos
    pygame.init()
    
    d = pygame.display.set_mode((1200, 600))
    
    def drawArc(display, startAngle, endAngle, distance, pos, color, thickness=1):
        if startAngle > endAngle:
            theta = endAngle
            bigger = startAngle
        else:
            theta = startAngle
            bigger = endAngle
            
        while theta < bigger: 
            for t in range(thickness):
                x = round((cos(radians(theta)) * (distance-t)) + pos[0])
                y = round((-sin(radians(theta)) * (distance-t)) + pos[1])
                display.set_at((x, y), color)
                theta += 0.01
        
            
    while True:
        pygame.event.get()
        d.fill((255, 255, 255))
    
        drawArc(d, -90, 0, 100, [0, 0], (0, 0, 0), thickness=5)
        drawArc(d, 180, 270, 100, [1200, 0], (0, 0, 0), thickness=5)
        drawArc(d, 0, 90, 100, [0, 600], (0, 0, 0), thickness=5)
        drawArc(d, 180, 90, 100, [1200, 600], (0, 0, 0), thickness=5)
        
        pygame.display.update() 
    

    Btw, using this function hinders performance, so i strongly recommend using pygame.draw.arc with floating point numbers if possible.