home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / NeXT / Tree0.5 / treeobj / TreeCoord.m < prev    next >
Encoding:
Text File  |  1992-05-16  |  1.8 KB  |  71 lines

  1. /* TreeCoord.m   Copyright 1992  Steve Ludtke  All Rights Reserved */
  2. /* This object will distribute the branches as "Trees" All branches*/
  3. /* are at a 45 degree angle to the z (up/down) axis. Branches      */
  4. /* become shorter as we move higher in the tree.           */
  5.  
  6. #import "TreeCoord.h"
  7. #import <math.h>
  8. #import <libc.h>
  9.  
  10. float 
  11. frand(float lo, float hi);
  12.  
  13. @implementation TreeCoord
  14.  
  15. /* useful pointers */
  16. - start: (Root *) Ptop: Pobject:(char *)path
  17. {
  18.     object = Pobject;
  19.     return self;
  20. }
  21.  
  22. /* set the coordinates of the passed branch */
  23. - setCoord:(Branch *) branch
  24. {
  25.     unsigned char      *adr;
  26.     float               r, m;
  27.  
  28. /* Root Branch, set position based on address */
  29.     if (branch->prev == NULL) {
  30.     adr = (unsigned char *)&branch->root->addr;
  31.     branch->z = 1.5;
  32.     branch->x = adr[0] * 256 + adr[2] + (branch->root->port & 255);
  33.     branch->y = adr[1] * 256 + adr[3] + ((branch->root->port >> 8) & 255);
  34.     return self;
  35.     }
  36.  
  37. /* if this is a link, this branch should point towards its destination */
  38.     if (branch->link != NULL)
  39.     r = atan2(branch->link->y - branch->prev->y, branch->link->x - branch->prev->x);
  40.     else r = frand(0, M_PI * 2.0);    /* not a link, random direction */
  41.  
  42.     m = 1.5 / (float)branch->level;
  43.  
  44. /* m==scaling factor  r==azimuthal angle */
  45.     branch->x = branch->prev->x + cos(r) * m;
  46.     branch->y = branch->prev->y + sin(r) * m;
  47.     branch->z = branch->prev->z + m;
  48.  
  49.     return self;
  50. }
  51.  
  52. /* be helpful */
  53. - (char *)help:window :browser
  54. {
  55.     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");
  56. }
  57.  
  58. /* no prefs yet */
  59. - preferences:sender
  60. {
  61.     return self;
  62. }
  63.  
  64. @end
  65.  
  66. /* return a random float between lo and hi */
  67. float frand(float lo, float hi)
  68. {
  69.     return (((float)random() / (float)MAXINT) * (hi - lo) + lo);
  70. }
  71.