home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / ASCENDER / ascendMar8.tar / UMass / BoldtOld / MERGE / geometry2d.c next >
C/C++ Source or Header  |  1995-04-14  |  2KB  |  106 lines

  1. #include "geometry2d.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. /*======================================================================\
  6. |  GEOMETRY2D.C  - 2d point and line geometry                           |
  7. |                                                                       |
  8. |  1/28/95 Bob Collins,  University of Massachusetts                    |
  9. \======================================================================*/
  10.  
  11.  
  12.   /***********/
  13.  /* POINT2D */
  14. /***********/
  15.  
  16. POINT2D *set_point2d(POINT2D *point, float xval, float yval)
  17. {
  18.   point->x = xval;
  19.   point->y = yval;
  20.   return(point);
  21. }
  22.  
  23. POINT2D *translate_point2d(POINT2D *point, float deltax, float deltay)
  24. {
  25.   point->x += deltax;
  26.   point->y += deltay;
  27.   return(point);
  28. }
  29.  
  30. POINT2D *copy_point2d(POINT2D *to, POINT2D *from)
  31. {
  32.   to->x = from->x;
  33.   to->y = from->y;
  34.   return(to);
  35. }
  36.  
  37. void swap_points2d(POINT2D *a, POINT2D *b)
  38. {
  39.   POINT2D tmp; 
  40.   tmp.x = a->x; tmp.y = a->y; 
  41.   a->x = b->x; a->y = b->y; 
  42.   b->x = tmp.x; b->y = tmp.y;
  43.   return;
  44. }
  45.  
  46. double distance_pp2d(POINT2D *a, POINT2D *b)
  47. {
  48.   double dx,dy;
  49.   dx = a->x - b->x;
  50.   dy = a->y - b->y;
  51.   return(sqrt(dx*dx + dy*dy));
  52. }
  53.  
  54.  
  55.   /**********/
  56.  /* LINE2D */
  57. /**********/
  58.  
  59. LINE2D *copy_line2d(LINE2D *to, LINE2D *from)
  60. {
  61.   to->a = from->a;
  62.   to->b = from->b;
  63.   to->c = from->c;
  64.   return(to);
  65. }
  66.  
  67. LINE2D *set_line2d(LINE2D *line, float aval, float bval, float cval)
  68. {
  69.   float tmp;
  70.  
  71.   tmp = sqrt(aval*aval + bval*bval);
  72.  
  73.   if (tmp == 0.0)
  74.     return(NULL);
  75.   else {
  76.     line->a = aval/tmp; 
  77.     line->b = bval/tmp; 
  78.     line->c = cval/tmp;
  79.     return(line);
  80.   }
  81. }
  82.  
  83.  
  84. LINE2D *set_line2d_from_endpoints(LINE2D *line, POINT2D *pt1, POINT2D *pt2)
  85. {
  86.  return(set_line2d(line, pt1->y - pt2->y, pt2->x - pt1->x, 
  87.            pt1->x * pt2->y - pt2->x * pt1->y));
  88. }
  89.  
  90.  
  91. float distance_pl2d(POINT2D *point, LINE2D *line)
  92. {
  93.   return(fabs(line->a * point->x + line->b * point->y + line->c));
  94. }
  95.  
  96. float signed_distance_pl2d(POINT2D *point, LINE2D *line)
  97. {
  98.   return(line->a * point->x + line->b * point->y + line->c);
  99. }
  100.  
  101. float sin_angle_ll2d(LINE2D *line1, LINE2D *line2)
  102. {
  103.   return(fabs((line1->a * line2->b) - (line1->b * line2->a)));
  104. }
  105.  
  106.