給定一個角度,我想知道橢圓上頂點的坐標。橢圓中心在 x = 0 和 y = 0 上。半長軸為 x,值為 1。半短軸為 y,值為 0.38。角度是可變的。
我已經嘗試了這個 Stack Exchange 帖子上的所有內容,但沒有任何效果:
如何根據角度找到橢圓中一點的坐標?
uj5u.com熱心網友回復:
這樣的橢圓就是一個圓,在 y 軸上縮放了 0.38 倍。
我將用于理解答案的推理是反轉縮放比例。
如果將整個繪圖的 y 軸縮放 1/0.38≈2.63,則橢圓看起來像一個圓。但是,藍線的角度會有所不同。因為它的槽不是 0.7/0.3,而是 2.63×0.7/0.3。
而那個角度,也將是那條藍線和橢圓的點交點的角度。它是橢圓中交點的引數坐標。
由于該引數坐標不依賴于縮放比例,因此即使在當前 0.38 比例下它也保持不變
所以,結論是
let angle = Math.atan((0.7/0.3)*a/b);
let x = a*Math.cos(angle);
let y = b*Math.sin(angle);
示范:
let a=1;
let b=0.38;
let canvas=document.getElementById('canvas');
let ctx = canvas.getContext("2d");
let y0=120;
let x0=100;
let sc=150;
ctx.strokeStyle='#000000';
// Axis
ctx.beginPath();
ctx.moveTo(0,y0);
ctx.lineTo(300,y0);
ctx.stroke();
ctx.moveTo(x0,0);
ctx.lineTo(x0,300);
ctx.stroke();
// Ellipse
ctx.strokeStyle='#ff0000';
ctx.beginPath();
ctx.moveTo(x0 sc*a,y0);
ctx.ellipse(x0,y0,sc*a, sc*b, 0, 0, 6.28);
ctx.stroke();
// Blue line
ctx.strokeStyle='#0000ff';
ctx.beginPath();
ctx.moveTo(x0,y0);
ctx.lineTo(x0 0.3*sc, y0-0.7*sc);
ctx.stroke();
// Intersection coordinate. The next 3 lines are my answer
// (all the lines before, and after the next 3, are just
// what is needed for drawing)
let angle = Math.atan((0.7/0.3)*a/b);
let x = a*Math.cos(angle);
let y = b*Math.sin(angle);
// Draw a yellow box at this point. It should intersect both ellipse and line
ctx.fillStyle='#888800';
ctx.beginPath();
ctx.fillRect(x0 x*sc-5, y0-y*sc-5, 10, 10);
<canvas id=canvas width=300 height=200>
</canvas>
現在,您可能會注意到它并沒有嚴格回答問題標題中的問題。它不會根據角度給出橢圓中點的坐標。因為angle這里不是藍線的角度,而是如果我們縮放圖表時它會有的角度,所以橢圓顯示為圓形。
但是從你的問題的主體,以及更多的代碼來看,你真正的問題似乎是“根據線斜率 0.7/0.3 在橢圓中找到點”。
但是如果你真的想從角度找到點(藍線的角度,你只需要使用tan來獲得斜率,然后縮放并使用atan來獲得我們需要的真實角度。
所以
let parametricAngle = Math.atan(Math.tan(inputAngle)*a/b);
let x=a*Math.cos(parametricAngle);
let y=b*Math.sin(parametricAngle);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537659.html
標籤:javascript数学
上一篇:沒有錯誤,但沒有輸出或數學域錯誤
