P5Vector.mag

概要

p5.jsリファレンス(参考情報)

[p5.js] mag

サンプル

def draw
  background(240)

  v0 = createVector(0, 0)
  v1 = createVector(mouseX, mouseY)
  drawArrow(v0, v1, 'black')

  noStroke
  text(format('vector length: %.2f', P5Vector.mag(v1)), 10, 70, 90, 30)
end

# draw an arrow for a vector at a given base position
def drawArrow(base, vec, myColor)
  push do
    stroke(myColor)
    strokeWeight(3)
    fill(myColor)
    translate(base.x, base.y)
    line(0, 0, vec.x, vec.y)
    rotate(vec.heading)
    arrowSize = 7
    translate(vec.mag - arrowSize, 0)
    triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0)
  end
end

構文

戻値