polar coordinates?
Peter Otten
__peter__ at web.de
Sun Dec 9 15:07:21 EST 2018
Oscar Benjamin wrote:
> On Sun, 9 Dec 2018 at 16:37, Brian Christiansen
> <brian_christians at hotmail.com> wrote:
>>
>> I have been messing with a program that is inspried by a video on
>> youtube that is about the vizualization of pi. I might make a post
>> about that program someday, but I want to talk about something else.
>> One of the ways of visualizing it is to put dots corresponding to each
>> digits in a spiral pattern, in a color corresponding to what the digit
>> is. I think this would be easiest, at least in the initial calculation
>> of the point would be to use polar coordinates.
>>
>> For example, if I were to use a very simple archimedian spiral, r = 0 +
>> (1 x theta), the "first 4" points, if theta increases by 1 degree
>> (2pi/360 radians), are (0,0) (2pi/360 "units",2pi/360"radians")
>> (4pi/360, 4pi/360) (6pi/360,6pi/360).
>>
>> The problem is that python (more specifically tkinter or graphics.py
>> file that I downloaded) can't use polar coordinates directly to plot
>> points (or at least I don't think they can). The polar coordinates have
>> to be converted to cartesian coordinates, then converted to the
>> coordinate system that a computer uses to actually plot points on the
>> screen.
>
> Hi Brian,
>
> I don't think anything exists (apart from matplotlib) to do this for you:
> https://matplotlib.org/examples/pylab_examples/polar_demo.html
>
> Converting from polar to Cartesian coordinates is easy enough though.
> For your case if xc_p, yc_p are the pixel coordinates of the centre of
> your window and rq and thetaq are the polar coordinates for point q
> then
>
> from math import sin, cos
> xq_p = xc_p + r * cos(theta) * pix_scale
> yq_p = yc_p - r * sin(theta) * pix_scale
>
> gives the pixel coordinates for q. The parameter pix_scale is the
> number of pixels that corresponds to a distance of 1 in your polar
> coordinate system. You might choose this parameter based on the
> height/width in pixels of the window. Depending on what you're doing
> you may need to convert xq_p and yq_p to int rounding in some way.
Python has native support for complex numbers. With these:
>>> def angle(deg):
... return cmath.rect(1, math.radians(deg))
...
>>> def point(c):
... return (int(c.real), int(c.imag))
...
>>> center = 350 + 350j
>>> p = center + 350
>>> point(p)
(700, 350)
To rotate p around the center by 90 degrees:
>>> point((p-center) * angle(90) + center)
(350, 700)
Scaling can also be performed with a single multiplication
>>> scale = 2 + 2j
>>> p * 2
(1400+700j)
Another option is to let the Canvas widget do it:
http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.scale-method
More information about the Python-list
mailing list