vo_extend.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <grass/vector.h>
  4. #include "sw_defs.h"
  5. #include "defs.h"
  6. #include "write.h"
  7. /*-extend_line() finds coordinates along the boundary of a window
  8. * that also lie on a specified line (ax+by=c). The
  9. * line can cross at least two boundaries---the line
  10. * that intersects the midpoint of s1 and s2 determines
  11. * which coordinates are placed in (c_x, c_y).
  12. *
  13. * The limits of the window are described by:
  14. * e: east
  15. * w: west
  16. * s: south
  17. * n: north
  18. * Note that the following constraints must be true:
  19. * ( w < e ) ( s < n )
  20. *
  21. * x and y are points on the line ax + by = c that are assumed
  22. * to lie within the window.
  23. *
  24. * the c_x and c_y values are changed.
  25. *
  26. * returns: 0 on error, 1 otherwise
  27. */
  28. int extend_line(double s, double n, double w, double e,
  29. double a, double b, double c, double x, double y,
  30. double *c_x, double *c_y, int knownPointAtLeft)
  31. {
  32. double nx, ny; /* intesection coordinates */
  33. if (x > w && x < e && y > s && y < n) {
  34. /* vertical line? */
  35. if (a == 0) {
  36. *c_y = c / b;
  37. *c_x = knownPointAtLeft ? s : n;
  38. }
  39. /* horizontal line? */
  40. if (b == 0) {
  41. *c_x = c / a;
  42. *c_y = knownPointAtLeft ? e : w;
  43. }
  44. /* south */
  45. nx = (c - b * s) / a;
  46. if (Vect_point_in_box(nx, s, 0.0, &Box) &&
  47. ((nx > x && knownPointAtLeft) || (nx <= x && !knownPointAtLeft)))
  48. {
  49. *c_x = nx;
  50. *c_y = s;
  51. return 1;
  52. }
  53. /* north */
  54. nx = (c - b * n) / a;
  55. if (Vect_point_in_box(nx, n, 0.0, &Box) &&
  56. ((nx > x && knownPointAtLeft) || (nx <= x && !knownPointAtLeft)))
  57. {
  58. *c_x = nx;
  59. *c_y = n;
  60. return 1;
  61. }
  62. if (knownPointAtLeft) {
  63. /* east */
  64. ny = (c - a * e) / b;
  65. if (Vect_point_in_box(e, ny, 0.0, &Box)) {
  66. *c_x = e;
  67. *c_y = ny;
  68. return 1;
  69. }
  70. }
  71. else {
  72. /* west */
  73. ny = (c - a * w) / b;
  74. if (Vect_point_in_box(w, ny, 0.0, &Box)) {
  75. *c_x = w;
  76. *c_y = ny;
  77. return 1;
  78. }
  79. }
  80. }
  81. return 0;
  82. }