當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


p5.js max()用法及代碼示例


p5.j​​s中的max()函數用於獲取數字序列或兩個給定數字中的最大值。 max()函數接受任意數量的參數或數組。

用法:

max(a, b)

或者


max(arr)

參數:max(a,b)函數接受數量不同的兩個參數,並進行比較以獲得其中的最大值。 max(arr)函數接受數字數組並返回最大值。

返回值:它返回數字之間的最大值。

以下示例程序旨在說明p5.js中的max()函數:

例:本示例使用max()函數獲取最大值。

function setup() {  
   
    // Create Canvas of given size 
    createCanvas(450, 230);  
}  
   
function draw() {  
       
    // Set the background color  
    background(220);  
       
    // Call to max() function  
    let u = max(1, 3); 
    let v = max(1, 1); 
    let w = max(5, 9); 
    let x = max(2, 3.9); 
    let y = max(1.5, 3.2); 
      
    let arr = [1, 5, 3, 8, 6, 9]; 
    let z = max(arr); 
        
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting minimum value     
    text("Maximum value between (1, 3) is:" + u, 50, 30); 
    text("Maximum value between (1, 1) is:" + v, 50, 50); 
    text("Maximum value between (5, 9) is:" + w, 50, 70); 
    text("Maximum value between (2, 3.9) is:" + x, 50, 90); 
    text("Maximum value between (1.5, 3.2) is:" + y, 50, 110); 
    text("Maximum value in an array is:" + z, 50, 130); 
            
} 

輸出:

參考: https://p5js.org/reference/#/p5/max



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | max() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。