python math problem

K

Kene Meniru

I am trying to calculate the coordinates at the end of a line. The length
and angle of the line are given and I am using the following formula:

x = (math.sin(math.radians(angle)) * length)
y = (math.cos(math.radians(angle)) * length)

The following are sample answers in the format (x, y) to the given
length/angle values of the line:

120/0 = (0.0, 25.0)
120/89 = (24.9961923789, 0.436310160932)
120/90 = (25.0, 1.53075794228e-15)
120/91 = (24.9961923789, -0.436310160932)

Why am I getting a string number instead of the expected answer for 120/90
which should be (25.0, 0.0). This happens at all multiples of 90 (i.e. 180
and 270)
 
J

John Machin

x = (math.sin(math.radians(angle)) * length)
y = (math.cos(math.radians(angle)) * length)

A suggestion about coding style:

from math import sin, cos, radians # etc etc
x = sin(radians(angle)) * length
y = cos(radians(angle)) * length

.... easier to write, easier to read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top