tgmjack
tgmjack

Reputation: 42

tryng to create a circle of co-ordinates in python

im trying to create points with an x and y co-ordinate. i want these points to form a circle. my genius plan was to set the radius of the circle then by using the fact the x^2 + Y^2 = R^2 i set a range of x's and set y to be y= root(r^2-x^2)
but ive realised that hasn't made a circle. the points in z make a quarter of circle because everything is positive. i don't know how to fix this i think i just need to do this 4 times. however i do only want 100 values for my full circle

import random 
import numpy as np
import math
import numpy as np
N=100
r=10     #radius
xcharge_corord = np.linspace(0,10,N)  # a bunch of possible x's
for i in range(len(xcharge_corord)):
    ycharge_corord = np.sqrt(r**2-xcharge_corord**2)   #y=root(r-x)

print(ycharge_corord)
Z = []                                   #z=[(x,y)(x,y)(x,y)(x,y)]
for i in range(len(xcharge_corord)):
    Z.append((xcharge_corord[i], ycharge_corord[i]))
print(Z)

Upvotes: 1

Views: 117

Answers (2)

ozzyzig
ozzyzig

Reputation: 719

This is the simple polar coordinate version:

import math
n = 10
i = 0
while i < 360:
    x = n * math.cos(math.radians(i))
    y = n * math.sin(math.radians(i))
    print x, y
    i += 10 #whatever step angle

NB: you will have to watch floating point issues for zero so maybe round to 4 or so decimal places.

Upvotes: 1

Srini
Srini

Reputation: 1639

What you have

x and y in the first quadrant. You need to just multiply the x and y co-ordinates by -1 appropriately to reflect this quarter circle into the other quadrants to get those values.

But IMO the best way would be to use polar co-ordinates

What you can do

Consider this (sorry I don't have numpy)

import random
import math

r = 5
yc = []
xc = range(0,6)

for x in xc:
    yc.append(int(math.sqrt(r**2 - x**2)))
print yc

def neg_one(a):
    return -a

z1 = zip(xc,yc)
z2 = zip(xc, map(neg_one, yc))
z3 = zip(map(neg_one, xc), map(neg_one, yc))
z4 = zip(map(neg_one, xc), yc)

print z1
print z2
print z3
print z4

Now this might have 4 times as many points as you wanted. So to fix this do

z_f = z1 + z2 + z3 + z4
print z_f

desired_value_points = 10
chunk_size = len(z_f) / desired_value_points

print "Chunk size: " + str(chunk_size)
print z_f[::chunk_size]

Will generate the desired number of points spread across the circle

Upvotes: 0

Related Questions