def main():
f = sin(x)
a = 0
b = 2 * pi
N = 1000
# consider N points in the interval [a, b]
X = [a + (b - a) / N * k for k in range(N + 1)]
Y = [f(x=u) for u in X]
ll = 3
# length of lines perpendicular to the curve
lls = 0.3
# smaller subsegments
thin_line = 2
thick_line = 4
# will draw lines perpendicular to the graph of Y=f(X) at
# points separted by length of 'spacing'
spacing = 0.015
M = floor(spacing * N)
# colors
red = [0.867, 0.06, 0.14]
blue = list(vector([0, 129, 205]) / QQ(256))
green = list(vector([0, 200, 70]) / QQ(256))
G = Graphics()
# plot the lines
for k in range(1, N + 1):
p = (k - 1) * M + 2
if p >= N:
break
# the normal to the curve at (X(p), Y(p))
Normal = vector([-(Y[p + 1] - Y[p - 1]), X[p + 1] - X[p - 1]])
Normal = Normal / norm(Normal)
G += line([(X[p] - lls * Normal[0], Y[p] - lls * Normal[1]),
(X[p] + lls * Normal[0], Y[p] + lls * Normal[1])],
color=red,
thickness=0.7 * thick_line)
# plot the curve
G += line2d(zip(X, Y), thickness=thick_line, color=blue)
G.axes(False)
G.set_aspect_ratio(1)
return G