/*
 * File: outro.c
 * ---------------
 * Este programa desenha outro fractal.
 */

#include <stdio.h>
#include <math.h>
#include "genlib.h"
#include "simpio.h"
#include "graphics.h"

/*
 * Function: DrawLineDirection
 * Usage: DrawLine(length, direction);
 * -----------------------------------
 * This procedure draws a line extending from the current
 * point by moving the pen length inches in the direction
 * given by the parameter direction, which is an angle.  
 * The final position becomes the new current point.
 */

void DrawLineDirection(double length, double direction);

/*
 * Function: outro
 * Usage: outro(length, direction, depth);
 * --------------------------------------
 * This procedure draws another fractal.  The parameter 
 * length defines the distance between the initial and
 * final points, and direction is the angle determined
 * by the line between these points and the x-axis.
 * The parameter depth gives the depth of the recursion
 * that is to be used.
 */

void outro(double length, double direction, int depth);

/* Main program */ 

main()
{
    double length;
    int depth;

    printf("Comprimento? ");
    length = GetReal();
    printf("Profundidade? ");
    depth = GetInteger();
    
    InitGraphics();

    MovePen(3*GetWindowWidth()/4, GetWindowHeight()/4);

    outro(length, PI/2, depth);
    outro(length, PI, depth);
    outro(length, 3*PI/2, depth);
    outro(length, 0, depth);
}

void DrawLineDirection(double length, double direction)
{
  double dx, dy;

  dx = length * cos(direction);
  dy = length * sin(direction);

  DrawLine(dx, dy);
}

void outro(double length, double direction, int depth)
{
  if (depth == 0) 
    DrawLineDirection(length, direction);
  else {
    double l = length/3;
    outro(l, direction, depth - 1);
    outro(l, direction + PI/2, depth -1);
    outro(l, direction, depth -1);
    outro(l, direction - PI/2, depth - 1);
    outro(l, direction, depth -1);
  }
}
