Introduction
I have implemented Convex hull
algorithm in grasshopper by creating one C# scripting component
. Here, I would like to note some codes I have learned.
There are many algorithms for finding convex hull, Javis' March
, also known as Gift Wrapping Algorithm
, should be the most intuitive one.
Though it is not the fastest one, this algorithm solve the problem with only an simple geometric algebra sense.
Some other algorithms about Convex hull
can be found here
Dive into Algorithm
While marching, by the result of the cross Product to determine the next point.
- Prepare point cloud(many points) on one plane.
- Decided the starting point
- Using crossProduct property to march through the points.
Starting point
I set the buttom-most point as the starting point, and if the y is the same then compare x-value.
1 | bool compare(Point3d a, Point3d b){ // to find the starting point |
CrossProduct
1 | Vector3d crossProduct(Vector3d u, Vector3d v){ |
Length2
Though we should calculate distance, which should use square root
, we here can do the same thing without using it because we want the relationship, instead of the precise distance.
By doing this way, we get what we want, and because not using square root
calculation, it is faster.
1 | double length2(Point3d a, Point3d b){ |
All code
1 | private void RunScript(List<Point3d> pts, ref object A) |
I used do-while
first ,cause I want to at least run once, which make sure the list contains at least two points.
Note
Let’s assume if there are n
points in the point cloud, and m
points on the convex hull.
Then the total time complexity will be O(NM)
.
There are some faster algorithms doing sorting before running cross Product calculation, Andrew's Monotone Chain
, for example. I might try to implement this algorithm the other day.