P5Vector.dist

def draw 
  background(240)
  v0 = createVector(0, 0)
  v1 = createVector(70, 50)
  drawArrow(v0, v1, 'red')
  v2 = createVector(mouseX, mouseY)
  drawArrow(v0, v2, 'blue')
  noStroke
  text(format("distance between vectors: %.2f",
               P5Vector.dist(v2, v1)), 5, 50, 95, 50)
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] dist

概要

2つのベクトル間の距離を取得します。

書式

P5Vector.dist(v1, v2)

引数

引数名内容備考オプションデフォルト値
v1ベクトル1p5.Vectorオブジェクト
v2ベクトル2p5.Vectorオブジェクト

戻値

2点間の距離

備考

関連

(p5.Vector.obj).dist