Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
canvas {
  display: block;
  border: solid 1px;
}

              
            
!

JS

              
                /**
 * 座標クラス
 */
class Point {

  /**
   * コンストラクタ
   * @param {number} _x X座標
   * @param {number} _y Y座標
   */
  constructor (_x, _y) {
    this.x = _x;
    this.y = _y;
  }

  /**
   * 引数の位置へ向かうベクトルを求める
   * @param {Point} p 処理対象位置
   * @returns 引数の位置へ向かうベクトル
   */
  getVecPoint(p) {
    return new Vec(p.x - this.x, p.y - this.y);
  }
}

/**
 * ベクトルクラス
 */
class Vec {

  /**
   * コンストラクタ
   * @param {number} _x X成分 
   * @param {number} _y Y成分 
   */
  constructor (_x, _y) {
    this.x = _x;
    this.y = _y;
  }

  /**
   * ベクトル同士の内積を求める
   * @param {Vec} v 処理対象ベクトル
   * @returns 内積結果
   */
  dotVec(v) {
    return this.x * v.x + this.y * v.y;
  }

  /**
   * ベクトル同士の外積を求める
   * @param {Vec} v 処理対象ベクトル
   * @returns 外積結果
   */
  crossVec(v) {
    return this.x * v.y - this.y * v.x;
  }

  /**
   * ベクトルの長さを求める
   * @returns ベクトルの長さ
   */
  getLength() {
    return Math.sqrt(this.x*this.x + this.y*this.y);
  }
}

/**
 * 線分クラス
 */
class Line {
  /**
   * コンストラクタ
   * @param {Point} _start_p 線分開始位置
   * @param {Point} _end_p   線分終了位置
   */
  constructor (_start_p, _end_p) {
    this.start_p = _start_p;
    this.end_p = _end_p;
    // ベクトルを設定
    this.v = new Vec(
      this.end_p.x - this.start_p.x,
      this.end_p.y - this.start_p.y
    );
  }
}

/**
 * 円クラス
 */
class Circle {
  /**
   * コンストラクタ
   * @param {Point} _p  位置
   * @param {number} _r 半径
   */
  constructor (_p, _r) {
    this.p = _p;
    this.r = _r;
  }
}

// 変数定義
// 線分AB
let line_start_p = new Point(300, 300);
let line_end_p = new Point(300, 100);
let lineAB = new Line(line_start_p, line_end_p);
// 円P
let circle_p = new Point(200, 200);
let circle_r = 50;
let circleP = new Circle(circle_p, circle_r);

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // マウスクリック時
  // マウス座標を設定
  if (mouseIsPressed == true) {
    if (0 <= mouseX && mouseX <= 400 && 
        0 <= mouseY && mouseY <= 400) {
      lineAB.end_p.x = mouseX;
      lineAB.end_p.y = mouseY;
      lineAB.v = new Vec(
        lineAB.end_p.x - lineAB.start_p.x,
        lineAB.end_p.y - lineAB.start_p.y
      );
    }
  }
  // 衝突有無を判定
  let collision = circleColLine(circleP, lineAB);

  // 図形描画
  background(255);
  fill(0);
  circle(lineAB.start_p.x, lineAB.start_p.y, 20);
  fill(230);
  circle(lineAB.end_p.x, lineAB.end_p.y, 20);
  line(lineAB.start_p.x, lineAB.start_p.y, lineAB.end_p.x, lineAB.end_p.y);
  fill(0);
  circle(circleP.p.x, circleP.p.y, circleP.r*2);
  
  // テキスト描画
  fill(0);
  textSize(15);
  text(collision ? "[衝突!!]" : "[衝突なし]", 300, 50);
}

/**
 * 円と線分の衝突判定処理
 * (引数を円P、線分ABとして計算する)
 * @param {Circle} circleP 円P
 * @param {Line} lineAB 線分AB
 * @returns true:衝突している false:衝突していない
 */
function circleColLine(circleP, lineAB) {
  // ① 線分AP、線分BPのベクトルを求める
  let vecAP = lineAB.start_p.getVecPoint(circleP.p);
  let vecBP = lineAB.end_p.getVecPoint(circleP.p);

  // ② 線分ABと線分APの内積・外積を線分ABの長さで除算することで
  // 線分AX、PXの長さを求める
  let dotAX = lineAB.v.dotVec(vecAP) / lineAB.v.getLength();
  let crossPX = lineAB.v.crossVec(vecAP) / lineAB.v.getLength();
  
  // ③ 最短距離を設定
  // 基本は線分PXの長さを設定
  let distance = Math.abs(crossPX);
  if (dotAX < 0) {
    // 例外1:線分AXと逆方向に最短座標がある場合 -> 線分APの長さを設定
    distance = vecAP.getLength();
  } else if (dotAX > lineAB.v.getLength()) {
    // 例外2:線分ABよりも先に最短座標がある場合 -> 線分BPの長さを設定
    distance = vecBP.getLength();
  }
  // ④ 最短距離が円の半径より小さければ衝突と判定
  return distance < circleP.r;
}
              
            
!
999px

Console