PROGRAM but; { Berg und Tal }

CONST n = 50;

{$I graphics.inc}

VAR x, y, xp, yp, i : integer;
    min, max        : ARRAY [-100..739] OF integer;

{====================================================================}

FUNCTION z (x, y : integer) : integer;

VAR a, b : real;

BEGIN { z }
  a := (x-200)/50;
  b := (y-50)/25;
  z := round (25*sin (a*b)+30*sin (a+b))
END;  { z }

{====================================================================}

PROCEDURE draw;

BEGIN { draw }
  IF max [xp] = 0
    THEN
      BEGIN
        max [xp] := yp;
        min [xp] := yp
      END
    ELSE
      IF yp > max [xp]
        THEN
          max [xp] := yp
        ELSE
          IF (yp < min [xp]) AND (yp >= 0)
            THEN
              min [xp] := yp
            ELSE
              penup;
  moveto (xp, yp)
END;  { draw }

{====================================================================}

BEGIN { but }
  clrgbs;
  clrtbs;
  quad (0, 0, xmax, ymax);
  FOR i := 0 TO xmax DO
    BEGIN
      min [i] := 0;
      max [i] := 0
    END;
  FOR i := 0 TO n DO
    BEGIN
      write ('.');
      y := 100*i DIV n;
      FOR x := -100 TO xmax DO
        BEGIN
          IF x > -100
            THEN
              pendown
            ELSE
              penup;
          xp := x+y;
          yp := 50+y+z (x, y);
          draw
        END
    END;
  hardcopy2
END.  { but }
