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

1 回表示 (過去 30 日間)
Alessandro Verniani
Alessandro Verniani 2022 年 7 月 8 日
回答済み: KSSV 2022 年 7 月 8 日
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 件のコメント
Image Analyst
Image Analyst 2022 年 7 月 8 日
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:

サインインしてコメントする。

採用された回答

KSSV
KSSV 2022 年 7 月 8 日
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;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by