我正在尝试用 Javascript 确定椭圆 SVG 的边缘。我现在拥有的是椭圆的中心坐标,它上面的矩形坐标,以及椭圆的上/左/右/下边缘,但是我如何确定椭圆的 A、B、C、D 点坐标在 Javascript 中?
最佳答案
Rational parametric equation椭圆的可能有帮助:
var e = document.querySelector('ellipse'),
p = document.querySelector('circle');
var rx = +e.getAttribute('rx'),
ry = +e.getAttribute('ry');
var angle = 0;
const spin = () => {
angle *= angle !== 360;
var t = Math.tan(angle++ / 360 * Math.PI);
var px = rx * (1 - t ** 2) / (1 + t ** 2),
py = ry * 2 * t / (1 + t ** 2);
p.setAttribute('cx', px);
p.setAttribute('cy', py);
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 ** 2) / (1 + t ** 2),
py = ry * 2 * t / (1 + t ** 2);
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>
关于javascript - 如何在特定度数下获取Javascript中椭圆的边缘坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51660456/