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


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

p5.j​​s中的bezierPoint()函數用於評估給定點處貝塞爾曲線的坐標。它獲取特定軸的貝塞爾曲線的坐標,並在點“t”處找到曲線的坐標,可以將其指定為參數。

通過一次使用該函數在曲線的x-coordinates和一次在y-coordinates上查找,然後將它們一起使用,可以找到貝塞爾曲線中點的完整位置。

用法:



bezierPoint( a, b, c, d, t )

參數:該函數接受上述和以下所述的五個參數:

  • a:它是一個數字,指定曲線的第一個點。
  • b:它是一個數字,指定曲線的第一個控製點。
  • c:它是一個數字,指定曲線的第二個控製點。
  • d:該數字指定曲線的第二點。
  • t:它是介於0和1之間的數字,用作曲線坐標的開始和結束之間的位置。

返回值:它返回一個數字,該數字指定給定位置的貝塞爾曲線的值。

以下示例說明了p5.js中的bezierPoint()函數:

範例1:

function setup() { 
  createCanvas(600, 300); 
  textSize(18); 
  
  bezierPointLocationSlider = createSlider(0, 1, 0, 0.1); 
  bezierPointLocationSlider.position(20, 40); 
} 
  
function draw() { 
  background("green"); 
  fill("black"); 
  text("Move the slider to change the location of the displayed bezier point", 10, 20); 
  
  // Get the required location of bezier 
  bezierPointLocationValue = bezierPointLocationSlider.value(); 
  
  let p1 = { x:50, y:250 }; 
  let p2 = { x:140, y:150 }; 
  let p3 = { x:400, y:150 }; 
  let p4 = { x:500, y:250 }; 
  
  noFill(); 
  // Draw bezier using bezier() 
  bezier(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y); 
  
  // Find the X and Y coordinate using the bezierPoint() function 
  let pointX = bezierPoint(p1.x, p2.x, p3.x, p4.x, bezierPointLocationValue); 
  let pointY = bezierPoint(p1.y, p2.y, p3.y, p4.y, bezierPointLocationValue); 
  fill("red"); 
  
  // Display it on the sketch 
  ellipse(pointX, pointY, 10, 10); 
}

輸出:

bezierPoint-currentPoint

範例2:

function setup() { 
  createCanvas(600, 300); 
  textSize(20); 
  
  maxPointsSlider = createSlider(2, 20, 10, 1); 
  maxPointsSlider.position(20, 40); 
} 
  
function draw() { 
  background("green"); 
  fill("black"); 
  text("Move the slider to change the number of intermediate points", 10, 20); 
  
  // Get the required location of bezier 
  maxPoints = maxPointsSlider.value(); 
  
  let p1 = { x:50, y:250 }; 
  let p2 = { x:140, y:150 }; 
  let p3 = { x:400, y:150 }; 
  let p4 = { x:500, y:250 }; 
  
  noFill(); 
  // Draw bezier using bezier() 
  bezier(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y); 
  
  for (let i = 0; i <= maxPoints; i++) { 
    let step = i / maxPoints; 
  
    // Find the X and Y coordinate using the bezierPoint() function 
    let pointX = bezierPoint(p1.x, p2.x, p3.x, p4.x, step); 
    let pointY = bezierPoint(p1.y, p2.y, p3.y, p4.y, step); 
    fill("red"); 
  
    // Display it on the sketch 
    ellipse(pointX, pointY, 8, 8); 
  } 
}

輸出:

bezierPoint-maxPoints

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

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

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




相關用法


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