Skip to content

Layman's Magazine

Everyday's tech for the everyday's layman

Menu
  • Home
  • News
  • Best picks
  • How To
  • Reviews
  • Other
    • Smart Home
    • Smartphones & tablets
    • Computers
    • Gadgets
    • Streaming
    • Gaming
    • Web
    • Software
    • Hardware
    • Cybersecurity
    • VR
    • Design
    • Programming
    • AI
    • Database
    • Network
    • Audio
    • Video
    • Crypto
    • Other technologies
  • Privacy Policy
Menu

How to draw Arcs in Pygame without gaps

Posted on August 20, 2022

Pygame, the python game development library, gives the possibility to draw multiple geometrical shapes using its sub module draw: rectangles, polygons, circles, ellipses, lines and arcs… However, the arc drawing function is presenting some bugs and pygame is not showing any signs of fixing it. When drawing an arc, the function seem to generate some gaps in the drawn shape. In this short tutorial, we propose a fast solution to draw Arcs in Pygame without gaps.

Using built-in function (With gaps):

This code below draws an Arc between 30° and 160° with a thickness of 40:

Copy CodeCopiedUse a different Browser
################# IMPORTING/INITIALIZING LIBRARIES  
import pygame, pygame.gfxdraw
import sys, numpy, math
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()

################# SCREEN SETTING
pygame.display.set_caption('Arc tutorial')
screen = pygame.display.set_mode((500,500), pygame.RESIZABLE)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.draw.arc(screen,(50,50,255),pygame.Rect(0,0,500,500),30*math.pi/180,160*math.pi/180,width=40)
    pygame.display.update()
    mainClock.tick(60)

As we can see in the resulting image, the shape seems to have some gaps. This is due to the fact that the function draws with a thickness of 40, as 40 arcs with a thickness of 1. This phenomenon is also called aliasing.

To solve this we propose the custom function below:

Using custom function (Without gaps):

This function uses the filled_polygon function to draw the arc, it creates the points enveloping the arc then fills it with the passed color.

Copy CodeCopiedUse a different Browser
def draw_arc(surface, x, y, r, th, start, stop, color):
    points_outer = []
    points_inner = []
    n = 300
    for i in range(n):
        delta = i/n
        phi = (start + (stop-start)*delta)*math.pi/180
        x0 = round(x+r*math.cos(phi))
        y0 = round(y+r*math.sin(phi))
        points_outer.append([x0,y0])
        x1 = round(x+(r-th)*math.cos(phi))
        y1 = round(y+(r-th)*math.sin(phi))
        points_inner.append([x1,y1])
    points = numpy.append(points_inner , numpy.flip(points_outer,0), axis=0)
    pygame.gfxdraw.filled_polygon(surface, points, color)

We use this function in the context of a simple game:

Copy CodeCopiedUse a different Browser
################# IMPORTING/INITIALIZING LIBRARIES  
import pygame, pygame.gfxdraw
import sys, numpy, math
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()

################# SCREEN SETTING
pygame.display.set_caption('Arc tutorial')
screen = pygame.display.set_mode((500,500), pygame.RESIZABLE)

################# FUNCTION

def draw_arc(surface, x, y, r, th, start, stop, color):
    points_outer = []
    points_inner = []
    n = 300
    for i in range(n):
        delta = i/n
        phi = math.pi/180 - (start + (stop-start)*delta)*math.pi/180
        x0 = round(x+r*math.cos(phi))
        y0 = round(y+r*math.sin(phi))
        points_outer.append([x0,y0])
        x1 = round(x+(r-th)*math.cos(phi))
        y1 = round(y+(r-th)*math.sin(phi))
        points_inner.append([x1,y1])
    points = numpy.append(points_inner , numpy.flip(points_outer,0), axis=0)
    pygame.gfxdraw.filled_polygon(surface, points, color)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    draw_arc(screen, 250, 250, 250, 40, 30, 160, (50,50,255))
    pygame.display.update()
    mainClock.tick(60)

The implemented function gives up the possibility to draw an arc without gaps:

Tags

academics ai android apple artificial intelligence comet crypto design elon musk ESA gaming google google scholar intel interceptor iphone Java keywords linux machine learning mysql NASA overleaf pattern pico H pico W pico WH plagiarism checker python R raspberry Pi reasearchgate research reverso robots search engine optimization SEO spacex spam telescope tesla testing twitter whatsapp youtube

©2023 Layman's Magazine | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage vendors Read more about these purposes
View preferences
{title} {title} {title}