Convex Hull Algorithm Implementation in Revit
ConvertHullAlgorithm (Convex Hull Algorithm)
Reference
“Computational Geometry” - Introduction: An Example of Convex Hull
Preface
- The basic logic and concept of the algorithm come from the book “Computational Geometry”. The demos of other chapters will also be implemented and debugged in Revit, hoping to find a suitable implementation direction for each algorithm in Revit.
- All code is written in C# and debugged in Revit. Some interfaces and judgments utilize the Revit API. If you want to run the algorithm purely, you can modify those API parts to use common libraries.
Algorithm Logic
The algorithm is relatively simple. It primarily involves acquiring the outer boundary of a point set by comparing points with vectors to determine the boundary area.

- The algorithm sorts all points in the set by their X value from small to large. After sorting, connecting the leftmost point and the rightmost point divides the entire set into two subsets: the upper subset and the lower subset.

- If a boundary is found, it means the boundary points will enclose all points. Therefore, connecting boundary points pairwise will form the largest vectors, ensuring no other points lie to the right of the vector, which identifies them as boundary points.

- Through the above steps, we can create a traversal to find the outermost vector and gradually advance towards the maximum value position, thereby outlining the boundary area.

- After completing the upper subset traversal (advancing to the point with the maximum X value), continue traversing the subset clockwise. At this point, the entire array collection needs to be transposed from large to small, and the reference vector
Dirmodified to(TheRight - TheLeft).

- Once the leftmost endpoint and the rightmost endpoint become the start and end points of each other, all boundary traversals are completed.
Code
- Create a judgment for the directionality of two vectors. Initially, use the vector of the two starting points as the comparison vector. First, traverse the upper subset, which requires checking if the target vector is above or to the left of the secondary vector.
Vector cross product and Z-value judgment are used at the decision position. The principle is: calculating the normal vector allows determining the rotation direction of the vector according to the left-hand rule, thereby determining the positional relationship of the two vectors.

1 | /// <summary> |
- Create the traversal function. Here is the pseudo-code from the book and the C# code explanation.
1 | Algorithm CONVEXHULL(P) |
- Define start point
TheLeft, end pointTheLast, and point setvertices. - Determine if the vector of the collection relative to
TheLeftlies to the right offakeDir = TheLast - TheLeft. If the point is to the right of the vector, iterate the pointPointdata, replacefakeDir, and enter the next loop to check if it’s to the right offakeDir. - After the loop ends, add the rightmost point to the collection, thereby identifying all boundary points.
1 | private List<XYZ> GetHullVertices(bool isUp = true) |
Execution Results


All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.





