Skip to content

Commit c620069

Browse files
committed
Bug fix for issue #216: circle radius is computed more accurately now
1 parent 921d395 commit c620069

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/js/shapes/Circle.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,50 @@ export let Circle = (function() {
222222

223223
let hidden = true;
224224

225-
var ra, dec, vertOnCircle, dx, dy;
226-
this.radius = Number.NEGATIVE_INFINITY;
227225

228226
// Project 4 points lying on the circle and take the minimal dist with the center as radius
229-
[[-1, 0], [1, 0], [0, -1], [0, 1]].forEach(([cardDirRa, cardDirDec]) => {
230-
ra = this.centerRaDec[0] + cardDirRa * this.radiusDegrees;
231-
dec = this.centerRaDec[1] + cardDirDec * this.radiusDegrees;
227+
const degToRad = Math.PI / 180;
228+
const radToDeg = 180 / Math.PI;
229+
const sampleCoordinates = [];
230+
const radiusRadians = this.radiusDegrees * degToRad;
231+
const raCenterRadians = this.centerRaDec[0] * degToRad;
232+
const decCenterRadians = this.centerRaDec[1] * degToRad;
233+
// compute 4 sample coordinates lying on the circle
234+
for (let i = 0; i < 4; i++) {
235+
const phi = i * 2 * Math.PI / 4;
236+
237+
const sampleDec = Math.asin(
238+
Math.sin(decCenterRadians) * Math.cos(radiusRadians) +
239+
Math.cos(decCenterRadians) * Math.sin(radiusRadians) * Math.cos(phi)
240+
);
241+
242+
const sampleRa = raCenterRadians + Math.atan2(
243+
Math.sin(phi) * Math.sin(radiusRadians) * Math.cos(decCenterRadians),
244+
Math.cos(radiusRadians) - Math.sin(decCenterRadians) * Math.sin(sampleDec)
245+
);
246+
247+
// Normalize RA to [0, 2π] and convert to degrees
248+
const sampleRaDeg = radToDeg * ((sampleRa + 2 * Math.PI) % (2 * Math.PI));
249+
const sampleDecDeg = radToDeg * sampleDec;
250+
251+
sampleCoordinates.push([sampleRaDeg, sampleDecDeg]);
252+
}
232253

254+
let vertOnCircle, dx, dy;
255+
this.radius = Number.NEGATIVE_INFINITY;
256+
sampleCoordinates.forEach(([ra, dec]) => {
233257
vertOnCircle = view.aladin.world2pix(ra, dec);
234258

235259
if (vertOnCircle) {
236260
dx = vertOnCircle[0] - this.center.x;
237261
dy = vertOnCircle[1] - this.center.y;
238262

239-
this.radius = Math.max(Math.sqrt(dx*dx + dy*dy), this.radius);
263+
if (this.radius !== Number.NEGATIVE_INFINITY) {
264+
this.radius = Math.min(Math.sqrt(dx*dx + dy*dy), this.radius);
265+
}
266+
else {
267+
this.radius = Math.sqrt(dx*dx + dy*dy);
268+
}
240269

241270
hidden = false;
242271
}
@@ -245,6 +274,7 @@ export let Circle = (function() {
245274
if (hidden) {
246275
return false;
247276
}
277+
248278
// Then we can draw
249279

250280
var baseColor = this.color;

0 commit comments

Comments
 (0)