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/0.10.2/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/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>
    <div id="ui_container"></div>
  </body>
</html>

              
            
!

CSS

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

JS

              
                
// *** ベクトルクラス ***
class Vec3 {
  constructor(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  };
}

// *** 位置クラス ***
class Point {
  constructor(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  };
}

// *** 座標ベクトル計算 ***
// 座標-座標
function subPoint(p1, p2) {
  return new Vec3(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z);
}
// ベクトル+ベクトル
function addVec(v1, v2) {
  return new Vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}

// ベクトル-ベクトル
function subVec(v1, v2) {
  return new Vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}

// ベクトル*スカラー
function mulVec(v, s) {
  return new Vec3(v.x * s, v.y * s, v.z *s);
}

// ベクトルの正規化
function normalize(v) {
  let l = sqrt(v.x**2 + v.y**2 + v.z**2);
  return new Vec3(v.x/l, v.y/l, v.z/l);
}

// ベクトルの外積
function crossVec(v1, v2) {
  let cross_v = new Vec3();
  cross_v.x = v1.y * v2.z - v1.z * v2.y;
  cross_v.y = v1.z * v2.x - v1.x * v2.z;
  cross_v.z = v1.x * v2.y - v1.y * v2.x;
  return cross_v;
}

// ベクトル描画処理
function drawVecLine(vec, start_p) {
  end_p_x = start_p.x + vec.x;
  end_p_y = start_p.y + vec.y;
  end_p_z = start_p.z + vec.z;
  return line(start_p.x, start_p.y, start_p.z, end_p_x, end_p_y, end_p_z);
}

/**
 * 平面に対する座標の表裏判定
 * 平面の方程式に当てはめて判定を行う
 * @param {Point} check_p チェックする座標
 * @param {Vec3}  cross_v 平面の法線
 * @param {Point} plane_p 平面座標(任意の一点)
 * @return {boolean} 表裏判定結果 true:表 false:裏
 */
function checkFrontPlane(check_p, cross_v, plane_p) {
  // チェック対象座標 - 平面の座標
  sub_x = check_p.x - plane_p.x;
  sub_y = check_p.y - plane_p.y;
  sub_z = check_p.z - plane_p.z;
  // 上記で求めたベクトルを法線の各要素に掛けて加算
  check_value = cross_v.x * sub_x + cross_v.y * sub_y + cross_v.z * sub_z;
  // 0以上の場合:表(0と同値の場合も表とみなす)
  return check_value >= 0;
}

// *** 変数定義 ***
// 平面の座標
let plane_p1 = new Point(100, 50, 35);
let plane_p2 = new Point(100, -50, 0);
let plane_p3 = new Point(-100, -50, -35);
let plane_p4 = new Point(-100, 50, 0);
// スライダー値(初期値)
let p1_x = 0;
let p1_y = 10;
let p1_z = 30;
// ベクトル
let v1 = subPoint(plane_p1, plane_p2);
let v2 = subPoint(plane_p1, plane_p4);
let v3 = crossVec(v1, v2); // v1 と v2 の外積
// 判定する座標
let p1 = new Point(p1_x, p1_y, p1_z);
// 表裏判定結果
let bool_p1_front = checkFrontPlane(p1, v3, plane_p1);

// *** 3D表示用SKETCH ***
// 初期設定処理
function setup() {
  // CANVAS設定(WEBGLを使用)
  createCanvas(400, 400, WEBGL);
  // ノーマルマテリアルを適用
  normalMaterial();
}

// 描画処理
function draw() {
  // マウスコントロールを設定
  orbitControl();
  // 背景色設定
  background(200);
  // 回転処理
  rotateX(radians(75));
  rotateZ(radians(45));
  // 平面描画
  push();
  beginShape();
  vertex(plane_p1.x, plane_p1.y, plane_p1.z);
  vertex(plane_p2.x, plane_p2.y, plane_p2.z);
  vertex(plane_p3.x, plane_p3.y, plane_p3.z);
  vertex(plane_p4.x, plane_p4.y, plane_p4.z);
  endShape();
  // 線分設定
  strokeWeight(3);
  // 平面座標から2つのベクトルを求めて描画
  v1 = subPoint(plane_p1, plane_p2);
  v2 = subPoint(plane_p1, plane_p4);
  stroke(255, 0, 0);
  drawVecLine(v1, new Point(0, 0, 0));
  stroke(0, 255, 0);
  drawVecLine(v2, new Point(0, 0, 0));
  // 求めたベクトルの外積から平面の法線を求めて描画
  v3 = crossVec(v1, v2);
  stroke(0, 0, 255);
  drawVecLine(v3, new Point(0, 0, 0));
  pop();
  // 判定する座標(球)の描画
  push();
  p1 = new Point(p1_x, p1_y, p1_z);
  translate(p1.x, p1.y, p1.z);
  sphere(10);
  // 平面に対する座標の表裏判定
  bool_p1_front = checkFrontPlane(p1, v3, plane_p1);
  pop();
}

// *** UI表示用SKETCH ***
// WEBGL上で画面表示できないため別で作成
var ui_sketch = function(p) {

  // 初期設定処理
  p.setup = function() {
    p.createCanvas(400, 180);
    // p1スライダー
    p1x_slider = createSlider(-200.0, 200.0, p1_x);
    p1x_slider.position(230, 30);
    p1x_slider.mouseReleased(setP1x);
    p1y_slider = createSlider(-200.0, 200.0, p1_y);
    p1y_slider.position(230, 50);
    p1y_slider.mouseReleased(setP1y);
    p1z_slider = createSlider(-200.0, 200.0, p1_z);
    p1z_slider.position(230, 70);
    p1z_slider.mouseReleased(setP1z);
  };

  // 描画処理
  p.draw = function() {
    // 背景設定
    p.background(150);
    // テキスト描画
    p.fill(0);
    p.noStroke();
    p.textSize(15);
    p.fill(0, 0, 0);
    p.text("[p1] " + "(" + p1.x + ", " + p1.y + ", " + p1.z + ")", 230, 30);
    p.text("[表裏判定] " + (bool_p1_front ? "表" : "裏"), 230, 110);
    p.fill(0, 0, 255);
    p.text("[外積] " + "(" + v3.x + ", " + v3.y + ", " + v3.z + ")", 30, 30);
    p.text("(v1 × v2)", 30, 50);
  };

  // *** スライダー値設定 ***
  function setP1x() {
    p1_x = p1x_slider.value();
  }
  function setP1y() {
    p1_y = p1y_slider.value();
  }
  function setP1z() {
    p1_z = p1z_slider.value();
  }
};
new p5(ui_sketch, "ui_container");
              
            
!
999px

Console