Reputation: 63
How do I find a user defined amount of points on a circumference of a circle. Then when it has been found, place it in a 2 dimensional array for later use. For a slightly better view of it:
Thanks for all the help :)
Upvotes: 3
Views: 3859
Reputation: 20438
You could also use pygame's Vector2
class. Just rotate a vector with the length of the radius and add it to the center of the circle to get a point on the circumference.
import pygame as pg
from pygame.math import Vector2
def points(number, center, radius):
angle = 360/number
point_list = []
for i in range(number):
# Create a vector with the length of the radius, rotate it
# and add it to the center point to get a point on the circumference.
vec = center + Vector2(radius, 0).rotate(i*angle)
# pygame.draw.circle needs ints, so we have to convert the vector.
point_list.append([int(vec.x), int(vec.y)])
return point_list
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
center = (320, 240)
radius = 150
number = 2
point_list = points(number, center, radius)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_c:
number += 1 # Increment the number and generate new points.
point_list = points(number, center, radius)
screen.fill((30, 30, 30))
pg.draw.circle(screen, (220, 120, 0), center, radius, 2)
for pos in point_list:
pg.draw.circle(screen, (0, 120, 250), pos, 20, 2)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
Upvotes: 1
Reputation: 637
Im assuming you want the points evenly spaced.
A circle has 360 degrees about the center, or 2pi radians.
you need to divide 2pi by the number of points that you want. say 4 -> 2pi/4
that is the number of radians from one point to the next.
to compute x and y coordinates use these two equations r = sqrt( x2 + y2 ), and θ = tan-1 ( y / x )
where θ1 = 0*2*pi/4, θ2 = 1*2*pi/4, θ3 = 2*2*pi/4, and θ4 = 3*2*pi/4
some code would look like this:
import numpy as np
def get_points(radius, number_of_points):
radians_between_each_point = 2*np.pi/number_of_points
list_of_points = []
for p in range(0, number_of_points):
list_of_points.append( (radius*np.cos(p*radians_between_each_point),radius*np.sin(p*radians_between_each_point)) )
return list_of_points
Upvotes: 4
Reputation: 4444
You want to find points (x,y) which are points which are solutions to the equation x^2 + y^2 = R^2.
First note that both x and y must be in [-R,R], and when you pick x (or y), the other can be found by solving y = sqrt(R^2 - x^2).
An alternative is to use the angle, theta, and x = R * cos(theta), y = R * sin(theta). You need only solve for theta in [0,2*PI) radians (sin, cos typically use radians, PI radians is 180 degrees). Which depends upon whether you want to use trignometry (sin/cos are transcedental functions).
You can translate these points (x,y) to a new center (xc,yc) simply by adding (x1,y1) to (x,y) to get translated points (xt,yt) = (x+xc,y+yc).
Upvotes: 0