/* TreeCoord.m   Copyright 1992  Steve Ludtke  All Rights Reserved */
/* This object will distribute the branches as "Trees" All branches*/
/* are at a 45 degree angle to the z (up/down) axis. Branches      */
/* become shorter as we move higher in the tree.		   */

#import "TreeCoord.h"
#import <math.h>
#import <libc.h>

float 
frand(float lo, float hi);

@implementation TreeCoord

/* useful pointers */
- start: (Root *) Ptop: Pobject:(char *)path
{
    object = Pobject;
    return self;
}

/* set the coordinates of the passed branch */
- setCoord:(Branch *) branch
{
    unsigned char      *adr;
    float               r, m;

/* Root Branch, set position based on address */
    if (branch->prev == NULL) {
	adr = (unsigned char *)&branch->root->addr;
	branch->z = 1.5;
	branch->x = adr[0] * 256 + adr[2] + (branch->root->port & 255);
	branch->y = adr[1] * 256 + adr[3] + ((branch->root->port >> 8) & 255);
	return self;
    }

/* if this is a link, this branch should point towards its destination */
    if (branch->link != NULL)
	r = atan2(branch->link->y - branch->prev->y, branch->link->x - branch->prev->x);
    else r = frand(0, M_PI * 2.0);    /* not a link, random direction */

    m = 1.5 / (float)branch->level;

/* m==scaling factor  r==azimuthal angle */
    branch->x = branch->prev->x + cos(r) * m;
    branch->y = branch->prev->y + sin(r) * m;
    branch->z = branch->prev->z + m;

    return self;
}

/* be helpful */
- (char *)help:window :browser
{
    return ("TreeCoord - Steve Ludtke  May 1992\n\nThis is the original view of gopherspace. Each site is drawn as a tree (branches get smaller the higher you go)\n");
}

/* no prefs yet */
- preferences:sender
{
    return self;
}

@end

/* return a random float between lo and hi */
float frand(float lo, float hi)
{
    return (((float)random() / (float)MAXINT) * (hi - lo) + lo);
}
