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