简体   繁体   中英

finding the x and y coordinates for every angle

I am trying to make some code that finds the coordinates of a satellite in orbit, but I am currently trying to do it for just a circle where you ignore gravity etc. and you are already given the radius. I have already tried this code but the loop wont work please help

import math

r = 5

angle = 0

while angle <= 360:

    angle = math.radians(angle)

    x_coord = math.sin(angle)*r

    y_coord = math.cos(angle)*r
    print ("Position [",x_coord,y_coord,"]")
    angle +=1

When the line angle = math.radians(angle) executes, this is replacing the value of the same angle variable that's being checked on the previous line - thus, since angle will never be higher than 360, the loop will never finish.

My recommendation is to change the name of the variable when you convert it to radians, so that you're not overwriting its value - such as like this: angle_radians = math.radians(angle) . Thus, you'd also have to change your sin and cos to use the new variable as well.

First, as pointed out by @Random, you need to use a different variable name for angle = math.radians(angle) . For example, angle_radians

Second, when you use print , you need to convert x_coord and y_coord to string: str(x_coord) and str(y_coord)

Try this:

import math

r = 5

angle = 0

while angle <= 360:

    angle_radians = math.radians(angle)

    x_coord = math.sin(angle_radians)*r

    y_coord = math.cos(angle_radians)*r
    print ("Position [",str(x_coord),str(y_coord),"]")
    angle +=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM