[Tutor] Using trigonometry to draw a circle

Joel Goldstick joel.goldstick at gmail.com
Tue May 18 04:12:10 EDT 2021


On Tue, May 18, 2021 at 3:23 AM Phil <phillor9 at gmail.com> wrote:
>
> While this question is not exactly Python related, I'm trying to
> understand how I can incorporate trig functions into Python code.
>
> Take for example a circle.
>
> radius = 60
> x_centre = 100
> y_centre = 100
>
> for angle in range(0,45):
>      x = math.cos(angle) * radius + x_centre
>      y = math.sin(angle) * radius + y_centre
>      plot(x, y)
>
> Increasing the steps in the range function adds more points to the
> circle, but still draws a full circle. How would I draw part of a circle
> by giving a function the starting and ending angles along the circumference?
>
> I spent most of the afternoon searching the Internet for an answer and,
> while I found many articles on unit circles, I couldn't find the answer
> I was looking for.
>
> --
> Regards,
> Phil
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

Note: Your code doesn't include the import of math, or the import of
where plot comes from.

More to the point (no pun intended), those math functions are defined
for radians, not degrees.  There are 2*pi radians in a circle, and 360
degrees.  So, you need to convert degrees to radians.

The code below prints radians from 0 to 90 degrees.

>>> for angle in range(91):
...     print(angle*2*math.pi/360)
...
0.0
0.017453292519943295
0.03490658503988659
...
...
...
1.53588974175501
1.5533430342749535
1.5707963267948966

-- 
Joel Goldstick


More information about the Tutor mailing list