OBB

Hw 5 option 3

HTML

<div id="info">OBB 2D: Option 3
  <button id="bttn">clear</button>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js">


</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeric/1.2.6/numeric.min.js"></script>

CSS

#info {
  position: absolute;
  top: 0px;
  width: 100%;
  padding: 10px;
  text-align: center;
  color: #ff8800
}

body {
  overflow: hidden;
}

JavaScript

var camera, scene, renderer;
var mouse = new THREE.Vector2();
var dataPoints, mesh, obbLine;

init();
animate();

$("#bttn").click(function() {
  scene.remove(dataPoints);
  scene.remove(obbLine);
  dataPoints = new THREE.Object3D();
  scene.add(dataPoints);
});


function init() {
  scene = new THREE.Scene();

  camera = new THREE.OrthographicCamera(-50, 50, 50, -50, -10, 10);
  camera.position.z = 10;
  scene.add(camera);

  ////////////////////////////////////////////
  dataPoints = new THREE.Object3D();
  scene.add(dataPoints);

  mesh = new THREE.Mesh(new THREE.CircleGeometry(1, 10), new THREE.MeshBasicMaterial());
  //////////////////////////////////////////////////////////

  var gridXZ = new THREE.GridHelper(50, 10);
  gridXZ.setColors(new THREE.Color(0xff00ff), new THREE.Color(0xffffff));
  gridXZ.rotation.x = Math.PI / 2;
  //scene.add(gridXZ);

  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(0x888888);

  document.body.appendChild(renderer.domElement);
  window.addEventListener('resize', onWindowResize, false);
  window.addEventListener('mousedown', onDocumentMouseDown, false);
}

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}

function onDocumentMouseDown(event) {
  event.preventDefault();
  // NDC: [-1,1]x[-1,1]
  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

  console.log(mouse.x + ', ' + mouse.y);

  if (event.button === 0) { // left button
    if (obbLine) scene.remove(obbLine);
    var m = mesh.clone();
    dataPoints.add(m);
    m.position.set(mouse.x * 50, mouse.y * 50, 0);

    var obb = findOBB(dataPoints);
    obbLine = drawOBB(obb);
    scene.add(obbLine);
  }
}

function animate() {
  requestAnimationFrame(animate);
  render();
}

function render() {
 ...