hex.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "map.h"
  4. #define BIG 1.e15
  5. #define HFUZZ .0001
  6. static double hcut[3] ;
  7. static double kr[3] = { .5, -1., .5 };
  8. static double ki[3] = { -1., 0., 1. }; /*to multiply by sqrt(3)/2*/
  9. static double cr[3];
  10. static double ci[3];
  11. static struct place hem;
  12. static struct coord twist;
  13. static double rootroot3, hkc;
  14. static double w2;
  15. static double rootk;
  16. static void
  17. reflect(int i, double wr, double wi, double *x, double *y)
  18. {
  19. double pr,pi,l;
  20. pr = cr[i]-wr;
  21. pi = ci[i]-wi;
  22. l = 2*(kr[i]*pr + ki[i]*pi);
  23. *x = wr + l*kr[i];
  24. *y = wi + l*ki[i];
  25. }
  26. static int
  27. Xhex(struct place *place, double *x, double *y)
  28. {
  29. int ns;
  30. register i;
  31. double zr,zi;
  32. double sr,si,tr,ti,ur,ui,vr,vi,yr,yi;
  33. struct place p;
  34. copyplace(place,&p);
  35. ns = place->nlat.l >= 0;
  36. if(!ns) {
  37. p.nlat.l = -p.nlat.l;
  38. p.nlat.s = -p.nlat.s;
  39. }
  40. if(p.nlat.l<HFUZZ) {
  41. for(i=0;i<3;i++)
  42. if(fabs(reduce(p.wlon.l-hcut[i]))<HFUZZ) {
  43. if(i==2) {
  44. *x = 2*cr[0] - cr[1];
  45. *y = 0;
  46. } else {
  47. *x = cr[1];
  48. *y = 2*ci[2*i];
  49. }
  50. return(1);
  51. }
  52. p.nlat.l = HFUZZ;
  53. sincos(&p.nlat);
  54. }
  55. norm(&p,&hem,&twist);
  56. Xstereographic(&p,&zr,&zi);
  57. zr /= 2;
  58. zi /= 2;
  59. cdiv(1-zr,-zi,1+zr,zi,&sr,&si);
  60. csq(sr,si,&tr,&ti);
  61. ccubrt(1+3*tr,3*ti,&ur,&ui);
  62. csqrt(ur-1,ui,&vr,&vi);
  63. cdiv(rootroot3+vr,vi,rootroot3-vr,-vi,&yr,&yi);
  64. yr /= rootk;
  65. yi /= rootk;
  66. elco2(fabs(yr),yi,hkc,1.,1.,x,y);
  67. if(yr < 0)
  68. *x = w2 - *x;
  69. if(!ns) reflect(hcut[0]>place->wlon.l?0:
  70. hcut[1]>=place->wlon.l?1:
  71. 2,*x,*y,x,y);
  72. return(1);
  73. }
  74. proj
  75. hex(void)
  76. {
  77. register i;
  78. double t;
  79. double root3;
  80. double c,d;
  81. struct place p;
  82. hcut[2] = PI;
  83. hcut[1] = hcut[2]/3;
  84. hcut[0] = -hcut[1];
  85. root3 = sqrt(3.);
  86. rootroot3 = sqrt(root3);
  87. t = 15 -8*root3;
  88. hkc = t*(1-sqrt(1-1/(t*t)));
  89. elco2(BIG,0.,hkc,1.,1.,&w2,&t);
  90. w2 *= 2;
  91. rootk = sqrt(hkc);
  92. latlon(90.,90.,&hem);
  93. latlon(90.,0.,&p);
  94. Xhex(&p,&c,&t);
  95. latlon(0.,0.,&p);
  96. Xhex(&p,&d,&t);
  97. for(i=0;i<3;i++) {
  98. ki[i] *= root3/2;
  99. cr[i] = c + (c-d)*kr[i];
  100. ci[i] = (c-d)*ki[i];
  101. }
  102. deg2rad(0.,&twist);
  103. return(Xhex);
  104. }
  105. int
  106. hexcut(struct place *g, struct place *og, double *cutlon)
  107. {
  108. int t,i;
  109. if(g->nlat.l>=-HFUZZ&&og->nlat.l>=-HFUZZ)
  110. return(1);
  111. for(i=0;i<3;i++) {
  112. t = ckcut(g,og,*cutlon=hcut[i]);
  113. if(t!=1) return(t);
  114. }
  115. return(1);
  116. }