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


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

p5.j​​s中p5.Table的setNum()方法用於將給定的Float值存儲到表的給定行和列。可以通過行ID指定行,並且可以通過其列ID或列名指定列。

用法:

setNum( row, column, value )

參數:此函數接受上述和以下所述的三個參數:

  • row:它是一個數字,表示該行的行ID。
  • column:它是一個字符串或數字,表示列的名稱或列的ID。
  • value:它是一個數字,用於指定必須存儲的值。

下麵的示例說明了p5.js中的setNum()方法:

例:



function setup() { 
  createCanvas(500, 300); 
  textSize(18); 
  
  text("Enter the index of the table" + 
       " to be modified", 20, 20); 
  
  rowInput = createInput(); 
  rowInput.size(30); 
  rowInput.position(30, 40); 
  
  colInput = createInput(); 
  colInput.size(30); 
  colInput.position(80, 40); 
  
  setBtn = 
    createButton("Modify Given Row and Column"); 
  setBtn.position(30, 80); 
  setBtn.mouseClicked(modifyTableData); 
  
  // Create the table 
  table = new p5.Table(); 
  
  // Add 5 columns and rows to the table 
  for (let i = 0; i < 4; i++) { 
    table.addColumn("Column " + i); 
    table.addRow(); 
  } 
  
  setTableData(); 
} 
  
function modifyTableData() { 
    
  givenRow = int(rowInput.value()); 
  givenCol = int(colInput.value()); 
  
  if (givenRow < table.getRowCount() &&  
      givenCol < table.getColumnCount()) 
  
    // Set a number value to the given 
    // row and column 
    table.setNum(givenRow, givenCol, -999); 
  
  showTable(); 
} 
  
function setTableData() { 
  for (let r = 0; r < 4; r++) { 
    for (let c = 0; c < 4; c++) { 
  
      // Set the value at the given 
      // column and row of the table 
      table.set(r, c, r + " " + c); 
    } 
  } 
  
  showTable(); 
} 
  
function showTable() { 
  clear(); 
  text("The value is modified using" + 
       " the setNum() method", 20, 20); 
  
  // Show all the columns present 
  for (let c = 0; c < 4; c++) { 
    text(table.columns, 
         20 + 100 * c, 120); 
  } 
  
  // Show all the rows currently 
  // present in the table 
  for (let r = 0; r < 4; r++) { 
    for (let c = 0; c < 4; c++) { 
      text(table.getString(r, c), 
           20 + 100 * c, 
           140 + 20 * r); 
    } 
  } 
}

輸出:
setNum-ex

在線編輯: https://editor.p5js.org/

環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

參考: https://p5js.org/reference/#/p5.Table/setNum




相關用法


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