'''This script produces Figure 14.10, the clothoid.
This file requires the scipy package in order to compute the integrals.
If you don't want to install scipy, remove line 6 and read the comments'''

import numpy as np
from scipy.integrate import quadrature
import matplotlib.pyplot as plt

a, b = 0, 3./2*np.pi
dt = 1e-2
t = np.arange(a, b, dt) 
N = len(t)

x, y = np.zeros(N), np.zeros(N)

def fcn1(x, c=1):
   return np.cos(c*x**2)

def fcn2(x, c=1):
   return np.sin(c*x**2)   

'''Uncomment the following three lines if you don't have scipy installed'''
#for k in range(N):
#    x[k] = np.trapz(fcn1(t[:k]), dx = dt)
#    y[k] = np.trapz(fcn2(t[:k]), dx = dt)

'''Comment the following three lines if you don't have scipy installed'''
for k in range(N):
    x[k] = quadrature(fcn1, 0, t[k])[0]
    y[k] = quadrature(fcn2, 0, t[k])[0]

plt.plot([-1.2, 1.2], [0, 0], 'k', [0, 0], [-1.2, 1.2], 'k')
plt.plot(1.2, 0, 'k>', 0, 1.2,'k^')
plt.text(1.35, 0, 'x')
plt.text(0, 1.35, 'y')

plt.plot(x,y,'b',-x,-y,'b')
plt.axis('equal')
plt.show()