Algorithm "NoObtuse" in javascript -


algorithm "noobtuse" in javascript. have implement algo below in canvas. user put set of points , click on button call function "noobtuse" , have draw graph (see image).

how can ? no obtuse algoritm

edit: change code informations of "mbo" don't need, nextpoint not right 1 (neither in cw nor in ccw).

my code:

class point {     constructor(x, y) {         this.x = x;         this.y = y;         this.col = 'red';     }     drawdot() {         fill(this.col);         ellipse(this.x, this.y, 8, 8);     }     tagnotextreme() {         this.col = 'blue';     }     setcolor(color) {         this.col = color;     } }  var points;  var vertices = [];  var rightmost= null; var tableau;  var angle1; var angle2; var ; var b; var p1 ; var indice;  var pointsize= 0; var canvas; var button_ch; var button_clear;  var isenter;  var times_clicked = 0;  function setup() {     canvas = createcanvas(500, 500);     background(255);     pointsize = 8;     points = [];     tableau = [];     rightmost = {x: 0, y:0};     = {x: 0, y:0};     b = {x: 0, y:0};     p1 = {x: 0, y:0};     indice = 2;     isenter = -1;     angle1 = 0;     angle2 = 0;     canvas.parent('candiv');    }   function draw() { }   function contient(point, tableau){     var oui = -1;     (var = 0; < tableau.length;i++){         if (point == tableau[i]){              oui = 1;         }     }     return oui; }  function noobtuse(){     p1 = rightmost;     angle1 = 0;     angle2 = 0;     = points[0];     (var k=0; k < points.length; k++){         angle = math.atan2(points[k].y - rightmost.y, points[k].x - rightmost.x);         if (angle < angle1){             angle1 = angle;             = points[k];         }     }     tableau[0] = p1;     tableau[1] = a;     var flag = -1;     var n = points.length-2;     var i;      (i = 0; <= 0; i++){         if (contient(points[i],tableau) == -1){             b = nextpoint(points[i], a, flag);             if (math.degrees(find_angle(points[i],a,b)) <= 90 ){                 points[i+1] = a;                 = b;             } else {                 points[i+1] = b;                 flag = -flag;             }             tableau[indice] = points[i];             indice = indice+1;             // ...         } else{             if(i == 0){                 strokeweight(2);                 line(p1.x, p1.y, a.x, a.y);             }         }     }     points[points.length - 1] = a;  }  math.degrees = function(radians) {   return radians * 180 / math.pi; };  function find_angle(a,b,c) {     var ab = math.sqrt(math.pow(b.x-a.x,2)+ math.pow(b.y-a.y,2));         var bc = math.sqrt(math.pow(b.x-c.x,2)+ math.pow(b.y-c.y,2));      var ac = math.sqrt(math.pow(c.x-a.x,2)+ math.pow(c.y-a.y,2));     return math.acos((bc*bc+ab*ab-ac*ac)/(2*bc*ab)); }  function nextpoint(pi, a, flag){     /* : let ρ ray attached , continuing directed segment pia*/      fill('blue');     strokeweight(2);     line(pi.x, pi.y, a.x, a.y);     b = a; /* : let b first point encountered ρ when rotating around in direction indicated flag*/     return b; }   function mousepressed() {     if(isenter == -1){         newpointn = new point(mousex,mousey);     if (rightmost && (rightmost.x < newpointn.x)) {         rightmost = newpointn;     }      points.push(newpointn);     newpointn.drawdot();     }else{         setup();     } }  function keypressed() {     if (keycode == 82) {         setup();     } else if (keycode == enter){           noobtuse();         isenter = 1;     } else if (keycode == 77){         rightmost.setcolor('green');         rightmost.drawdot();     }  } 

to first point rightmost, choose 1 smallest value of

angle = math.atan2(p[i].y - rightmost.y, p[i].x - rightmost.x) 

to next point goes after current , b points in ccw order, can compare angles a-b-c, calculated through atan2, cross-product , dot product of vectors.

angle = math.atan2(cross(ab, bc), dot(ab, bc)) 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -