我想知道给定角度的椭圆上顶点的坐标。椭圆中心位于 x = 0 和 y = 0 处。长半轴为 x,值为 1。短半轴为 y,值为 0.38。角度是可变的。
我已经尝试了这个 Stack Exchange 帖子上的所有内容,但没有任何效果:https ://math.stackexchange.com/questions/22064/calculate-a-point-that-lies-on-an-ellipse-given-an-angle
可能不是给定的解决方案是错误的,而是我将它们集成在 JavaScript 中。
例如,这不起作用:
a = 1
b = 0.38
function getAngle(x1, y1, x2, y2) {
return Math.atan((y2 - y1) / (x2 - x1)) * 180 / Math.PI
}
angle = getAngle(0,0,0.3,0.7)
x = (a*b*Math.cos(angle))
/
Math.sqrt(
(a*Math.cos(angle))**2 +
(b*Math.sin(angle))**2
)
y = (a*b*Math.sin(angle))
/
Math.sqrt(
(a*Math.cos(angle))**2 +
(b*Math.sin(angle))**2
)
console.log({x,y})
Run Code Online (Sandbox Code Playgroud)
输出:
{
"x": -0.35112437558647797,
"y": -0.3823646436315567
}
Run Code Online (Sandbox Code Playgroud)
正如您在此图中看到的,结果不是椭圆上的顶点:
如何根据角度找到椭圆中一点的坐标?
这样的椭圆只是一个圆,在 y 轴上缩放了 0.38 倍。
\n我用来理解答案的推理是反转缩放比例。
\n如果要将整个绘图 y 轴缩放系数 1/0.38\xe2\x89\x882.63,则椭圆看起来像一个圆形。\n但是,蓝线的角度会有所不同。因为它的插槽不是 0.7/0.3,而是 2.63\xc3\x970.7/0.3。
\n这个角度,也是蓝线和椭圆的交点的角度。它是椭圆中交点的参数坐标。
\n由于该参数坐标不依赖于缩放比例,因此即使在当前 0.38 比例下它也保持不变
\n所以,结论是
\nlet angle = Math.atan((0.7/0.3)*a/b);\nlet x = a*Math.cos(angle);\nlet y = b*Math.sin(angle);\nRun Code Online (Sandbox Code Playgroud)\n示范:
\nlet a=1;\nlet b=0.38;\n\nlet canvas=document.getElementById(\'canvas\');\nlet ctx = canvas.getContext("2d");\nlet y0=120;\nlet x0=100;\nlet sc=150;\nctx.strokeStyle=\'#000000\';\n// Axis\nctx.beginPath(); \nctx.moveTo(0,y0);\nctx.lineTo(300,y0);\nctx.stroke();\nctx.moveTo(x0,0);\nctx.lineTo(x0,300);\nctx.stroke();\n// Ellipse\nctx.strokeStyle=\'#ff0000\';\nctx.beginPath();\nctx.moveTo(x0+sc*a,y0);\nctx.ellipse(x0,y0,sc*a, sc*b, 0, 0, 6.28);\nctx.stroke();\n// Blue line\nctx.strokeStyle=\'#0000ff\';\nctx.beginPath();\nctx.moveTo(x0,y0);\nctx.lineTo(x0+0.3*sc, y0-0.7*sc);\nctx.stroke();\n\n// Intersection coordinate. The next 3 lines are my answer\n// (all the lines before, and after the next 3, are just \n// what is needed for drawing)\nlet angle = Math.atan((0.7/0.3)*a/b);\nlet x = a*Math.cos(angle);\nlet y = b*Math.sin(angle);\n\n// Draw a yellow box at this point. It should intersect both ellipse and line\nctx.fillStyle=\'#888800\';\nctx.beginPath();\nctx.fillRect(x0+x*sc-5, y0-y*sc-5, 10, 10);Run Code Online (Sandbox Code Playgroud)\r\n<canvas id=canvas width=300 height=200>\n</canvas>Run Code Online (Sandbox Code Playgroud)\r\n现在,您可能会注意到它并没有严格回答您的问题标题中的问题。它不给出基于角度的椭圆中的点的坐标。因为angle这里不是蓝线的角度,而是如果我们缩放图表以使椭圆显示为圆形时它所具有的角度。
但从你的问题的正文,以及从你的代码来看,你真正的问题似乎是“根据线斜率 0.7/0.3 在椭圆中查找点”。
\n但如果你真的想从角度(蓝线的角度)找到该点,你只需使用它tan来获取斜率,然后再进行缩放并使用atan它来获取我们需要的真实角度。
所以
\nlet parametricAngle = Math.atan(Math.tan(inputAngle)*a/b);\nlet x=a*Math.cos(parametricAngle);\nlet y=b*Math.sin(parametricAngle);\nRun Code Online (Sandbox Code Playgroud)\n