Permalink
Browse files

Added code for computing the smallest enclosing disk for a set of poi…

…nts and added an usage (enclosingdisk.html)
  • Loading branch information...
1 parent 79cbbd4 commit fea5ce858b90c6e8c49043299b1c16793d88106a @bjwbell committed Oct 4, 2012
Showing with 432 additions and 226 deletions.
  1. +1 −0 .#hulltest.js
  2. +1 −0 .#turntest.js
  3. +10 −0 enclosingdisk.html
  4. +88 −0 enclosingdisk.js
  5. +332 −221 geometry.js
  6. +0 −5 turn.html
View
View
View
@@ -0,0 +1,10 @@
+<html><head>
+<title>Enclosing Disk Example</title>
+<script type="text/javascript" src="geometry.js"></script>
+<script type="text/javascript" src="enclosingdisk.js"></script>
+</head>
+<body>
+
+ <canvas id="canvas" width="800" height="600"><p>Fallback content goes here.</p></canvas>
+</body>
+</html>
View
@@ -0,0 +1,88 @@
+var width = 800;
+var height = 600;
+var points = new Array();
+points.push([100, 100]);
+points.push([200, 200]);
+points.push([300, 50]);
+//points.push([2, 0]);
+
+function getCanvas() {
+ return document.getElementById('canvas');
+}
+
+window.addEventListener('load', function() {
+ var canvas = getCanvas();
+ if(canvas && canvas.getContext) {
+ initCanvas();
+ }
+ canvas.addEventListener('mousemove', mouseMove, false);
+ canvas.addEventListener('mousedown', mouseDown, false);
+
+
+}, false);
+
+function getEventXCoord(ev){
+ if(ev.layerX){ //Firefox
+ return ev.layerX;
+ }
+ return ev.offsetX; //Opera
+}
+function getEventYCoord(ev){
+ if(ev.layerY){ //Firefox
+ return ev.layerY;
+ }
+ return ev.offsetY; //Opera
+}
+
+function initCanvas(){
+ var canvas = getCanvas();
+ context = canvas.getContext('2d');
+ context.fillStyle = '#fff';
+ context.fillRect(0, 0, width, height);
+ context.fillStyle = '#fff'; //white
+ context.strokeStyle = '#000'; //black
+ context.lineWidth = 4;
+ drawEnclosingDisk();
+}
+
+function drawEnclosingDisk() {
+ var canvas = getCanvas();
+ context = canvas.getContext('2d');
+ context.fillStyle = '#fff';
+ context.fillRect(0, 0, width, height);
+
+ for(var i = 0; i < points.length; i++){
+ var p = new Vector(points[i][0], points[i][1]);
+ p.color = '#000';
+ p.size = 10;
+ p.draw(canvas);
+ }
+ // compute the smallest disk enclosing the points
+ var disk = enclosingDisk(points)
+ var x = disk[0][0];
+ var y = disk[0][1];
+ var r = disk[1];
+ context.beginPath();
+ context.arc(x, 600 - y, r, 0, 2 * Math.PI);
+ context.stroke();
+}
+
+
+function mouseDown(ev){
+ var x = getEventXCoord(ev);
+ var y = getEventYCoord(ev);
+ y = 600 - y;
+ points.push([x, y]);
+ drawEnclosingDisk();
+}
+
+function getLine2(){
+ return new Segment(new Vector(0, 100), new Vector(150, 50));
+}
+
+var debug = 1;
+
+function mouseMove(ev) {
+};
+
+
Oops, something went wrong.

0 comments on commit fea5ce8

Please sign in to comment.