在 JavaScript 中找到距离半圆中心 n% 的点?
我很遗憾地说数学确实不是我的强项。正常情况下我还能过得去,但这件事让我完全难住了。
我正在尝试用 HTML/CSS/Javascript 编写测验结果屏幕。
在我的界面上,我有一个半圆(目标的右半球)。
我有一系列“分数”(100 以内的整数 - 50、80、90 等)。
我需要将这些点绘制在距离中心 n% 的半圆上,其中 n 是每个分数的值 - 分数越高,该点就越接近目标中心。
我知道我的半圆有多宽,并且已经处理了%值的转换,以便较高的半圆看起来更靠近中心,而较低的半圆看起来更远。
我无法理解的是,将这些点绘制在一条从目标中心点(x = 0,y = 目标高度/2)以随机角度延伸的线上(因此这些点不会重叠) )。
如有任何建议,我们将不胜感激!
I'm sorry to say that Math really isn't my strong suit. Normally I can get by, but this has got me totally stumped.
I'm trying to code up a quiz results screen in HTML/CSS/Javascript.
On my interface, I have a semicircle (the right hemisphere of a target).
I have a range of 'scores' (integers out of 100 - so 50, 80, 90 etc.).
I need to plot these points on the semicircle to be n% away from the centre, where n is the value of each score - the higher the score, the closer to the centre of the target the point will appear.
I know how wide my semicircle is, and have already handled the conversion of the % values so that the higher ones appear closer to the centre while the lower ones appear further out.
What I can't wrap my head around is plotting these points on a line that travels out from the centre point (x = 0, y = target height/2) of the target at a random angle (so the points don't overlap).
Any suggestions are gratefully received!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个示例来说明您希望它是什么样子吗?听起来您想将圆分成
N
片,其中N
是您需要显示的点数,然后沿每个半径绘制点。因此,您可能会看到类似的内容:编辑:代码围绕原点旋转,而不是指定的圆
然后您可以根据您认为合适的方式绘制
点
。Do you have an example of what you want this to look like? It sounds like you want to divide up the circle into
N
slices whereN
is the number of points you need to display, then plot the points along each of those radii. So you might have something like:Edit: code was rotating about the origin, not the circle specified
Then you can plot
points
however you see fit.经过一番绞尽脑汁后,我终于找到了这个解决方案(在一位同事的帮助下,他在这方面比我做得更好):(
arr_result 是一个包含 ID 和分数的数组 - 分数是 100 的百分比)
我'我们省略了用于生成角度数组并随机化该数组的代码 - 这仅用于演示目的,因此标记不会重叠。
在移动标记之前,我还对坐标做了一些奇怪的事情(同样,这已被省略),因为我希望该点位于标记的底部中心而不是左上角。
After much headscratching, I managed to arrive at this solution (with the help of a colleague who's much, much better at this kind of thing than me):
(arr_result is an array containing IDs and scores - scores are percentages of 100)
I've omitted the code for generating the array of angles and randomising that array - that's only needed for presentational purposes so the markers don't overlap.
I also do some weird things with the co-ordinates before I move the markers (again, this has been omitted) as I want the point to be at the bottom-centre of the marker rather than the top-left.