0

i'm new here and this is my first post.. i want to know if there is a way to calculate all the possible convex hulls from 100 random points.. i've created a code that does this right but it gives me an error at convhull and everything beneath that part can't be calculated.. in other words i'd like to know if there is a way i can use function convhull inside a for loop without getting an error..

HERE IS THE CODE

hold on
N=100;
Points = zeros(N,2);
for i=1:N
    Points(i,1)= rand(1,1)*100;
    Points(i,2)= rand(1,1)*100;
end

SortedX = sortrows(Points,1);
NewVertices = SortedX;

X = reshape(SortedX(:,1),2,[]);
X = X(:);
Y = reshape(SortedX(:,2),2,[]);
Y = Y(:);
plot(X,Y,'*');

while length(NewVertices)>=3
    NewVertices = NewVertices(NewVertices~=0);
    rowsNV = length(NewVertices(:,1));
    NewVertices = reshape(NewVertices,rowsNV/2,2);
    XNV = reshape(NewVertices(:,1),rowsNV/2,[]);
    XNV = XNV(:);
    YNV = reshape(NewVertices(:,2),rowsNV/2,[]);
    YNV = YNV(:);
    K = convhull(XNV,YNV);
    plot(XNV(K),YNV(K),'r-');
    for i=1:length(K)-1
        NewVertices(K(i),1)=0;
        NewVertices(K(i),2)=0;
    end

end

HERE IS THE ERROR

Error using convhull
Error computing the convex hull. Not
enough unique points specified.

Error in test2 (line 28)
    K = convhull(XNV,YNV);

Thanks in advance

co2ark5

HERE IS THE FINAL CODE THAT WORKS CORRECTLY WITH DAN'S HELP

hold on
N=100;
Points = rand(N,2);

SortedX = sortrows(Points,1);
NewVertices = SortedX;

plot(SortedX(:,1),SortedX(:,2),'*');

while length(NewVertices)>=3
    X=NewVertices(:,1);
    Y=NewVertices(:,2);
    K = convhull(X,Y);
    plot(X(K),Y(K),'r-');
    for i=1:length(K)-1
        NewVertices(K(i),1)=0;
        NewVertices(K(i),2)=0;
    end
    NewVertices = NewVertices(any(NewVertices,2),:);
end
co2ark5
  • 45
  • 1
  • 3
  • 12
  • 2
    Please post a small relevant section of your code and a description of the error so we can see where you're going wrong. – wakjah Apr 03 '13 at 10:50
  • Isn't there only one unique convex hull for a given set of points? What do you mean by all possible convex hulls? Also please post the code and the error – Dan Apr 03 '13 at 10:58
  • You are right there is one convex hull for a set of points but every time i calculate the next convex hull from the points left.. – co2ark5 Apr 03 '13 at 11:06

2 Answers2

1

The error is fairly expressive, you aren't specifying enough points (i.e. atleast 3 non colinear points). Please explain what you are trying to achieve with this code.

When do you get this error? I'm assuming not on the first run of the loop? After checking that there are more than 3 points you immediately potentially remove more points and then call convhull so it's quite possible that there are fewer than 3 points (and this is still ignoring the chance of colinearity). After the error, what is size(NewVertices)?

Some side comments: please explain that whole reshaping business before you plot (maybe add some comments). Also Points can be initialized much faster and simpler:

Points = rand(N, 2);

EDIT:

From reading your comments below it is clear to me that the logic of your loop is wrong. I cannot work out exactly how you are trying to remove points to create the new subset on which to find the hull, there are no comments and I don't get what you're doing with the reshape but I'm pretty sure all you need to do to fix this is to move the first line of your loop to the end like this:

while length(NewVertices)>=3

    rowsNV = length(NewVertices(:,1));
    NewVertices = reshape(NewVertices,rowsNV/2,2);
    XNV = reshape(NewVertices(:,1),rowsNV/2,[]);
    XNV = XNV(:);
    YNV = reshape(NewVertices(:,2),rowsNV/2,[]);
    YNV = YNV(:);
    K = convhull(XNV,YNV);
    plot(XNV(K),YNV(K),'r-');
    for i=1:length(K)-1
        NewVertices(K(i),1)=0;
        NewVertices(K(i),2)=0;
    end

    NewVertices = NewVertices(NewVertices~=0);

end

This way you find your hull immediately after checking the number of points rather than checking, then removing points, which is why your loop runs on 1 - 2 points.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • I know this isn't an answer but it was too much to say in the comments. – Dan Apr 03 '13 at 11:40
  • I'm just trying to calculate each time the NEW convex hull from the points left on the NewVertices matrix.. If you run the code on MATLAB you'll understand my problem.. I just can't use this code on a program 'cause it shows that error and after that line's error matlab will stop.. – co2ark5 Apr 03 '13 at 15:47
  • It appears actually when the program finishes, so it's on the last loop.. I figured out that if you change "while length(NewVertices)>=3" to "while length(NewVertices)>=6" it will work most of the times but it's roughly written i think.. After the error the size(NewVertices) is always 1 or 2 which is logical i think 'cause that's when the while case ends.. By the way thanks for the last tip ;) – co2ark5 Apr 03 '13 at 15:59
  • OK, your logic is still very unclear but have a look at the edit above, I think this will solve your issue. It's possible that this might mess up the first run of the loop but I doubt it. – Dan Apr 04 '13 at 06:59
  • i did some minor changes which are shown on my answer bellow.. again thank's a lot DAN ;) – co2ark5 Apr 04 '13 at 12:37
1

Thanks a lot DAN,

first i tried to change only the line you said but it didn't work again 'cause of the reshape functions i was using.. Then i decided to change them and i made it more simple and it did work!!

HERE IS THE FINAL CODE THAT WORKS CORRECTLY WITH DAN'S HELP

hold on
N=100;
Points = rand(N,2);

SortedX = sortrows(Points,1);
NewVertices = SortedX;

plot(SortedX(:,1),SortedX(:,2),'*');

while length(NewVertices)>=3
    X=NewVertices(:,1);
    Y=NewVertices(:,2);
    K = convhull(X,Y);
    plot(X(K),Y(K),'r-');
    for i=1:length(K)-1
        NewVertices(K(i),1)=0;
        NewVertices(K(i),2)=0;
    end
    NewVertices = NewVertices(any(NewVertices,2),:);
end
co2ark5
  • 45
  • 1
  • 3
  • 12
  • please accept and upvote Dan's answer if it helped you and maybe update your question with the full working code. this is the idea of stackoverflow ;) – mr.VVoo Apr 04 '13 at 12:23
  • i dont's have enough reputation to do so 'cause i'm new here :/ – co2ark5 Apr 04 '13 at 12:26
  • but you should accept his answer if it helped you and this shouldn't require reputation since you asked the question. – mr.VVoo Apr 04 '13 at 12:28
  • i voted his answer and wrote a comment underneath it saying that the right code is on my answer bellow.. well i also did as you said ;) – co2ark5 Apr 04 '13 at 12:42
  • This is exactly what I was saying, I didn't want to take out your reshapes as they made no sense to me so I wasnt sure what you were up to with them but this is how I would have done it. – Dan Apr 04 '13 at 12:46