简体   繁体   English

为什么html5 canvas标签使用PI值绘制圆圈?

[英]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: 最常见的是:

  • Degrees , where 360 degrees equals one full circle ,其中360度等于一个完整的圆
  • Radians , where the mathematical constant π (Greek letter Pi) — which is roughly 3.14159265 — is one half circle, and a full circle is . 数学常数π (希腊字母Pi) - 大致为3.14159265 - 的弧度是半圆,整圆是

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=0endAngle=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%。

Its using Radians as the unit of measure to describe the angle of the arc. 它使用Radians作为测量单位来描述弧的角度。 This is very common in programming languages, especially in graphics programming. 这在编程语言中非常常见,尤其是在图形编程中。 A full circle, 360 degrees, is equal to 2 * PI Radians. 360度的整圆等于2 * PI Radians。

Value of Math.PI: 3.141592653589793 is more accurate. Math.PI: 3.141592653589793Math.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM