如何以特定程度获得JavaScript中椭圆的边缘坐标? 我正在尝试确定JavaScript中椭圆SVG的边缘。我现在拥有的是椭圆的中心坐标,矩形坐标,顶部/左/右/底部边缘o ...

问题描述 投票:0回答:2

椭圆形的正常参数方程可能会有所帮助: enter image description here

javascript jquery math geometry ellipse
2个回答
9
投票
var e = document.querySelector('ellipse'), p = document.querySelector('circle'); var rx = +e.getAttribute('rx'), ry = +e.getAttribute('ry'); var angle = 0; const spin = () => { var t = Math.tan(angle / 360 * Math.PI); var px = rx * (1 - t * t) / (1 + t * t), py = ry * 2 * t / (1 + t * t); p.setAttribute('cx', px); p.setAttribute('cy', py); angle = ++angle % 360; requestAnimationFrame(spin) } requestAnimationFrame(spin)

<svg viewBox="-105 -55 210 110" height="200" width="400"> <ellipse stroke="#000" fill="#fff" cx="0" cy="0" rx="100" ry="50"/> <circle fill="red" r="3"/> </svg>

为您的观点A,B,C,D:

var e = document.querySelector('ellipse'), a = document.querySelector('#a'), b = document.querySelector('#b'), c = document.querySelector('#c'), d = document.querySelector('#d'); var rx = +e.getAttribute('rx'), ry = +e.getAttribute('ry'); [a, b, c, d].forEach((p, i) => { var t = Math.tan(i * Math.PI / 4 + Math.atan(2 * ry / rx) / 2); var px = rx * (1 - t * t) / (1 + t * t), py = ry * 2 * t / (1 + t * t); console.log(p.id + '(' + px + ', ' + py + ')'); p.setAttribute('cx', px); p.setAttribute('cy', py); })

<svg viewBox="-105 -55 210 110" height="200" width="400"> <rect stroke="#000" fill="#fff" x="-100" y="-50" width="200" height="100"/> <path stroke="#000" d="M-100-50L100 50zM-100 50L100-50z"/> <ellipse stroke="#000" fill="none" cx="0" cy="0" rx="100" ry="50"/> <circle id="a" fill="red" r="3"/> <circle id="b" fill="red" r="3"/> <circle id="d" fill="red" r="3"/> <circle id="c" fill="red" r="3"/> </svg>

LET计算得出点
A
用坐标
A.x, A.y
。为此,让我们首先假设椭圆的中心具有坐标。为了在结尾获取一般案例,最终结果将仅由

O


1
投票
0, 0

O.x, O.y
的线被描述为
O
要简化下面的符号,让我们表示。椭圆本身被定义为满足的一组点:
R2
为了获得十字路口,我们可以将第一个方程式替换为第二个方程。这产生了:

y = (R2.y / R2.x) * x

(由于相交点位于第一个象限,我们知道
a := R2.y / R2.x
具有正符号):

(y/yd)**2 + (x/xd)**2 = 1

最终,要解决椭圆中心的非零偏移,我们可以添加相应的偏移量。因此:

x**2 * ( (a/yd)**2 + 1/xd**2 ) = 1

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.