javascript - D3js find closest point on circle -
what i'm trying achieve take current coordinates of mousemove event , match them coordinates of closest location on circle. i've managed partially working using loop iterates on each possible point in circle , compares coordinates in order find closest point:
(var = math.pi; > -math.pi; -= 0.01) { // coords of current point in circle: var curx = math.sin(i+math.pi / 2)*radius + 200, cury = math.sin(i)*radius + 200; // conditional attempts find coords of point closest cursor... if (math.abs((x - curx)) < distancex && math.abs((y - cury)) < distancey ) { distancex = math.abs((x - curx)); distancey = math.abs((y - cury)); // , assigns values newx/newy variables newx = curx; newy = cury; } }
the trouble solution return wrong coordinates , i'm not sure why.
jsfiddle see mean:
no need loop, compute point between center of circle , mouse on circle.
var dx = x - 200, dy = y - 200, dist = math.sqrt(dx*dx + dy*dy), newx = 200 + dx * radius / dist, newy = 200 + dy * radius / dist;
Comments
Post a Comment