How to plot a box onto an existing convex hull graph?

1 view (last 30 days)
I have two sets of data, XData and YData, with a bunch of points. I plot a scatter of the points, then plot a convex hull of the points:
figure(a); scatter(XData,YData); hold on;
Hull = convhull(XData,YData);
HullCoordinates = [XData(Hull),YData(Hull)];
plot(HullCoordinates(:,1),HullCoordinates(:,2));
title('Title')
xlabel('X (lbs)')
ylabel('Y (lbs)');
grid on; hold off;
However, I'd also like to plot the limits in both axes as a box:
figure(a); scatter(XData,YData); hold on;
Hull = convhull(XData,YData);
HullCoordinates = [XData(Hull),YData(Hull)];
XLimit = 50;
YLimit2 = 100;
Coordinates = [XLimit,YLimit; XLimit,-YLimit; -XLimit,YLimit; -XLimit,-YLimit];
Limits = convhull(Coordinates);
LimitCoordinates = [Coordinates(Limits)];
plot(LimitCoordinates(:,1),BoundHullCoordinates(:,2));
plot(HullCoordinates(:,1),HullCoordinates(:,2));
title('Title')
xlabel('X (lbs)')
ylabel('Y (lbs)');
grid on; hold off;
But this doesn't work. I suspect that I am approaching the problem of creating a box from 4 coordinates given x-intercept and y-intercept bounds incorrectly. What should I change?
  1 Comment
Image Analyst
Image Analyst on 8 Jul 2022
Can you attach some screenshots and your actual data in a .mat file with the paperclip icon?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

Accepted Answer

KSSV
KSSV on 8 Jul 2022
XData = rand(100,1) ;
YData = rand(100,1) ;
figure(1); scatter(XData,YData); hold on;
Hull = convhull(XData,YData);
HullCoordinates = [XData(Hull),YData(Hull)];
% Get bounding box
x0 = min(XData) ; x1 = max(XData) ;
y0 = min(YData) ; y1 = max(YData) ;
B = [x0 y0 ; x1 y0; x1 y1; x0 y1 ; x0 y0] ;
plot(HullCoordinates(:,1),HullCoordinates(:,2));
plot(B(:,1),B(:,2),'g')
title('Title')
xlabel('X (lbs)')
ylabel('Y (lbs)');
grid on; hold off;

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!