我一直在使用这个椭圆函数(我发现在维基百科http://en.wikipedia.org/wiki/Ellipse)用于在布局中绘制点。我一直在用椭圆形绘制两到五个点,没有任何问题。该函数有一个名为'steps'的参数; 'steps'参数设置椭圆周围绘制点的数量。JavaScript和三角/椭圆
以下是主要问题:如果'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>
我会考虑一下;谢谢。关于阵列,阵列反复使用以布置具有不同数量点的不同椭圆。我一直在追踪步数(点数),所以数组大于实数点的数量并不是问题(虽然我意识到这不是一个优化解决方案,但我不相信数组的大小是一个问题)。 – 2011-05-16 01:56:23
修复它。谢谢。 – 2011-05-16 02:08:30