2-D Convex Hull
Create a matrix of 2-D points. Compute the convex hull and its area.
P = [0 0; 1 1; 1.5 0.5; 1.5 -0.5; 1.25 0.3; 1 0; 1.25 -0.3; 1 -1]; [k,av] = convhull(P);
Plot the original points and the convex hull.
plot(P(:,1),P(:,2),'*') hold on plot(P(k,1),P(k,2))
Display the area.
av = 1.7500
Simplify 3-D Convex Hull
Simplify a 3-D convex hull by removing points that do not affect its volume.
Create a set of 3-D points. Compute the convex hull and its volume. Plot the convex hull.
[x,y,z] = meshgrid(-2:1:2,-2:1:2,-2:1:2); x = x(:); y = y(:); z = z(:); [k1,av1] = convhull(x,y,z); trisurf(k1,x,y,z,'FaceColor','cyan') axis equal
Compute and plot a simplified version of the convex hull that contains fewer points, but preserves the volume.
[k2,av2] = convhull(x,y,z,'Simplify',true); trisurf(k2,x,y,z,'FaceColor','cyan') axis equal
Display the volumes of both convex hulls. The volumes are the same, but the simplified convex hull uses fewer points.
av1 = 64.0000
av2 = 64
P — Points
Points, specified as a matrix whose columns are the x-coordinates, y-coordinates, and (in three dimensions) z-coordinates.
x — x-coordinates
x-coordinates, specified as a column vector.
y — y-coordinates
y-coordinates, specified as a column vector.
z — z-coordinates
z-coordinates, specified as a column vector.
tf — Simplify indicator
0 (default) |
Simplify indicator, specified as a numeric or logical
k — Indices
vector | matrix
Indices, returned as a vector or matrix.
For 2-D points,
kis a column vector containing the row indices of the input points that make up the convex hull, arranged counterclockwise.
For 3-D points,
kis a 3-column matrix representing a triangulation that makes up the convex hull. Each row represents a facet of the triangulation. The values represent the row indices of the input points.
av — Area or volume
Area or volume of the convex hull, returned as a scalar.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation supports computation of a convex hull for 2-D input points only.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.