您需要使用的部分功能,輸入弧度到COS和罪取代「+ sin(...)
」;因此請將您得到的值作爲圓的四分之一或一半,並在中心點的軸上反射以獲得完整的圓。
這就是說JavaScript的罪惡和cos不太挑剔,所以你必須把你的弧度減半了;我把它寫成:
function circle(radius, steps, centerX, centerY){
var xValues = [centerX];
var yValues = [centerY];
var table="<tr><th>Step</th><th>X</th><th>Y</th></tr>";
var ctx = document.getElementById("canvas").getContext("2d");
ctx.fillStyle = "red"
ctx.beginPath();
for (var i = 0; i <= steps; i++) {
var radian = (2*Math.PI) * (i/steps);
xValues[i+1] = centerX + radius * Math.cos(radian);
yValues[i+1] = centerY + radius * Math.sin(radian);
if(0==i){ctx.moveTo(xValues[i+1],yValues[i+1]);}else{ctx.lineTo(xValues[i+1],yValues[i+1]);}
table += "<tr><td>" + i + "</td><td>" + xValues[i+1] + "</td><td>" + yValues[i+1] + "</td></tr>";
}
ctx.fill();
return table;
}
document.body.innerHTML="<canvas id=\"canvas\" width=\"300\" height=\"300\"></canvas><table id=\"table\"/>";
document.getElementById("table").innerHTML+=circle(150,15,150,150);
我認爲不管什麼原因,你想xValues [0]和yValues [0]是的centerX和centerY。我無法弄清楚你爲什麼要這樣做,因爲它們已經將值傳遞給了函數。
另外,我會首先將相位計算存儲在局部變量中:`var phase = 2 * Math.PI * i/steps;` – 2010-06-03 20:58:41