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
- Host: GitHub
- URL: https://github.com/dubniczky/convex-hull
- Owner: dubniczky
- License: mit
- Created: 2022-08-03T16:21:30.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-03T16:37:12.000Z (over 3 years ago)
- Last Synced: 2025-02-06T19:49:26.147Z (10 months ago)
- Topics: algorithm, javascript, optimization, p5js, web
- Language: JavaScript
- Homepage:
- Size: 148 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.
[](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.