The acos() function in p5.js is used to calculate the arc cosine value. The domain of this function is -1 to +1 and the range is 0 to 3.14.
Syntax:
acos(c)
Parameters: This function accepts single parameter c which stores the value in between -1 to 1.
Below programs illustrate the acos() function in p5.js:
Example 1: This example uses acos() function to calculate arc cosine value.
function setup() {
// Create Canvas of size 380*80
createCanvas(380, 80);
} function draw() {
// Set the background color
background(220);
// Set the angle in radian
let angle = PI;
// Compute value of cos()
let c = cos(angle);
// Compute value of acos()
// It returns the value of PI
let ac = acos(c);
// Set the font size
textSize(16);
// Set the font color
fill(color( 'red' ));
// Display result
text( "Value of acos is : " + ac, 50, 30);
} |
Output:
Example 2: This example uses acos() function to calculate arc cosine value.
function setup() {
// Create Canvas of given size
createCanvas(380, 80); } function draw() {
// Set the background color
background(220);
let c = 0.5;
// Use acos() function to calculate
// arc cosine value
let ac = acos(c);
// Set font size
textSize(16);
// Set font color
fill(color( 'red' ));
// Display result
text( "Value of acos is : " + ac, 50, 30);
} |
Output:
Reference: https://p5js.org/reference/#/p5/acos