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

조회 수: 8 (최근 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 CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by