From a7ff77364d6b3d15eaf38fb09d3a6a10711b7b90 Mon Sep 17 00:00:00 2001 From: bach Date: Mon, 12 Jun 2023 23:37:02 +0200 Subject: [PATCH] implemented Gift wrapping algorithm --- src/components/ConcernementMapItem.vue | 54 +++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/components/ConcernementMapItem.vue b/src/components/ConcernementMapItem.vue index 4142fc0..1536cd6 100644 --- a/src/components/ConcernementMapItem.vue +++ b/src/components/ConcernementMapItem.vue @@ -88,7 +88,8 @@ export default { // } this.parsePoints() - this.getSalientPoints() + // this.getSalientPoints() + this.getJarvisEnvelopeConvexe() if (this.salientPoints.length > 3) { // do not build item if it doesn't get enougth salient points if (this.canvasMap) { @@ -229,6 +230,57 @@ export default { } // console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints); }, + getJarvisEnvelopeConvexe(){ + // https://www.geeksforgeeks.org/convex-hull-using-jarvis-algorithm-or-wrapping/ + // the most left point + let l = 0, min_x = null; + for (let i = 1; i < this.entites.length; i++) { + let entite = this.entites[i]; + let x = entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180)); + if(!min_x || min_x > x){ + l = i; + min_x = x; + } + } + let p = l, q; + do { + let entite = this.entites[p]; + let farest = { + alpha: entite.display.alpha, + ray: entite.display.ray, + pos: { + x: (entite.display.ray) * Math.cos(entite.display.alpha * (Math.PI/180)), + y: (entite.display.ray) * Math.sin(entite.display.alpha * (Math.PI/180)) + } + }; + this.salientPoints.push(farest); + + q = (p + 1) % this.entites.length; + for (let i = 0; i < this.entites.length; i++) { + let p_x = (this.entites[p].display.ray + 3) * Math.cos(this.entites[p].display.alpha * (Math.PI/180)); + let p_y = (this.entites[p].display.ray + 3) * Math.sin(this.entites[p].display.alpha * (Math.PI/180)); + let i_x = (this.entites[i].display.ray + 3) * Math.cos(this.entites[i].display.alpha * (Math.PI/180)); + let i_y = (this.entites[i].display.ray + 3) * Math.sin(this.entites[i].display.alpha * (Math.PI/180)); + let q_x = (this.entites[q].display.ray + 3) * Math.cos(this.entites[q].display.alpha * (Math.PI/180)); + let q_y = (this.entites[q].display.ray + 3) * Math.sin(this.entites[q].display.alpha * (Math.PI/180)); + + let val = (i_y - p_y) * (q_x - i_x) - (i_x - p_x) * (q_y - i_y); + + if (val > 0){ + q = i; + } + + } + + // Now q is the most counterclockwise with + // respect to p. Set p as q for next iteration, + // so that q is added to result 'hull' + p = q; + } while (p != l) { + + } + + }, initCanvasMap (){ // console.log(`ConcernementsMapItem ${this.concernement.id} initCanvasMap`); // record canvas and ctx for rendering (drawing)