-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarmap.js
1117 lines (964 loc) · 32.9 KB
/
starmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Stereographic projection class. */
function StereographicProjection (phi1, lam1, rad) {
this.setCoords(phi1, lam1);
this.setRadius(rad);
}
StereographicProjection.prototype.setCoords = function (phi1, lam1) {
this.phi1 = phi1;
this.lam1 = lam1;
this.cph1 = Math.cos(phi1);
this.sph1 = Math.sin(phi1);
this.cla1 = Math.cos(lam1);
this.sla1 = Math.sin(lam1);
};
StereographicProjection.prototype.setRadius = function (rad) {
this.rad = rad;
};
StereographicProjection.prototype.projectPoints = function (arr) {
function sinSum(cosa, sina, cosb, sinb) {
return cosa*sinb+sina*cosb;
}
function cosSum(cosa, sina, cosb, sinb) {
return cosa*cosb-sina*sinb;
}
var lam1 = this.lam1;
var phi1 = this.phi1;
var rad = this.rad;
var len = arr.length, i;
var res = Array(len);
var cphi = this.cph1, sphi = this.sph1;
var clam = this.cla1, slam = this.sla1;
for (i = 0; i < len; ++i) {
var star = arr[i];
var mag = star[0], re = star[2], de = -star[1];
var t2c = re*re, t2l = de*de, t2c1=1+t2c, t2l1=1+t2l;
var cosc = (1-t2c)/t2c1, sinc = 2*re/t2c1;
var cosl1 = (1-t2l)/t2l1, sinl1 = 2*de/t2l1;
var cosl = cosSum(cosl1, sinl1, clam, slam), sinl = sinSum(cosl1, sinl1, clam, slam);
var k = rad / (1.0 + sphi * sinc + cphi * cosc * cosl);
var x = k * cosc * sinl, y = k * (sphi * cosc * cosl - cphi * sinc);
res[i] = [mag,
x,
y,
x*x + y*y < rad*rad];
}
return res;
};
StereographicProjection.prototype.projectMeridian = function (lam) {
var lam1 = this.lam1;
var phi1 = this.phi1;
var R = this.rad;
var cp1 = this.cph1;
var dlam = lam1-lam;
var sl1 = Math.sin(dlam);
if (Math.abs(sl1) < 1e-10 || Math.abs(cp1) < 1e-10) {
return {
type: 'line',
x: 0,
y: 0,
vx: Math.cos(dlam),
vy: sl1
};
} else {
var x = -R/(cp1*Math.tan(dlam));
var y = R*Math.tan(phi1);
var rho = R/(cp1*sl1);
return {
type: 'circle',
x: x,
y: y,
flip: rho < 0,
r: Math.abs(rho)
};
}
};
StereographicProjection.prototype.projectParallel = function (phi) {
var lam1 = this.lam1;
var phi1 = this.phi1;
var R = this.rad;
var s = this.sph1 + Math.sin(phi);
// TODO: line if s == 0
if (Math.abs(s) < 1e-10) {
return {
type: 'line',
x: 0,
y: 0,
vx: 1,
vy: 0
};
} else {
var rho = R*Math.cos(phi)/s;
return {
type: 'circle',
x: 0,
y: -R*this.cph1/s,
flip: rho < 0,
r: Math.abs(rho)
};
}
};
StereographicProjection.prototype.projectObj = function (re, de) {
var lam1 = this.lam1;
var phi1 = this.phi1;
var rad = this.rad;
var cphi = this.cph1, sphi = this.sph1;
de = lam1-de;
var cosc = Math.cos(re), sinc = Math.sin(re);
var cosl = Math.cos(de), sinl = Math.sin(de);
var k = rad / (1.0 + sphi * sinc + cphi * cosc * cosl);
var x = k * cosc * sinl, y = - k * (cphi * sinc - sphi * cosc * cosl);
return [x, y, x*x + y*y < rad*rad];
};
/** Coordinates of great circle segment from (ra1,de1) to (ra2,de2).
*/
StereographicProjection.prototype.projectGreatSegment = function (ra1, de1, ra2, de2) {
/*
Produced by Maxima 5.18.1:
(%i1) z1: re1 + %i*im1;
(%o1) re1 + %i im1
(%i2) z2: re2 + %i*im2;
(%o2) re2 + %i im2
Center of great circle defined by two points z1 and z2. See (4.4) in
Robin G. Stuart. Application of Complex Analysis to Spherical
Coordinate System. Q.Jl. R. astr. Soc. (1984) 25, 126-136.
(%i3) z0: ((abs(z1)^2-1)*z2 - (abs(z2)^2-1)*z1)/(conjugate(z1)*z2 - z1*conjugate(z2));
2 2 2 2
(re1 + im1 - 1) (re2 + %i im2) - (re1 + %i im1) (re2 + im2 - 1)
(%o3) -------------------------------------------------------------------
(re1 - %i im1) (re2 + %i im2) - (re1 + %i im1) (re2 - %i im2)
(%i4) realpart(z0);
2 2 2 2
im2 (re1 + im1 - 1) - im1 (re2 + im2 - 1)
(%o4) ---------------------------------------------
2 im2 re1 - 2 im1 re2
(%i5) imagpart(z0);
2 2 2 2
(re1 + im1 - 1) re2 - re1 (re2 + im2 - 1)
(%o5) - ---------------------------------------------
2 im2 re1 - 2 im1 re2
*/
function s(x) { return x*x; }
var p1 = this.projectObj(ra1, de1), p2 = this.projectObj(ra2, de2);
var re1 = p1[0]/this.rad, im1 = p1[1]/this.rad;
var re2 = p2[0]/this.rad, im2 = p2[1]/this.rad;
var a1 = re1*re1 + im1*im1 - 1;
var a2 = re2*re2 + im2*im2 - 1;
var ha1 = a1 * im2 - a2 * im1;
var ha2 = - a1 * re2 + a2 * re1;
var d = 2*(im2 * re1 - im1 * re2);
// find center
var c1 = ha1 / d;
var c2 = ha2 / d;
// find radius
var r = Math.sqrt(1 + c1*c1 + c2*c2);
// find angles
a1 = Math.atan2(im1 - c2, re1 - c1);
a2 = Math.atan2(im2 - c2, re2 - c1);
return {
type: 'circle',
x: c1*this.rad, y: c2*this.rad,
a1: a1, a2: a2,
p1: p1, p2: p2,
flip: d < 0,
r: r*this.rad
};
};
/** Project segment of parallel phi from lam1 to lam2.
*/
StereographicProjection.prototype.projectParallelSegment = function (lam1, lam2, phi) {
var p = this.projectParallel(phi);
var p1 = this.projectObj(phi, lam1), p2 = this.projectObj(phi, lam2);
if (p.type === 'circle') {
p.y = - p.y;
p.a1 = Math.atan2(p1[1] - p.y, p1[0] - p.x);
p.a2 = Math.atan2(p2[1] - p.y, p2[0] - p.x);
return p;
} else {
return {
type: 'line',
x1: p1[0], y1: p1[1],
x2: p2[0], y2: p2[1]
};
}
};
/** Project circle centered at point p (already projected to plane)
* with angular radius alpha.
*
* p is usually result of StereographicProjection.prototype.projectObj.
*/
StereographicProjection.prototype.projectCircle2 = function (p, alpha) {
var r = Math.tan(alpha/2);
var aa2 = (p[0]*p[0]+p[1]*p[1])/(this.rad*this.rad);
var denom = 1 - aa2*r*r;
var rho = r*(aa2+1)/denom;
var cx = p[0]*(1+r*r)/denom;
var cy = p[1]*(1+r*r)/denom;
return {
type: 'circle',
x: cx, y: cy,
flip: denom < 0,
rad: this.rad*Math.abs(rho)
};
};
/** Project circle centered at (re, de) on the sphere with angular
* radius alpha.
*/
StereographicProjection.prototype.projectCircle = function (re, de, alpha) {
var p = this.projectObj(re, de);
return this.projectCircle2(p, alpha);
};
/** Project arc of arbitrary circle centered at (re, de) on the sphere
* with angular radius alpha. Arc spans from (re1, de1) to
* (re2, de2).
*/
StereographicProjection.prototype.projectSegment = function (re, de, alpha, re1, de1, re2, de2) {
var p1 = this.projectObj(re1, de1);
var p2 = this.projectObj(re2, de2);
var c = this.projectCircle(re, de, alpha);
c.r = c.rad; // TODO Inconsistence...
c.p1 = p1;
c.p2 = p2;
c.a1 = Math.atan2(p1[1]-c.y, p1[0]-c.x);
c.a2 = Math.atan2(p2[1]-c.y, p2[0]-c.x);
return c;
};
/** Inverse projection of a single point.
*/
StereographicProjection.prototype.inverseObj = function (x, y) {
var lam1 = this.lam1;
var phi1 = this.phi1;
var cp1 = this.cph1, sp1 = this.sph1;
var rho2 = x*x + y*y;
if (rho2 > this.rad*this.rad) {
// Point is out of map
return null;
}
if (rho2 < 1e-18) {
return {
'ra': lam1,
'de': phi1
};
} else {
var rho = Math.sqrt(rho2);
var c = 2*Math.atan(rho/this.rad);
var cosc = Math.cos(c);
var sinc = Math.sin(c);
var phi = Math.asin(cosc*sp1
+ (y*sinc*cp1/rho));
var lam;
if (Math.abs(phi1-Math.PI) < 1e-9) {
// phi1 = PI
lam = lam1 - Math.atan2(x, -y);
} else if (Math.abs(this.phi - (-Math.PI)) < 1e-9) {
// phi1 = -PI
lam = lam1 - Math.atan2(x, y);
} else {
lam = lam1 - Math.atan2(x*sinc,
rho*cp1*cosc - y*sp1*sinc);
}
lam = StarJs.Math.mod(lam, 2*Math.PI);
phi = StarJs.Math.mod(phi+Math.PI, 2*Math.PI)-Math.PI;
return {
'ra': lam,
'de': phi
};
}
};
StereographicProjection.prototype['inverseObj'] = StereographicProjection.prototype.inverseObj;
/**
* Rendering scene: objects are cached for rotation and zooming. When
* rotation or zoom is changed, internal matrix is recalculated, and
* on render method this matrix is applied to all points. We do not
* use canvas' rotation and scaling because first affects labels (we
* wat them horizontal) and second affects line with (we want
* zoom-independent width).
*
* Possible workaround exist for this problem: keep orientation
* internally and compensate it when rendering labels, and keep zoom
* factor too and compensate it when drawing lines, arcs and circles.
*/
function Scene() {
this.objects = [];
this.matrix = null; // TODO
this.scale = 1.0;
this.rotation = 0.0;
}
Scene.prototype.drawLine = function (x1, y1, x2, y2, width, color) {
this.objects.push(function (ctx) {
ctx.beginPath();
ctx.strokeColor = color;
ctx.lineWidth = width/this.scale;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
});
};
Scene.prototype.drawArc = function (x, y, r, a1, a2, width, color) {
this.objects.push(function (ctx) {
var rot = this.rotation;
ctx.beginPath();
ctx.strokeColor = color;
ctx.lineWidth = width/this.scale;
ctx.arc(x, y, r, a1, a2);
ctx.stroke();
});
};
Scene.prototype.drawCircle = function (x, y, r, width, color) {
this.objects.push(function (ctx) {
var rot = this.rotation;
ctx.beginPath();
ctx.strokeColor = color;
ctx.lineWidth = width/this.scale;
ctx.arc(x, y, r, 0, 2*Math.PI);
ctx.stroke();
});
};
Scene.prototype.drawPlanet = function (x, y, r, title, color) {
};
Scene.prototype.drawStars = function (stars) {
};
Scene.prototype.reset = function () {
this.objects = [];
};
/**
* Celestial map component.
* @constructor
*/
function StarMap (elt, size, stars, cnstltns, prop) {
this.paper = document.getElementById(elt);
this.ctx = this.paper.getContext("2d");
this.prop = prop;
this.size = size;
var halfsize = Math.floor(size/2);
this.planets = (typeof prop['planets'] === 'undefined') ? true : prop['planets'];
this.orient = 0;
this.stars = stars;
this.cnstltns = cnstltns;
this['proj'] = new StereographicProjection(0, 0, halfsize);
//this.drawBg();
}
StarMap.prototype.setSize = function (size) {
this['proj'].setRadius(Math.floor(size/2));
this.size = size;
};
StarMap.prototype['setSize'] = StarMap.prototype.setSize;
StarMap.prototype.drawBg = function () {
var size = this.size;
var halfsize = Math.floor(size/2);
var ctx = this.ctx;
ctx.clearRect(0, 0, size, size);
ctx.fillStyle='#FFF';
ctx.fillRect(-halfsize,-halfsize,size, size);
ctx.beginPath();
ctx.fillStyle = (this.prop['circleFill'] || "#000010");
ctx.arc(0, 0, halfsize, 0, 2*Math.PI, true);
ctx.fill();
ctx.beginPath();
ctx.arc(0, 0, halfsize, 0, 2*Math.PI, true);
ctx.clip();
}
StarMap.prototype.setOrient = function (orient) {
this.orient = orient;
};
/** A planet object.
* @constructor
*/
StarMap.Planet = function (pl, size, color) {
this.pl = pl;
this.size = size;
this.color = color;
}
StarMap.Planet.prototype.getCoord = function (jct, earthPos, equ2ecl) {
var pos = this.pl['keplerCoord'](jct);
return new StarJs.Vector.Polar3(equ2ecl.apply(pos.sub(earthPos)));
};
/** A Moon object.
* @constructor
*/
StarMap.Moon = function (size, color) {
this.size = size;
this.color = color;
};
StarMap.Moon.prototype.pl = { name: 'Moon' };
StarMap.Moon.prototype.getCoord = function (jct, earthPos, equ2ecl) {
// earthPos and equ2ecl are ignored
var pos = StarJs.Solar.approxMoon(jct);
return {'phi': pos['ra'], 'theta': pos['dec']};
};
StarMap.PLANETS = [
new StarMap.Planet(StarJs.Solar.BODIES['Sun'], 20, '#FF0'),
new StarMap.Moon(20, '#880'),
new StarMap.Planet(StarJs.Solar.BODIES['Mercury'], 3, '#888'),
new StarMap.Planet(StarJs.Solar.BODIES['Venus'], 4, '#AAA'),
new StarMap.Planet(StarJs.Solar.BODIES['Mars'], 4, '#F80'),
new StarMap.Planet(StarJs.Solar.BODIES['Jupiter'], 6, '#FB0'),
new StarMap.Planet(StarJs.Solar.BODIES['Saturn'], 6, '#AA0'),
new StarMap.Planet(StarJs.Solar.BODIES['Uranus'], 6, '#CAF'),
new StarMap.Planet(StarJs.Solar.BODIES['Neptune'], 6, '#CAF')
];
StarMap.EARTH = StarJs.Solar.BODIES['Earth'];
/** Celestial path (of a comet, asteroid, satellite etc.).
* @constructor
*/
StarMap.Path = function (labels, ras, des, style, labelStyle) {
this.labels = labels;
this.ras = ras;
this.des = des;
this.style = style;
this.labelStyle = labelStyle;
};
StarMap.Path.prototype.draw = function (ctx, proj) {
var labels = this.labels, ra = this.ras, de = this.des;
var pts = Array(de.length);
for (var key in this.style) {
ctx[key] = this.style[key];
}
ctx.beginPath();
for (var i = 0; i < de.length; ++i) {
var cp = pts[i] = proj.projectObj(Math.PI*de[i]/180.0,
Math.PI*ra[i]/12.0);
if (cp[2]) {
var xx = cp[0];
var yy = cp[1];
ctx.lineTo(xx, yy);
}
}
ctx.stroke();
for (key in this.labelStyle) {
ctx[key] = this.labelStyle[key];
}
for (i = 0; i < de.length; ++i) {
var cp = pts[i];
if (cp[2]) {
xx = cp[0];
yy = cp[1];
ctx.beginPath();
ctx.arc(xx, yy, 2, 0, 2*Math.PI, true);
ctx.fill();
if (ctx.fillText) {
ctx.fillText(labels[i], xx, yy - 4);
}
}
}
};
/** Telrad pattern.
* @constructor
*/
StarMap.Telrad = function (lat, lon) {
this.lat = lat;
this.lon = lon;
};
StarMap.Telrad.prototype.draw = function (ctx, proj) {
var p = proj.projectObj(this.lat, this.lon);
var g05 = proj.projectCircle2(p, 0.5/180*Math.PI);
var g20 = proj.projectCircle2(p, 2.0/180*Math.PI);
var g40 = proj.projectCircle2(p, 4.0/180*Math.PI);
var h = Math.floor(this.size/2);
ctx.strokeStyle = 'rgba(255,0,0,0.6)';
ctx.lineWidth = g40.rad/9;
function drawBullEye(g) {
var D = 0.15;
ctx.beginPath();
ctx.arc(g.x, g.y, g.rad, D, 0.5*Math.PI-D, false);
ctx.stroke();
ctx.beginPath();
ctx.arc(g.x, g.y, g.rad, 0.5*Math.PI+D, Math.PI-D, false);
ctx.stroke();
ctx.beginPath();
ctx.arc(g.x, g.y, g.rad, Math.PI+D, 1.5*Math.PI-D, false);
ctx.stroke();
ctx.beginPath();
ctx.arc(g.x, g.y, g.rad, 1.5*Math.PI+D, 2*Math.PI-D, false);
ctx.stroke();
}
ctx.beginPath();
ctx.arc(g05.x, g05.y, g05.rad, 0, 2*Math.PI, true);
ctx.stroke();
drawBullEye(g20);
drawBullEye(g40);
};
/** Constellation boundaries.
* @constructor
*/
StarMap.ConstellationBoundaries = function (boundaries, epoch) {
var DEG2RAD = StarJs.Math.DEG2RAD;
if (typeof epoch === 'undefined') {
epoch = 0; // J2000
}
var prec = StarJs.Coord.precessionEquMatrix((1875-2000)/100.0, epoch);
// Precess polar point
var polar = new StarJs.Vector.Vector3(0,0,1);
this.polarPrec = new StarJs.Vector.Polar3(prec.apply(polar));
// Precess point
var len = boundaries.length;
var result = Array(len);
for (var i = 0; i < len; ++i) {
var pt = boundaries[i].slice(0);
var v = new StarJs.Vector.Polar3(15*Math.PI*pt[0]/180,
DEG2RAD*pt[1]).toVector3();
var p = new StarJs.Vector.Polar3(prec.apply(v));
pt[0] *= 15;
pt.push(p['phi']/DEG2RAD)
pt.push(p['theta']/DEG2RAD);
result[i] = pt;
}
this.boundaries = result;
};
StarMap.ConstellationBoundaries.prototype.draw = function (ctx, proj) {
var DEG2RAD = StarJs.Math.DEG2RAD;
function angSep(a1, a2) {
return StarJs.Math.mod(a1-a2, 2 * Math.PI);
};
// Constellation boundaries
var cstn = null;
// TODO: make a = 0.5 after boundaries line merging. Now many
// lines are drawn twice, and they color differs (somewhat
// brighter) if alpha is used.
ctx.strokeStyle = 'rgba(128,0,128,1)';
var prev = null, first, polarPrec = this.polarPrec;
function drawCnstBnd(prev, l) {
var seg, flip, a1, a2;
if (prev[1] === l[1]) {
a1 = DEG2RAD*prev[0];
a2 = DEG2RAD*l[0];
seg = proj.projectSegment(
// Center
polarPrec['theta'], polarPrec['phi'],
// Radius
Math.PI/2-DEG2RAD*l[1],
// Point 1
prev[4]*DEG2RAD,
prev[3]*DEG2RAD,
// Point 2
l[4]*DEG2RAD,
l[3]*DEG2RAD
);
flip = angSep(a1, a2) < angSep(a2, a1) !== seg.flip;
gr = false;
} else {
a1 = DEG2RAD*prev[1];
a2 = DEG2RAD*l[1];
seg = proj.projectGreatSegment(
prev[4]*DEG2RAD,
prev[3]*DEG2RAD,
// Point 2
l[4]*DEG2RAD,
l[3]*DEG2RAD
);
gr = true;
flip = seg.flip;
}
ctx.beginPath();
switch (seg.type) {
case 'circle':
ctx.arc(seg.x, seg.y,
seg.r,
seg.a1, seg.a2,
flip);
break;
case 'line':
// TODO sometimes lines shouldn't be drawn if their
// central point pass through infinity, or at least
// they should be drawn more intelligently. Can be
// such lines visible in viewport?
ctx.moveTo(seg.x1, seg.y1);
ctx.lineTo(seg.x2, seg.y2);
break;
}
ctx.stroke();
}
var boundaries = this.boundaries;
for (var j = 0; j < boundaries.length; ++j) {
var l = boundaries[j], a1, a2, gr;
if (cstn === l[2]) {
drawCnstBnd(prev, l);
} else {
if (prev !== null) {
drawCnstBnd(prev, first);
}
first = prev = l;
cstn = l[2];
}
prev = l;
}
};
/** An object catalogue (Messier, Caldwell, NGC, etc).
* @constructor
*/
StarMap.Catalogue = function (name, data, colors, renderer) {
function messierColor(mag) {
var v = Math.min(15, Math.floor(19-mag));
var h = v.toString(16);
return '#'+h+h+h;
}
this.name = name;
this.data = data.slice(0);
this.colors = colors;
this.renderer = renderer || function (ctx, cm, cc, colors) {
ctx.beginPath();
ctx.strokeStyle = colors[cc[2]] || messierColor(cc[5]);
ctx.arc(cm[0], cm[1], 4, 0, 2*Math.PI, true);
ctx.stroke();
};
};
StarMap.Catalogue.prototype.draw = function (ctx, proj) {
var data = this.data;
var len = data.length, cc, cm;
for (var i = 0; i < len; ++i) {
cc = data[i];
cm = proj.projectObj(cc[4], 15*cc[3]);
if (cm[2]) {
this.renderer(ctx, cm, cc, this.colors);
}
}
};
/** A graticule.
* @constructor
*/
StarMap.Graticule = function (lon) {
this.lon = lon;
};
StarMap.Graticule.prototype.draw = function (ctx, proj) {
ctx.strokeStyle = '#448';
var halfsize = Math.floor(this.size / 2);
for (var i = -80; i < 90; i += 10) {
var p = proj.projectParallel(Math.PI*i/180);
ctx.beginPath();
ctx.lineWidth = (i === 0) ? 1.7 : 1;
switch (p.type) {
case 'line':
ctx.moveTo(p.x-halfsize*p.vx,
p.y-halfsize*p.vy);
ctx.lineTo(p.x+halfsize*p.vx,
p.y+halfsize*p.vy);
break;
case 'circle':
ctx.arc(p.x, p.y, p.r, 0, 2*Math.PI, true);
break;
}
ctx.stroke();
}
for (i = 0; i < 180; i += 15) {
var p = proj.projectMeridian(Math.PI*i/180);
ctx.beginPath();
ctx.lineWidth = (i === 0 || i === -180) ? 1.7 : 1;
switch (p.type) {
case 'line':
ctx.moveTo(p.x-halfsize*p.vx,
p.y-halfsize*p.vy);
ctx.lineTo(p.x+halfsize*p.vx,
p.y+halfsize*p.vy);
break;
case 'circle':
ctx.arc(p.x, p.y, p.r,
0, 2*Math.PI, true);
break;
}
ctx.stroke();
}
ctx.lineWidth = 2;
ctx.beginPath();
p = proj.projectParallel(Math.PI/2-this.lon);
switch (p.type) {
case 'line':
ctx.moveTo(p.x-halfsize*p.vx,
p.y-halfsize*p.vy);
ctx.lineTo(p.x+halfsize*p.vx,
p.y+halfsize*p.vy);
break;
case 'circle':
ctx.arc(p.x, p.y, p.r, 0, 2*Math.PI, true);
break;
}
ctx.stroke();
ctx.beginPath();
p = proj.projectParallel(-Math.PI/2-this.lon);
switch (p.type) {
case 'line':
ctx.moveTo(p.x-halfsize*p.vx,
p.y-halfsize*p.vy);
ctx.lineTo(p.x+halfsize*p.vx,
p.y+halfsize*p.vy);
break;
case 'circle':
ctx.arc(p.x, p.y, p.r, 0, 2*Math.PI, true);
break;
}
ctx.stroke();
ctx.lineWidth = 1;
};
/** Ecliptics circle.
* @constructor
*/
StarMap.Ecliptics = function () {
};
StarMap.Ecliptics.prototype.draw = function (ctx, proj) {
var eclp = proj.projectCircle(Math.PI/2 + StarJs.Solar.EPS, Math.PI/2, Math.PI/2);
if (eclp.type === 'circle') {
ctx.beginPath();
ctx.strokeStyle = 'yellow';
ctx.arc(eclp.x, eclp.y, eclp.rad, 0, 2*Math.PI, true);
ctx.stroke();
}
};
/** Any object.
* @constructor
*/
StarMap.Object = function (params, color, mjd, jct) {
this.params = params;
this.color = color;
this.pqr = StarJs.Kepler.gaussVec(params.node, params.incl, params.peri);
this.mjd = mjd;
this.jct = jct;
};
StarMap.Object.prototype.draw = function (ctx, proj) {
var pos = StarJs.Kepler.keplerPos(StarJs.Solar.GM,
this.params.t0,
this.mjd,
this.params.q,
this.params.e,
this.pqr);
var earthPos = StarMap.EARTH['keplerCoord'](this.jct);
var ecl2equ = StarJs.Coord.ecl2equMatrix(this.jct);
pos = new StarJs.Vector.Polar3(ecl2equ.apply(pos.sub(earthPos)));
var cm = proj.projectObj(pos['theta'], pos['phi']);
ctx.strokeStyle = ctx.fillStyle = this.color;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(cm[0], cm[1], 6, 0, 2*Math.PI, true);
ctx.stroke();
ctx.beginPath();
ctx.arc(cm[0], cm[1], 4, 0, 2*Math.PI, true);
ctx.fill();
if (this.params.title) {
ctx.fillText(this.params.title, cm[0]+8, cm[1]+8);
}
};
StarMap.prototype.setPos = function (lat, lon, time) {
if (typeof time === 'undefined') {
time = +new Date();
} else if (typeof time !== 'number') {
time = +time;
}
this.lat = lat;
this.lon = lon;
this.time = time;
};
StarMap.prototype.draw = function () {
var lat = this.lat, lon = this.lon, time = this.time;
var Ti = StarJs.Time;
var mjd = Ti.time2mjd(time);
var gms_t = Ti.gmst(mjd);
/** @const */
var DEG2RAD = StarJs.Math.DEG2RAD;
lat *= DEG2RAD;
lon *= DEG2RAD;
lat += gms_t;
this['proj'].setCoords(lon, lat);
var ortho = this['proj'].projectPoints(this.stars);
var cst = [], i, j, slen = ortho.length, co = this.cnstltns, clen = co.length, halfsize = Math.floor(this.size/2);
var ctx = this.ctx;
ctx.save();
ctx.translate(halfsize, halfsize);
ctx.rotate(this.orient);
this.drawBg();
// Draw graticule
(new StarMap.Graticule(lon)).draw(ctx, this['proj']);
// Boundaries
(new StarMap.ConstellationBoundaries(window['CON_BOUND_18'])).draw(ctx, this['proj']);
// Constellations
ctx.beginPath();
ctx.strokeStyle = 'rgba(255,255,255,0.7)';
for (j = clen; j--; ) {
var s = co[j][0], e = co[j][1];
var so = ortho[s], eo = ortho[e];
if (so[3] || eo[3]) {
ctx.moveTo(so[1], so[2]);
ctx.lineTo(eo[1], eo[2]);
}
}
ctx.stroke();
// Draw ecliptics
(new StarMap.Ecliptics()).draw(ctx, this['proj']);
// Stars
ctx.fillStyle = '#FFF';
for (i = 0; i < slen; ++i) {
var s = ortho[i];
if (s[3]) {
ctx.beginPath();
ctx.arc(s[1], s[2],
Math.max(3-s[0]/2, 0.5),
0, 2*Math.PI, true);
ctx.fill();
}
}
function messierColor(mag) {
var v = Math.min(15, Math.floor(19-mag));
var h = v.toString(16);
return '#'+h+h+h;
}
// Draw Messier objects
if (this.prop && this.prop['messier']) {
(new StarMap.Catalogue("Messier", this.prop['messier'], this.prop['messier_colors'])).draw(ctx, this['proj']);
}
// Draw Caldwell objects
if (this.prop && this.prop['caldwell']) {
(new StarMap.Catalogue("Caldwell", this.prop['caldwell'], this.prop['caldwell_colors'])).draw(ctx, this['proj']);
}
// Draw planets
var jct = Ti.mjd2jct(mjd);
if (this.planets) {
var earthPos = StarMap.EARTH['keplerCoord'](jct);
var equ2ecl = StarJs.Coord.ecl2equMatrix(jct);
for (i = 0; i < StarMap.PLANETS.length; ++i) {
var planet = StarMap.PLANETS[i];
var cc = planet.getCoord(jct, earthPos, equ2ecl);
var cm = this['proj'].projectObj(cc['theta'], cc['phi']);
if (cm[2]) {
ctx.beginPath();
ctx.fillStyle = planet.color;
var xx = cm[0], yy = cm[1];
ctx.arc(xx, yy, planet.size/2,
0, 2*Math.PI, true);
ctx.fill();
ctx.beginPath();
ctx.strokeStyle = planet.color;
ctx.arc(xx, yy, planet.size/2 + 2,
0, 2*Math.PI, true);
ctx.stroke();
if (ctx.fillText) {
ctx.fillText(planet.pl.name,
xx + planet.size/2 + 1,
yy - planet.size/2 - 1);
}
}
}
}
// Draw sample telrads
(new StarMap.Telrad(0.901, 0.451)).draw(ctx, this['proj']);
(new StarMap.Telrad(0.301, 2.151)).draw(ctx, this['proj']);
/*
Ephemerides from http://www.minorplanetcenter.net/iau/MPEph/MPEph.html
C/2009 P1 (Garradd)
Perturbed ephemeris below is based on elements from MPEC 2012-A52.
CK09P010
Date UT R.A. (J2000) Decl. Delta r El. Ph. m1 Sky Motion
h m s "/min P.A.
2012 01 17 000000 17.4533 +32.839 1.749 1.586 64.0 33.9 7.2 1.17 350.9
2012 01 22 000000 17.4175 +35.301 1.683 1.602 68.1 34.8 7.2 1.34 348.6
2012 01 27 000000 17.3649 +38.107 1.615 1.621 72.6 35.4 7.1 1.54 346.0
2012 02 01 000000 17.2890 +41.290 1.548 1.642 77.3 35.8 7.1 1.77 343.2
2012 02 06 000000 17.1802 +44.877 1.482 1.666 82.2 35.9 7.1 2.02 340.1
2012 02 11 000000 17.0241 +48.874 1.421 1.692 87.3 35.6 7.0 2.29 336.3
2012 02 16 000000 16.7981 +53.248 1.367 1.720 92.4 35.0 7.0 2.57 331.7
2012 02 21 000000 16.4653 +57.889 1.321 1.750 97.4 34.1 7.0 2.85 325.6
2012 02 26 000000 15.9670 +62.547 1.288 1.782 102.1 32.9 7.1 3.09 317.2
2012 03 02 000000 15.2189 +66.752 1.269 1.815 106.2 31.6 7.1 3.26 305.0
2012 03 07 000000 14.1497 +69.747 1.267 1.851 109.4 30.4 7.2 3.35 288.1
2012 03 12 000000 12.8391 +70.676 1.283 1.888 111.4 29.4 7.3 3.33 267.6
2012 03 17 000000 11.5897 +69.266 1.317 1.926 112.0 28.6 7.4 3.20 247.8
2012 03 22 000000 10.6405 +66.145 1.368 1.965 111.4 28.2 7.6 3.00 232.4
2012 03 27 000000 09.9973 +62.191 1.436 2.005 109.7 27.9 7.8 2.75 221.2
2012 04 01 000000 09.5739 +58.016 1.517 2.047 107.0 27.8 8.0 2.48 213.1