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

  1. /* StarCoord.m  CopyRight 1992 Steve Ludtke  All Rights Reserved         */
  2. /* This object distributes the branches in all directions around a starting */
  3. /* point in 3d. Has a tendency (intentional) to move up and down more than  */
  4. /* left and right.                                */
  5.  
  6. #import "StarCoord.h"
  7. #import <stdio.h>
  8. #import <math.h>
  9. #import <libc.h>
  10.  
  11. @implementation StarCoord
  12.  
  13. /* get useful pointers */
  14. - start: (Root *) Ptop: Pobject:(char *)path
  15. {
  16.     object = Pobject;
  17.     return self;
  18. }
  19.  
  20. /* set coordinates of branch */
  21. - setCoord:(Branch *) branch
  22. {
  23.     unsigned char      *adr;
  24.     float               c, t, m;
  25.  
  26.     c = atan2(1.0, 2.0);
  27. /* network address + port are split into 3, 2 byte words and used as */
  28. /* x,y,z coordinates for the Root Branch                  */
  29.     if (branch->prev == NULL) {
  30.     adr = (unsigned char *)&branch->root->addr;
  31.     branch->x = adr[0] * 256 + adr[3];
  32.     branch->y = adr[1] * 256 + ((branch->root->port >> 8) & 255);
  33.     branch->z = adr[2] * 256 + (branch->root->port & 255);
  34.     return self;
  35.     }
  36.  
  37. /* if we are looking at a web link, point towards the destination in the */
  38. /* x-y plane (reduces crossed wires).                     */
  39.     if (branch->link == NULL)
  40.     c = frand(0, M_PI * 2.0);
  41.     else
  42.     c = atan2(branch->link->y - branch->prev->y, branch->link->x - branch->prev->x);
  43.  
  44. /* for the first branch, go in a random direction. After that only go in */
  45. /* the up or down half sphere.                         */
  46.     if (branch->level == 1)
  47.     t = frand(-M_PI / 2.0, M_PI / 2.0);
  48.     else if (branch->z - branch->root->branch.z > 0.0)
  49.     t = frand(0.0, M_PI / 2.0);
  50.     else
  51.     t = frand(-M_PI / 2.0, 0.0);
  52.  
  53. /* c==chi=azimuth  t==theta=altitude in spherical coords */
  54.     m = 1.5 / (float)branch->level;
  55.  
  56.     branch->x = branch->prev->x + cos(c) * cos(t) * m;
  57.     branch->y = branch->prev->y + sin(c) * cos(t) * m;
  58.     branch->z = branch->prev->z + sin(t) * m;
  59.  
  60.     return self;
  61. }
  62.  
  63. /* no prefs yet */
  64. - preferences:sender
  65. {
  66.     return self;
  67. }
  68.  
  69. /* be helpful */
  70. - (char *)help:window :browser
  71. {
  72.     return ("StarCoord - Steve Ludtke  May 1992\n\nThis is another simple example of a Coord object. This one splits the 6 bytes of address/port# into 2 16 bit words and uses the results as x,y,z coordinates for the center of the object. Branches spread both up and down from the center in a rather haphazard fashion. This is hardly an ideal distribution method, but it is a good, simple example.\n");
  73. }
  74.  
  75. @end
  76.