dist

def draw
  background(200)
  fill(0)

  x1 = 10
  y1 = 90
  x2 = mouseX
  y2 = mouseY

  line(x1, y1, x2, y2)
  ellipse(x1, y1, 7, 7)
  ellipse(x2, y2, 7, 7)

  # d is the length of the line
  # the distance from point 1 to point 2.
  d = dist(x1, y1, x2, y2)

  push do
    translate((x1 + x2) / 2, (y1 + y2) / 2)
    rotate(atan2(y2 - y1, x2 - x1))
    text(format("%.1f", d), 0, -5)
  end
end

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

[p5.js] dist

概要

2点間の距離を計算します。

書式

r = dist(x1, y1, x2, y2)
r = dist(x1, y1, z1, x2, y2, z2)

引数

引数名内容備考オプションデフォルト値
x1最初の点の x座標
y1最初の点の y座標
z1最初の点の z座標
x22番目の点の x座標
y22番目の点の y座標
z22番目の点の z座標

戻値

r : 2点間の距離

備考

関連