Hello everyone,
I am implementing a cubic bezier curve for my project and I have to store the calculated control points in a file. I have to use the output file in gnuplot to view the curve. Following one of the posts here, I understood how to implement but I am confused on how to get my output to a file. When I tried it just writes the values of the last point it calculates. Since there is a loop so I should have the values being written in the file as soon as it generates it. Here is the code below:
import math
points = [(0,0), (5,0), (5,5), (10,5)]
n = 20
for i in range(n) :
u = i / float(n)
x = math.pow(1-u,3) * points[0][0] + 3 * u * math.pow(1-u,2) * points[1][0] \
+ 3 * (1-u) * math.pow(u,2) * points[2][0] + math.pow(u,3) * points[3][0]
y = math.pow(1-u,3) * points[0][1] + 3 * u * math.pow(1-u,2) * points[1][1] \
+ 3 * (1-u) * math.pow(u,2) * points[2][1] + math.pow(u,3) * points[3][1]
print "(x,y)=", (x, y)
Could someone help me please. Thank you.
f = open('somefile.dat', 'w+')opens (and creates) a file. Withf.write()you can write a string to the file. In you case, you have to substitute theprintcall with thewritecall: