On Sun, 01 Jun 2014 05:17:07 -0700, Farzad Torabi wrote: > Hi Experts > > I am trying to draw a sine curve in Python , well first I had a script > that i could draw a function curve in this way : > > xMax = 25.0 > points = [] > for i in range(100): > x = (float(i)/99)*xMax > y = math.sqrt(x) > points.append([x,y]) > > s.Spline(points=points)
What is s? Where does it come from? > first i have questions that : what does the line x = (float(i)/99)*xMax > do ? why do we multiply it by In older versions of Python, division / with integer arguments does integer division, like C, instead of calculator division. For example: 1/2 => returns 0 instead of 1/2 returning 0.5 like a calculator does. In these older versions of Python, you can fix that by converting one of the arguments to a float first: 1/2.0 => 0.5 So "float(i)/99" converts the loop index i to a float, then divides by 99. An easier way to get the same result is "i/99.0". Then, multiplying by xMax simply scales the result to be between 0 and xMax, in this case 25.0. Look at the results: when i = 0, x = 0/99.0*25 = 0.0 when i = 99, x = 99/99.0*25.9 = 25.0 every other value of i gives a corresponding value between 0 and 25. > and then when I wanted to draw a sine curve I found this one : > > import math > > for angle in range(????): > y = math.sin(math.radians(angle)) > print(y) > > first , here instead of ???? can we put 2*pi ? No. The Python built-in range() function only accepts integer values. It is quite tricky to *accurately* produce floating point ranges. While it is easy to make a floating point range, it is much harder to make it accurate. You can see some discussion about the problem, and some sample code, here: http://code.activestate.com/recipes/577068 http://code.activestate.com/recipes/577878 http://code.activestate.com/recipes/577881 > second i wanted to try this method instead: > > xMax = pi > Lamda = 200 > points = [] > for i in range(Lamda): > x = (float(i)/99)*xMax > y = math.sin(x) > points.append([x,y]) > > it actually works much better and creates an actual sine curve but the > lengths are not really what i want , also if i want to draw a straight > line I use this command : > > xMax = 1 > Lamda = 200 > points = [] > for i in range(Lamda): > x = (float(i)/99) > y = xMax > points.append([x,y]) In this example, you are calculating points from a straight line. x varies from 0.0 to 2.0202 in steps of 1/99, and y is always the same value, 1. > but then the problem will be that I can not control the length of this > line and the sine curve , that should be equal You have to get the maths right, otherwise the graph will be wrong. -- Steven D'Aprano http://import-that.dreamwidth.org/ -- https://mail.python.org/mailman/listinfo/python-list