P5Vector.magSq

(別名: P5Vector.mag_sq, p5.Vector.magSq, p5.Vector.mag_sq)

def draw
  background(240)
  v0 = createVector(0, 0)
  v1 = createVector(mouseX, mouseY)
  drawArrow(v0, v1, 'black')
  noStroke
  text(format('vector length: %.2f', P5Vector.magSq(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

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

[p5.js] p5.Vector.magSq

概要

与えられたベクトルの大きさ(長さ)の2乗の値を取得します。

書式

r = P5Vector.magSq(v)

引数

引数名内容備考オプションデフォルト値
vベクトルp5vectorオブジェクト

戻値

r : ベクトルの大きさ(長さ)の2乗の値

備考

関連

(p5vector).magSq