vo_extend.c 2.0 KB

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