[英]Why html5 canvas tag uses PI value to draw circle?
var c = document.getElementById("myCanvas");
var cir = c.getContext("2d");
cir.beginPath();
cir.arc(100, 100, 50, 0, 2* Math.PI );
cir.stroke();
or 要么
var c = document.getElementById("myCanvas");
var cir = c.getContext("2d");
cir.beginPath();
cir.arc(100, 100, 50, 0, 6.14);
cir.stroke();
This two code can draw a full circle so in this why we are using pi value in this ? 这两个代码可以绘制一个完整的圆,所以我们在这里使用pi值为什么呢?
Angles can be measured with a variety of different measures. 可以使用各种不同的度量来测量角度。 The most common are: 最常见的是:
360
degrees equals one full circle 度 ,其中360
度等于一个完整的圆 π
(Greek letter Pi) — which is roughly 3.14159265 — is one half circle, and a full circle is 2π
. 数学常数π
(希腊字母Pi) - 大致为3.14159265 - 的弧度是半圆,整圆是2π
。 The arc(x, y, radius, startAngle, endAngle, anticlockwise)
function takes angles in radians, so passing the values startAngle=0
and endAngle=2*Math.PI
will draw a complete circle with radius radius
from point (x,y)
. arc(x, y, radius, startAngle, endAngle, anticlockwise)
函数以弧度为单位,因此传递值startAngle=0
和endAngle=2*Math.PI
将从点(x,y)
绘制一个半径为radius
的完整圆(x,y)
。
Edit: The line of code that you posted: 编辑:您发布的代码行:
cir.arc(100, 100, 50, 0, 6.14);
Will draw most of a circle, as 6.14 < 2π
. 将绘制大部分圆圈,如6.14 < 2π
。 2π is approximately 6.283185, so the arc will be about 98% of a full circle. 2π约为6.283185,因此弧度约为整圆的98%。
Value of Math.PI: 3.141592653589793
is more accurate. Math.PI: 3.141592653589793
值Math.PI: 3.141592653589793
更准确。 And cir.arc(100, 100, 50, 0, 6.14);
而cir.arc(100, 100, 50, 0, 6.14);
does not make full circle. 没有完整的圆圈。
The second code does not draw a full circle: there is a gap on the right. 第二个代码没有画出一个完整的圆圈:右边有一个间隙。 The reason is that 6.14 is less than a full circle (in radians), which is 2π, about 6.28. 原因是6.14小于整圆(以弧度表示),即2π,约为6.28。 It is common to use the property Math.PI
for π in JavaScript, since it gives a very accurate approximation (the most accurate approximation achievable using a double precision floating point number in the system). 在JavaScript中使用属性Math.PI
作为π是很常见的,因为它提供了非常精确的近似(使用系统中的双精度浮点数可实现的最精确的近似)。
PS This is not about the canvas
tag but about JavaScript code that draws on a canvas specified by a canvas
element. PS这不是关于canvas
标签,而是关于在canvas
元素指定的canvas
上绘制的JavaScript代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.