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 StraightLine {
  /**
   * コンストラクタ
   * @param {Point} _start_p 線分開始位置
   * @param {Vec} _v 開始位置からのベクトル
   */
  constructor (_start_p, _v) {
    this.start_p = _start_p;
    this.v = _v;
  }
}

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

// 変数定義
// 直線A
let line_start_p = new Point(400, 400);
let line_v = new Vec(-400, -400);
let lineA = new StraightLine(line_start_p, line_v);
// 円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) {
          lineA.v.x = mouseX - lineA.start_p.x;
          lineA.v.y = mouseY - lineA.start_p.y;
    }
  }
  // 衝突有無を判定
  let collision = circleColStraightLine(circleP, lineA);

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

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

  // ② 直線Aの正規化ベクトルを求める
  let vecALength = lineA.v.getLength();
  // ベクトルの長さが0の場合、線分APの長さで衝突判定
  if (vecALength == 0) {
    return vecAP.getLength() < circleP.r;
  }
  let nomVecA = new Vec(lineA.v.x / vecALength, lineA.v.y / vecALength);

  // ③ 正規化ベクトルA*線分APの外積で、PXの長さを求める
  let crossPX = nomVecA.crossVec(vecAP);
  
  // ④ 最短距離が円の半径より小さければ衝突と判定
  return Math.abs(crossPX) < circleP.r;
}
              
            
!
999px

Console