An open API service indexing awesome lists of open source software.

https://github.com/dubniczky/convex-hull

Optimizing a convex hull using trigonometric elimination on a web environment
https://github.com/dubniczky/convex-hull

algorithm javascript optimization p5js web

Last synced: 8 months ago
JSON representation

Optimizing a convex hull using trigonometric elimination on a web environment

Awesome Lists containing this project

README

          

# Trigonometric Convex Hull

Optimizing a convex hull using trigonometric elimination.

## Support ❤️

If you find the project useful, please consider supporting, or contributing.

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/dubniczky)

## Live Demo

https://dubniczky.github.io/Convex-Hull/

## Algorithm details

1. Calculate center point from average positions.
2. Sort points based on distance from center.
3. Eliminate center points using triangle intersection.
4. Sort remaining points based on theta angle from center.
5. Connect remaining points in sequence.

## Triangle intersection code sample

```javascript
let edges = []
for (let i = points.length - 1; i >= 0; i--) {
let intersect = false
for (let j = 0; !intersect && j < points.length - 1; j++) {
if (j == i) {
continue
}
for (let k = j + 1; !intersect && k < points.length; k++) {
if (k == i) {
continue
}

let a = points[i]
let b = points[j]
let c = points[k]
let d = center

let d1 = (a.pos.x - c.pos.x) * (b.pos.y - c.pos.y) - (b.pos.x - c.pos.x) * (a.pos.y - c.pos.y)
let d2 = (a.pos.x - d.pos.x) * (b.pos.y - d.pos.y) - (b.pos.x - d.pos.x) * (a.pos.y - d.pos.y)
let d3 = (a.pos.x - d.pos.x) * (c.pos.y - d.pos.y) - (c.pos.x - d.pos.x) * (a.pos.y - d.pos.y)

let neg = (d1 < 0) || (d2 < 0) || (d3 < 0)
let pos = (d1 > 0) || (d2 > 0) || (d3 > 0)

if (!(neg && pos)) {
intersect = true
}
}
}
if (!intersect) {
edges.push(i)
}
}
```

## Usage

Run the `./deploy.sh` script.

Host `./public` folder on a static file server or open `./public/index.html` locally in your browser.