Forum Discussion
RecalcOrDie
May 05, 2023Iron Contributor
Convex hull Lambda > would it be impossible to build?
Hello everyone, Ever since visiting the site of Andy Pope, I saw a chart that fascinated me because it can be very useful for creating the bounding area of a set of points. It's known as Convex ...
JKPieterse
May 08, 2023Silver Contributor
I did. After some discussion with it, it came up with this:
=LAMBDA(points,
LET(
leftmost_point, MIN(points, 1),
x_values, INDEX(points, 0, 1),
y_values, INDEX(points, 0, 2),
n, ROWS(points),
indices, SEQUENCE(n),
indices_sorted, SORTBY(indices, x_values),
upper_hull_indices, FILTER(indices_sorted, y_values >= LINEST(y_values, x_values,,TRUE)),
lower_hull_indices, FILTER(indices_sorted, y_values < LINEST(y_values, x_values,,TRUE)),
upper_hull_points, INDEX(points, upper_hull_indices),
lower_hull_points, INDEX(points, lower_hull_indices),
upper_hull_sorted, SORTBY(upper_hull_points, y_values, -1),
lower_hull_sorted, SORTBY(lower_hull_points, y_values),
upper_hull_final, IF(COUNT(upper_hull_sorted) > 1,
TRANSPOSE({INDEX(upper_hull_sorted, 1),INDEX(upper_hull_sorted,2)}) ,
upper_hull_sorted),
lower_hull_final, IF(COUNT(lower_hull_sorted) > 1,
TRANSPOSE({INDEX(lower_hull_sorted, 1),INDEX(lower_hull_sorted,2)}) ,
lower_hull_sorted),
hull_points, SORTBY(TRANSPOSE({leftmost_point, upper_hull_final, lower_hull_final}), x_values)
,
TEXTJOIN(",",TRUE,hull_points)
))
=LAMBDA(points,
LET(
leftmost_point, MIN(points, 1),
x_values, INDEX(points, 0, 1),
y_values, INDEX(points, 0, 2),
n, ROWS(points),
indices, SEQUENCE(n),
indices_sorted, SORTBY(indices, x_values),
upper_hull_indices, FILTER(indices_sorted, y_values >= LINEST(y_values, x_values,,TRUE)),
lower_hull_indices, FILTER(indices_sorted, y_values < LINEST(y_values, x_values,,TRUE)),
upper_hull_points, INDEX(points, upper_hull_indices),
lower_hull_points, INDEX(points, lower_hull_indices),
upper_hull_sorted, SORTBY(upper_hull_points, y_values, -1),
lower_hull_sorted, SORTBY(lower_hull_points, y_values),
upper_hull_final, IF(COUNT(upper_hull_sorted) > 1,
TRANSPOSE({INDEX(upper_hull_sorted, 1),INDEX(upper_hull_sorted,2)}) ,
upper_hull_sorted),
lower_hull_final, IF(COUNT(lower_hull_sorted) > 1,
TRANSPOSE({INDEX(lower_hull_sorted, 1),INDEX(lower_hull_sorted,2)}) ,
lower_hull_sorted),
hull_points, SORTBY(TRANSPOSE({leftmost_point, upper_hull_final, lower_hull_final}), x_values)
,
TEXTJOIN(",",TRUE,hull_points)
))
JKPieterse
May 08, 2023Silver Contributor
Convincing perhaps, but Excel doesn't like the {INDEX(upper_hull_sorted, 1),INDEX(upper_hull_sorted,2)} bits in there.
- HansVogelaarMay 08, 2023MVP
No, expressions aren't allowed in literal arrays {...}, only constant values.
- JKPieterseMay 08, 2023Silver ContributorYes. And because I couldn't enter the formula I gave up having real work to do rather than fool around chatting with am artificial intelligence. Lets put Actual Intelligence to work!
- SergeiBaklanMay 08, 2023Diamond Contributor
Afraid to generate correct answer with AI tool you need more knowledge formulating proper questions rather than to generate the formula by your own from scratch.