g_vector.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file includes vector-related utility routines. */
  16. #include "sys-defines.h"
  17. #include "extern.h"
  18. #define VLENGTH(v) sqrt( (v).x * (v).x + (v).y * (v).y )
  19. /* Scale an input vector to a new length, and return it. */
  20. plVector *
  21. _vscale(plVector *v, double newlen)
  22. {
  23. double len = VLENGTH(*v);
  24. if (len != 0.0)
  25. {
  26. v->x *= newlen/len;
  27. v->y *= newlen/len;
  28. }
  29. return(v);
  30. }
  31. /* Compute angle (arctangent) of 2-D vector via atan2, but standardize
  32. handling of singular cases. */
  33. double
  34. _xatan2 (double y, double x)
  35. {
  36. if (y == 0.0 && x >= 0.0)
  37. return 0.0;
  38. else if (y == 0.0 && x < 0.0)
  39. return M_PI;
  40. else if (x == 0.0 && y >= 0.0)
  41. return M_PI_2;
  42. else if (x == 0.0 && y < 0.0)
  43. return -(M_PI_2);
  44. else
  45. return atan2(y, x);
  46. }
  47. /* Compute angle between vectors pc..p0 and pc..pp1, in range -pi..pi;
  48. collinear vectors yield an angle of pi. This is used when drawing arcs. */
  49. double
  50. _angle_of_arc(plPoint p0, plPoint pp1, plPoint pc)
  51. {
  52. plVector v0, v1;
  53. double cross, angle, angle0;
  54. /* vectors from pc to p0, and pc to pp1 */
  55. v0.x = p0.x - pc.x;
  56. v0.y = p0.y - pc.y;
  57. v1.x = pp1.x - pc.x;
  58. v1.y = pp1.y - pc.y;
  59. /* relative polar angle of p0 */
  60. angle0 = _xatan2 (v0.y, v0.x);
  61. /* cross product, zero means points are collinear */
  62. cross = v0.x * v1.y - v1.x * v0.y;
  63. if (cross == 0.0)
  64. /* by libplot convention, sweep angle should be M_PI not -(M_PI), in
  65. the collinear case */
  66. angle = M_PI;
  67. else
  68. /* compute angle in range -(M_PI)..M_PI */
  69. {
  70. double angle1;
  71. angle1 = _xatan2 (v1.y, v1.x);
  72. angle = angle1 - angle0;
  73. if (angle > M_PI)
  74. angle -= (2.0 * M_PI);
  75. else if (angle < -(M_PI))
  76. angle += (2.0 * M_PI);
  77. }
  78. return angle;
  79. }
  80. /* Adjust the location of pc so it can be used as the center of a circle or
  81. circular arc through p0 and p1. If pc does not lie on the line that
  82. perpendicularly bisects the line segment from p0 to p1, it is projected
  83. orthogonally onto it. p0 and p1 are assumed not to be coincident. */
  84. plPoint
  85. _truecenter(plPoint p0, plPoint p1, plPoint pc)
  86. {
  87. plPoint pm;
  88. plVector a, b, c;
  89. double scale;
  90. /* midpoint */
  91. pm.x = 0.5 * (p0.x + p1.x);
  92. pm.y = 0.5 * (p0.y + p1.y);
  93. /* a points along perpendicular bisector */
  94. a.x = -p1.y + p0.y;
  95. a.y = p1.x - p0.x;
  96. /* b points from midpoint to pc */
  97. b.x = pc.x - pm.x;
  98. b.y = pc.y - pm.y;
  99. /* c is orthogonal projection of b onto a */
  100. scale = (a.x * b.x + a.y * b.y) / (a.x * a.x + a.y * a.y);
  101. c.x = scale * a.x;
  102. c.y = scale * a.y;
  103. /* adjust pc */
  104. pc.x = pm.x + c.x;
  105. pc.y = pm.y + c.y;
  106. return pc;
  107. }