I'm trying to label an animated pie chart, and I've been having a great deal of trouble getting rotated objects to line up with trigonometrically-positioned objects. So, for example, if I have a pie piece that's middle is angle theta and has been rotated n degrees in a tween, and then I try to position a label with code like this:
label.x = center.x + Math.cos((theta + n)/180 * Math.PI) * radius;
label.y = center.y + Math.sin((theta + n)/180 * Math.PI) * radius;
the l开发者_开发百科abel is often not aligned with the center of the pie slice. Since I am also zooming in to the pie chart a great deal, the error becomes significant enough that it occasionally causes the label to miss the pie slice altogether. The error seems relatively unpredictable, and it looks a great deal like a rounding error, but I don't see any obvious rounding going on (the trig functions evaluate to ten or so decimal places, which should be more than enough here).
How can I get these labels to position correctly?
rx = xcos@ + ysin@
ry = -xsin@ + ycos@
If you're rotating from (0,1) then:
rx = sin@
ry = cos@
So then instead of cos for x and sin for y above you should use sin for x and cos for y.
label.x = center.x + Math.sin((theta + n) * Math.PI / 180) * radius;
label.y = center.y + Math.cos((theta + n) * Math.PI / 180) * radius;
精彩评论