0

If a sprite is in the centre of a circle at the point 250,250 in pygame what is the equation to find the edge of the circle in any direction relative to the original point. Could the equation have the angle (as X) in the equation?

Rabbid76
  • 142,694
  • 23
  • 71
  • 112
  • 5
    Have you tried typing the "equation of circle" into a search engine? What happened? What did you not understand about the formula you found? – Useless Apr 21 '21 at 14:23
  • This is straightforward trigonometry. See for example https://courses.lumenlearning.com/boundless-algebra/chapter/trigonometric-functions-and-the-unit-circle/ – Jon Kiparsky Apr 21 '21 at 14:24
  • As we learn in high school geometry, the equation is `x^2 + y^2=r^2`. We also lean how to calculate this with angles and offsets – Panagiotis Kanavos Apr 21 '21 at 14:24
  • `(x - 250)^2 + (y - 250)^2 = radius ^ 2` – Albin Paul Apr 21 '21 at 14:25
  • 2
    `x = r * cos(theta) + 250, y = r * sin(theta) + 250`. Are these what you are looking for? – Albin Paul Apr 21 '21 at 14:27
  • @PanagiotisKanavos that equation defines the set of points on the circumference of the circle, but it does not help the OP find a point on the circle at a given angle. – Jon Kiparsky Apr 21 '21 at 14:27
  • @Rabbid76 thanks but (a potentially stupid question) what do the new x and y equal. – Jacob Boughton Apr 21 '21 at 15:01

1 Answers1

3

The general formula is (x, y) = (cx + r * cos(a), cy + r * sin(a)).

However, in your case° is at the top a nd the angel increase clockwise. Therefore the formual is:

angle_rad = math.radians(angle)
pt_x = cpt[0] + radius * math.sin(angle_rad)
pt_y = cpt[1] - radius * math.cos(angle_rad)  

Alternatively you can use the pygame.math module and pygame.math.Vector2.rotate:

vec = pygame.math.Vector2(0, -radius).rotate(angle)
pt_x, pt_y = cpt[0] + vec.x, cpt[0] + vec.y

Minimal example:

import pygame
import math

pygame.init()
window = pygame.display.set_mode((500, 500))
font = pygame.font.SysFont(None, 40)
clock = pygame.time.Clock()
cpt = window.get_rect().center
angle = 0
radius = 100

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False  

    # solution 1
    #angle_rad = math.radians(angle)
    #pt_x = cpt[0] + radius * math.sin(angle_rad)
    #pt_y = cpt[1] - radius * math.cos(angle_rad)    
    
    # solution 2
    vec = pygame.math.Vector2(0, -radius).rotate(angle)
    pt_x, pt_y = cpt[0] + vec.x, cpt[0] + vec.y
    
    angle += 1     
    if angle >= 360:
        angle = 0

    window.fill((255, 255, 255))
    pygame.draw.circle(window, (0, 0, 0), cpt, radius, 2)
    pygame.draw.line(window, (0, 0, 255), cpt, (pt_x, pt_y), 2)
    pygame.draw.line(window, (0, 0, 255), cpt, (cpt[0], cpt[1]-radius), 2)
    text = font.render(str(angle), True, (255, 0, 0))
    window.blit(text, text.get_rect(center = cpt))
    pygame.display.flip()

pygame.quit()
exit()
Rabbid76
  • 142,694
  • 23
  • 71
  • 112
  • 1
    @JacobBoughton This is just the math how the point (x, y) is computed, it is not a python expression. The code would be `x = cx + r * math.cos(a)`, `y = cy + r * math.sin(a)` – Rabbid76 Apr 21 '21 at 15:25