coord.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * coord.c - Coordinate representation and basic operations
  3. *
  4. * Written 2009, 2010, 2016 by Werner Almesberger
  5. * Copyright 2009, 2010, 2016 by Werner Almesberger
  6. * Copyright 2016, Erich Heinzle (gEDA additions)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <math.h>
  14. #include "util.h"
  15. #include "coord.h"
  16. /* ----- unit conversion --------------------------------------------------- */
  17. double mm_to_mil(double mm, int exponent)
  18. {
  19. return mm*pow(MIL_IN_MM, -exponent);
  20. }
  21. double mil_to_mm(double mil, int exponent)
  22. {
  23. return mil*pow(MIL_IN_MM, exponent);
  24. }
  25. double um_to_mm(double um, int exponent)
  26. {
  27. return um*pow(UM_IN_MM, exponent);
  28. }
  29. /* ----- convert internal units to best external unit ---------------------- */
  30. double units_to_best(unit_type u, int *mm)
  31. {
  32. /*
  33. * For finding the best choice, we work with deci-micrometers and
  34. * micro-inches. The conversion to "dum" is actually a no-op, but that
  35. * may change if we ever pick a different internal unit than 0.1 um.
  36. */
  37. long dum = round(units_to_mm(u)*10000.0);
  38. long uin = round(units_to_mil(u)*1000.0);
  39. /* remove trailing zeroes */
  40. while (dum && !(dum % 10))
  41. dum /= 10;
  42. while (uin && !(uin % 10))
  43. uin /= 10;
  44. /* ceil(log10(dum)) <= ceil(log10(uin)) ? */
  45. while (dum && uin) {
  46. dum /= 10;
  47. uin /= 10;
  48. }
  49. if (!dum) {
  50. *mm = 1;
  51. return units_to_mm(u);
  52. } else {
  53. *mm = 0;
  54. return units_to_mil(u);
  55. }
  56. }
  57. /* ----- vector operations ------------------------------------------------- */
  58. struct coord normalize(struct coord v, unit_type len)
  59. {
  60. double f;
  61. f = len/hypot(v.x, v.y);
  62. v.x *= f;
  63. v.y *= f;
  64. return v;
  65. }
  66. struct coord rotate(struct coord v, double angle)
  67. {
  68. double rad = M_PI*angle/180.0;
  69. struct coord res;
  70. res.x = v.x*cos(rad)-v.y*sin(rad);
  71. res.y = v.y*cos(rad)+v.x*sin(rad);
  72. return res;
  73. }
  74. struct coord add_vec(struct coord a, struct coord b)
  75. {
  76. a.x += b.x;
  77. a.y += b.y;
  78. return a;
  79. }
  80. struct coord sub_vec(struct coord a, struct coord b)
  81. {
  82. a.x -= b.x;
  83. a.y -= b.y;
  84. return a;
  85. }
  86. struct coord neg_vec(struct coord v)
  87. {
  88. v.x = -v.x;
  89. v.y = -v.y;
  90. return v;
  91. }
  92. /* ----- point on circle --------------------------------------------------- */
  93. struct coord rotate_r(struct coord c, unit_type r, double angle)
  94. {
  95. struct coord p;
  96. angle = angle/180.0*M_PI;
  97. p.x = c.x+r*cos(angle);
  98. p.y = c.y+r*sin(angle);
  99. return p;
  100. }
  101. double theta_vec(struct coord v)
  102. {
  103. double a;
  104. a = atan2(v.y, v.x)/M_PI*180.0;
  105. if (a < 0)
  106. a += 360.0;
  107. return a;
  108. }
  109. double theta(struct coord c, struct coord p)
  110. {
  111. p.x -= c.x;
  112. p.y -= c.y;
  113. return theta_vec(p);
  114. }
  115. /* ----- sorting coordinates ----------------------------------------------- */
  116. void sort_coord(struct coord *min, struct coord *max)
  117. {
  118. if (min->x > max->x)
  119. SWAP(min->x, max->x);
  120. if (min->y > max->y)
  121. SWAP(min->y, max->y);
  122. }
  123. /* ----- distance calculations --------------------------------------------- */
  124. unit_type dist_point(struct coord a, struct coord b)
  125. {
  126. return hypot(a.x-b.x, a.y-b.y);
  127. }
  128. static unit_type dist_line_xy(unit_type px, unit_type py,
  129. unit_type ax, unit_type ay, unit_type bx, unit_type by)
  130. {
  131. unit_type d_min, d;
  132. double a, f;
  133. d_min = hypot(ax-px, ay-py);
  134. d = hypot(bx-px, by-py);
  135. if (d < d_min)
  136. d_min = d;
  137. if (ax != bx || ay != by) {
  138. /*
  139. * We make a the line vector from point B and b the vector from
  140. * B to point P. Then we calculate the projection of b on a.
  141. */
  142. ax -= bx;
  143. ay -= by;
  144. bx = px-bx;
  145. by = py-by;
  146. a = hypot(ax, ay);
  147. f = ((double) ax*bx+(double) ay*by)/a/a;
  148. if (f >= 0 && f <= 1) {
  149. bx -= f*ax;
  150. by -= f*ay;
  151. d = hypot(bx, by);
  152. if (d < d_min)
  153. d_min = d;
  154. }
  155. }
  156. return d_min;
  157. }
  158. unit_type dist_line(struct coord p, struct coord a, struct coord b)
  159. {
  160. return dist_line_xy(p.x, p.y, a.x, a.y, b.x, b.y);
  161. }
  162. unit_type dist_rect(struct coord p, struct coord a, struct coord b)
  163. {
  164. unit_type d_min, d;
  165. d_min = dist_line_xy(p.x, p.y, a.x, a.y, b.x, a.y);
  166. d = dist_line_xy(p.x, p.y, a.x, a.y, a.x, b.y);
  167. if (d < d_min)
  168. d_min = d;
  169. d = dist_line_xy(p.x, p.y, a.x, b.y, b.x, b.y);
  170. if (d < d_min)
  171. d_min = d;
  172. d = dist_line_xy(p.x, p.y, b.x, a.y, b.x, b.y);
  173. if (d < d_min)
  174. d_min = d;
  175. return d_min;
  176. }
  177. int inside_rect(struct coord p, struct coord a, struct coord b)
  178. {
  179. sort_coord(&a, &b);
  180. if (p.x < a.x || p.x > b.x)
  181. return 0;
  182. if (p.y < a.y || p.y > b.y)
  183. return 0;
  184. return 1;
  185. }
  186. unit_type dist_circle(struct coord p, struct coord c, unit_type r)
  187. {
  188. unit_type d;
  189. d = hypot(p.x-c.x, p.y-c.y);
  190. return fabs(d-r);
  191. }