[英]How to divide an ellipse to equal angle segments?
以下代碼正在創建 svg 路徑,以將圓分成 8 或 24 個部分,在圓的中心具有相同的角度。 該因子僅用於創建在外部矩形邊界處結束的路徑,因此我們最終在所有路徑的末端周圍都有一個矩形。
function get8Or24PiePath(is24Pie, pieChartKind, width, height, customAngle) {
let resultPath = '';
const maxIndex = is24Pie ? 12 : 4;
const circleAngleStep = (2 * Math.PI) / (maxIndex * 2);
const additionalIndexValue = (customAngle / 360) * (maxIndex * 2);
for (let index = 0; index < maxIndex; index++) {
if (!is24Pie || (index + 2) % 3 !== 0) {
const fromIndex = index + 0.5 + additionalIndexValue;
const toIndex = fromIndex + maxIndex;
const factorFrom =
pieChartKind === 'S' || pieChartKind === 'R'
? Math.sqrt(1.0) /
Math.max(
Math.abs(Math.cos(circleAngleStep * fromIndex)),
Math.abs(Math.sin(circleAngleStep * fromIndex)),
)
: 1;
const factorTo =
pieChartKind === 'S' || pieChartKind === 'R'
? Math.sqrt(1.0) /
Math.max(
Math.abs(Math.cos(circleAngleStep * toIndex)),
Math.abs(Math.sin(circleAngleStep * toIndex)),
)
: 1;
const xFrom =
factorFrom * Math.cos(circleAngleStep * fromIndex) * (width * 0.5) + width * 0.5;
const yFrom =
factorFrom * Math.sin(circleAngleStep * fromIndex) * (height * 0.5) + height * 0.5;
const xTo =
factorTo * Math.cos(circleAngleStep * toIndex) * (width * 0.5) + width * 0.5;
const yTo =
factorTo * Math.sin(circleAngleStep * toIndex) * (height * 0.5) + height * 0.5;
resultPath += `M ${xFrom} ${yFrom} L ${xTo} ${yTo} `;
}
}
return resultPath;
}
但是,如果我對橢圓使用相同的計算,它將拉伸橢圓中心的線條和角度不再相等。 我需要更改什么才能獲得所需的行為?
似乎你計算圓周點,然后擠壓坐標 - 是的,在這種情況下角度變得錯誤。
而是計算橢圓處的點( 此處相對於中心的極坐標形式)
ro(theta) = a * b / sqrt(a^2*sin^2(theta) + b^2*cos^2(theta))
x(theta) = ro * cos(theta)
y(theta) = ro * sin(theta)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.