Question

In the text book, the code can draw sin(x).

for x in range(-175,176):
       turtle.goto(x,50*math.sin((x/100)*2*math.pi))

I don't know why ? why sin(x) is 50*math.sin((x/100)*2*math.pi)?

Était-ce utile?

La solution

Work out the y-coordinates of the points that are being drawn:

50 * sin((-175/100) * 2π)
50 * sin((-174/100) * 2π)
50 * sin((-173/100) * 2π)
...
50 * sin((175/100) * 2π)
50 * sin((176/100) * 2π)

The 50 simply scales things up because sin values are between 0 and 1, so multiplying by 50 means the y-coordinates go from -50 to 50, enabling you to see the graph much better.

The arguments to the sin function are similarly scaled to go from -3.5π to 3.5π because the author of this script thought that would be a good range for the x-axis. And note the code is making 350 steps as it goes from left to right.

Bottom line is that this code was thought by its author to "look good".

Autres conseils

More Easy and Simplified Answer would be this.

import math
import turtle

wn = turtle.Screen()
wn.bgcolor('lightblue')
fred = turtle.Turtle()
sc = turtle.Screen()
sc.reset()

sc.setworldcoordinates(0,-1.5,360,1.5)

for angle in range(360):
    y = math.sin(math.radians(angle))
    fred.goto(angle,y)   

wn.exitonclick()

Here the sc.setworldcoordinates(0,-1.5,360,1.5)+ Divides the Scree into Co-ordinates Ranging from x : 0 , 360 and y : -1.5 , 1.5

The y = math.sin(math.radians(angle))Converts the angel to sin vlaues.

IMHO, that equation is used to convert degree (180) to radian (math.pi, 3.141592 ...).

BTW, if you use Python 2.7, replace x/100 with x/100.0. Otherwise you will get flat line instead of sin wave.

>>> 1/100
0
>>> 1/100.0
0.01

range() only handles integer values gracefully. So they chose to use integer values for the x axis and to map 100 units to a full sine period. The code displays 3 1/2 periods, from -1.75*2pi to +1.75*2pi.

Then the result is mapped to the range [-50..50], in order to have clean coordinates for y.

More Easy and Simplified Answer would be this.

I like what @HardCoder did with setworldcoordinates() but I don't see how calling turtle.Screen() twice and storing it under two different variables makes things simpler, nor why it's even necessary.

What makes all these implementations messy is we need to use radians for math.sin() but degrees to make the arguments to range() integers. Short of using scale factors or the floating range operator from numpy, one way to keep things simple, i.e. eliminating the degrees/radians conversion, is to dump range():

from turtle import Turtle, Screen
from math import pi, sin as sine

screen = Screen()
screen.setworldcoordinates(0, -1.25, 2 * pi, 1.25)

turtle = Turtle()

angle = 0

while angle < 2 * pi:
    turtle.goto(angle, sine(angle))
    angle += 0.1

screen.exitonclick()

make a list containing all of the x and y values as a tuple and then just set the position of the turtle :)

import math,turtle,threading

sine_line=[]
cos_line=[]
amp=100


for x in range(0,1000,10):
   sine_line.append((x/10,amp*math.sin(math.radians(x))))
   cos_line.append((x/10,amp*math.cos(math.radians(x))))

window=turtle.Screen()

myT=turtle.Turtle()
myT2=turtle.Turtle()

for x in range(0,len(sine_line)):
   myT2.setpos(cos_line[x][0],cos_line[x][1])
   myT.setpos(sine_line[x][0],sine_line[x][1])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top