【发布时间】:2011-05-16 11:33:41
【问题描述】:
我一直在使用这个椭圆函数(我在 Wikipedia http://en.wikipedia.org/wiki/Ellipse 找到)在布局中绘制点。我一直在围绕椭圆绘制两到五个点,没有任何问题。该函数有一个名为“steps”的参数; 'steps' 参数设置要在椭圆周围绘制的点的数量。
这是主要问题:如果“步数”(要绘制的点数)等于数字 7、11、13 或 14,则会中断。我学三角学已经有几年了,所以基本上我被困住了。
第二个小问题:我的代码打印出所有点,但是当我复制/粘贴并删除无关代码以在此处发布时,它只打印出最后一个绘图点(不知道为什么)。
<html>
<head>
<script type="text/javascript">
var elipticalLayout=new Array();
for (i=0; i <36; i++){
elipticalLayout[i]=new Array(2);
}
/*
* This functions returns an array containing the specified
* number of 'steps' (points) to draw an ellipse.
*
* @param x {double} X coordinate
* @param y {double} Y coordinate
* @param a {double} Semimajor axis
* @param b {double} Semiminor axis
* @param angle {double} Angle of the ellipse
*
* Attribution: This function is from http://en.wikipedia.org/wiki/Ellipse
*/
function calculateEllipticalLayout(x, y, a, b, angle, steps) {
var points = [];
// Angle is given by Degree Value
var beta = -angle * (Math.PI / 180); //(Math.PI/180) converts Degree Value into Radians
var sinbeta = Math.sin(beta);
var cosbeta = Math.cos(beta);
for (var i = 0; i < 360; i += 360 / steps) //{
var alpha = i * (Math.PI / 180) ;
var sinalpha = Math.sin(alpha);
var cosalpha = Math.cos(alpha);
var X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta);
var Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta);
elipticalLayout[i/(360/steps)][0]=X;
elipticalLayout[i/(360/steps)][1]=Y;
}
</script>
</head>
<body>
<script type="text/javascript">
calculateEllipticalLayout(300, 300, 245, 125, 15, 15);
for (i=0; i<elipticalLayout.length; i++){
document.write(i + ", " + elipticalLayout[i][0] + ", " + elipticalLayout[i][1] + "<br>");
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript math trigonometry