class Terrain() :
    L = 350.0

    def __init__(self, sd, vit_vent=5.0, hmax=10.0) :
        import numpy as np
        from random import Random
        rd = Random(sd)
        self.vit_vent = vit_vent
        self.hmax = hmax
        self.A = [ 10*(rd.random()-0.5)*np.exp(-i/20) for i in range(20) ]
        self.B = [ 10*(rd.random()-0.5)*np.exp(-i/20) for i in range(20) ]

    def h(self, x) :
        import numpy as np
        return sum(ai*np.sin(np.pi*i*x/self.L) for i, ai in enumerate(self.A)) + sum(bi*(1-np.cos(np.pi*i*x/self.L)) for i, bi in enumerate(self.B))

    def vent(self, x, y) :
        import numpy as np
        dx = 1.0
        alt1 = max(0.0, y-self.h(x))
        alt2 = max(0.0, y-self.h(x+dx))
        dy = np.exp(-alt1/self.hmax)*(alt1-alt2)
        n = (dx*dx+dy*dy)**0.5
        v = self.vit_vent*(1-np.exp(-alt1/self.hmax))
        return v*dx/n, v*dy/n

    def trace(self) :
        import numpy as np
        import matplotlib.pyplot as plt
        Xt = np.linspace(0, self.L, 1000)
        Yt = [ self.H(x) for x in Xt ]
        Xg, Yg = np.meshgrid(np.linspace(0, self.L), np.linspace(-10, 130))
        Vg = np.vectorize(self.vent)(Xg, Yg)
        plt.quiver(Xg, Yg, Vg[0], Vg[1], scale=200, width=0.001, color="c")
        plt.fill_between(Xt, [-15.0]*len(Yt), Yt, color='g')
        plt.xlim(0, self.L)
        plt.ylim(-15, 130)


# Création du terrain

terrain = Terrain(42, vit_vent=5, hmax=10)