(p5vector).magSq

(別名: (p5vector).mag_sq)

def draw 
  background(240)
  v0 = createVector(0, 0)
  v1 = createVector(mouseX, mouseY)
  drawArrow(v0, v1, 'black')
  noStroke
  text(format("vector length squared: %.2f", v1.magSq), 10, 45, 90, 55)
end

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.obj).magSq

概要

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

書式

r = obj.magSq

引数

なし

戻値

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

備考

・「(x * x) + (y * y) + (z * z)」の値と等価です。

関連

P5Vector.magSq