Polar Coordinates

Polar coordinates are an alternative way to represent points in a two-dimensional plane. Unlike the familiar Cartesian coordinates (x, y), which use horizontal and vertical distances from the origin, polar coordinates use distance and angle measurements from a fixed point, known as the polar origin. In polar coordinates, a point is represented as (r, θ), where r is the radius and θ the angle between the positive x-axis and the line connecting the origin to the point, measured in a counterclockwise direction.


The parameters of the Algorithm

  • SCREEN: (int,int) - The width and height of the screen given in pixels
  • CIRCLE: Circle() - The basic unity of information in the program. A circle rendered in the center of the screen with a dot traveling around its circumference
  • ISFIRST: bool - Whether or not the program is rendereing its first frame
# MODULES
import pygame
import math

# DD
SCREEN = (800,800)
display = pygame.display.set_mode(SCREEN)

# DD. CIRCLE
# circle = Circle()
# interp. a circle rendered in the middle of the SCREEN
class Circle():
    def __init__(self,r):
        self.r = r 
        self.x = SCREEN[0]//2
        self.y = SCREEN[1]//2
        self.color = "white"
        # attr. related to a dot
        self.dotSize = 10
        self.angle = 0
        self.dx = 0
        self.dy = 0
        self.direction = 1
    
    def draw(self):
        self.getDotPos()
        # pygame.draw.circle(display,self.color,(self.x,self.y),self.r,3)
        pygame.draw.circle(display,'red',(self.x + self.dx,self.y + self.dy),self.dotSize)

    def getDotPos(self):
        self.dx = math.cos(self.angle) * self.r
        self.dy = math.sin(self.angle) * self.r
        self.angle += 0.01

circle0 = Circle(350)

# DD. FIRST
# isFirst = bool
# interp. whether or not this is the first frame
isFirst = True

The Algorithm

  • Calculate the x and y position of the dot relative to the circle given the CIRCLE radius and CIRCLE angle
  • Update angle

Let's implement this algorithm using Python!
# MODULES
import pygame
import math

# DD
SCREEN = (800,800)
display = pygame.display.set_mode(SCREEN)

# DD. CIRCLE
# circle = Circle()
# interp. a circle rendered in the middle of the SCREEN
class Circle():
    def __init__(self,r):
        self.r = r 
        self.x = SCREEN[0]//2
        self.y = SCREEN[1]//2
        self.color = "white"
        # attr. related to a dot
        self.dotSize = 10
        self.angle = 0
        self.dx = 0
        self.dy = 0
        self.direction = 1
    
    def draw(self):
        self.getDotPos()
        # pygame.draw.circle(display,self.color,(self.x,self.y),self.r,3)
        pygame.draw.circle(display,'red',(self.x + self.dx,self.y + self.dy),self.dotSize)

    def getDotPos(self):
        self.dx = math.cos(self.angle) * self.r
        self.dy = math.sin(self.angle) * self.r
        self.angle += 0.01

circle0 = Circle(350)

# DD. FIRST
# isFirst = bool
# interp. whether or not this is the first frame
isFirst = True


# CODE

def draw():
    global isFirst
    if isFirst:
        display.fill("#1e1e1e")
        isFirst = False
    circle0.draw()
    pygame.display.flip()

def update():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
    
    if circle0.r <=0:
        circle0.direction = 1
    if circle0.r >=350:
        circle0.direction = -1
    circle0.r += 0.1 * circle0.direction

while True:
    draw()
    update()