画圆

根据割圆法.看下图

根据割圆法画圆。
假设横坐标从0开始逐渐增加到直径长度。
假设半径r=100,O的坐标是100,100。假设C的横坐标是x,纵坐标就是r- Math.sqrt(r*r-(x-r)*(x-r))

<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
</style>
</head>
<body>
<canvas id="myCanvas" width="480" height="300" style="border:1px
solid #c3c3c3;margin:10px;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">

var c = document.getElementById("myCanvas");
var cxt = c.getContext("2d");
var r = 100;
cxt.moveTo(0, r);
for(var i=0;i<=2*r;i++)
cxt.lineTo(i,r-Math.sqrt(r*r-(r-i)*(r-i)))
cxt.moveTo(0,r);
for(var i=0;i<=2*r;i++)
cxt.lineTo(i,r+Math.sqrt(r*r-(r-i)*(r-i)))
cxt.stroke();

</script>

</body>
</html>


根据隔圆法求圆周率PI。

根据上图,圆的面积 S = PI*r*r.假设r=1.可知 PI=S.假设圆内接了正n边形,当n趋于无穷大时,圆的面积就越精确
根据上图,正4边形的面积是4个三角形,正8边形的面积是正4边形+4个三角形,正16边形的面积是正8边形+8个三角形
假设三角形的底边是l n,高是h n
我们知道四边形的面积S4 = 4 * l4 * h4 ; h4=l4=CF=Math.sqrt(2)/2;
S8= S4 + 8/2*l8*h8 ;l8=CF,h8=1-CF
s16=s8+ 16/2*l16*h16; l16=CD=Math.sqrt(l8*l8+h8*h8)/2;h16=AD=1-Math.sqrt(1-l16*l16)
S32=s16+l32*h32;l32=Math.sqrt(l16*l16+h16*h16)/2;

            //正八边形的面积为s,l是半个三角形底边长,h是三角形高,x是正几边形,temp是x/2个三角形的大小
var t=Math.sqrt(2)/2,l=t,h=1-l,s=4*t*t+4*t*(1-t),x=8,n=20,temp;
console.log(s)
for(var i=2;i<=n;i++){
l=Math.sqrt(l*l+h*h)/2;
h=1-Math.sqrt(1-l*l);
temp=x*l*h;
s=s+temp;
x=x*2;
//console.log('正'+x+'边形的面积是'+s)
}





原文地址:https://www.cnblogs.com/lunalord/p/2263240.html