Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

plotting n number of equal points in circular direction in python

I am working on the task in which I have to make a circle which is having n number of equal parts. I am provided with centre and radius of the circle which is (0,0) and 4 respectively. To achieve this task, I have written below code,

parts = 36                     # desire number of parts of the circle          
theta_zero = 360/parts
R = 4                          # Radius

x_p = []
y_p = []

n = 0
for n in range(0,36):
    x_p.append(R * math.cos(n*theta_zero))
    y_p.append(R * math.sin(n*theta_zero))

However, after running this code, I got output like below which does not seem a coorect which I am suppose to have.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Kindly let me know what I am doing wrong and give some suggestion of correct code. Thank you

>Solution :

Your circle is weird because math.cos and math.sin accept radians while you are passing degrees. You just need to convert the degrees to radians when calling the math functions.

Like this:

parts = 36         
theta_zero = 360/parts
R = 4 

x_p = []
y_p = []

for n in range(0,36):
    x_p.append(R * math.cos(n*theta_zero /180*math.pi))
    y_p.append(R * math.sin(n*theta_zero /180*math.pi))

Result:

Circle

Alternatively changing theta_zero to 2*math.pi/parts would also work and would be slightly faster but it might be a little less intuitive to work with.

Also as @Mad Physicist mentioned you should probably add plt.axis('equal') to unstretch the image.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading