How can I put 5 points inside of the triangular by using rand (5,2)?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Beibit Sautbek
am 23 Jul. 2016
Beantwortet: Image Analyst
am 24 Jul. 2016
I have triangular ABC, where
A=[5,60];
B=[50,90];
C=[50,30];
I need to put 5 points just inside of this ABC triangular. x and y values of 5 points should be randomly chosen, by using rand(5,2).
How can I do? Could anyone help me?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 24 Jul. 2016
Yet another solution using simple geometry and the congruence of triangles.
A=[5,60];
B=[50,90];
C=[50,30];
x = [A(1), B(1), C(1), A(1)];
y = [A(2), B(2), C(2), A(2)];
x1 = min(x);
x2 = max(x);
plot(x, y, 'b*-', 'LineWidth', 2);
grid on;
hold on;
% Get an array of 5x2 random numbers, like required.
r = rand(5,2);
% Get 5 random x
xt = x1 + (x2-x1)*r(:,1)
% Get 5 random y and plot them
yt = (xt - x1) .* (90-60) ./ (x2 - x1) .* (2*r(:, 2)-1) + 60
plot(xt, yt, 'r+', 'LineWidth', 2);
0 Kommentare
Weitere Antworten (3)
the cyclist
am 24 Jul. 2016
Bearbeitet: the cyclist
am 24 Jul. 2016
For each of the 5 points, do this
t = sqrt(rand());
s = rand();
P = (1-t)*A + t*((1-s)*B+s*C)
Here is a vectorized version:
N = 5;
A=[5,60];
B=[50,90];
C=[50,30];
AN = repmat(A,N,1);
BN = repmat(B,N,1);
CN = repmat(C,N,1);
t = sqrt(rand(N,1));
s = rand(N,1);
P = AN + t.*(s.*(CN-BN)+(BN-AN));
figure
hold on
plot([A(1) B(1) C(1) A(1)],[A(2) B(2) C(2) A(2)],'r-')
h = plot(P(:,1),P(:,2),'.');
set(h,'MarkerSize',24)
1 Kommentar
the cyclist
am 24 Jul. 2016
I don't know if it is important to you that the points be uniformly distributed within the triangle, but this solution has that property.
Image Analyst
am 24 Jul. 2016
Not sure by what you mean by "just" inside. Does it need to be pretty close to the sides and not in the middle of the triangle? Anyway, here's a brute force but easy to understand way using a for loop to try locations until you get the required number of points inside, as determined by the inpolygon() function:
A=[5,60];
B=[50,90];
C=[50,30];
x = [A(1), B(1), C(1), A(1)];
y = [A(2), B(2), C(2), A(2)];
x1 = min(x);
x2 = max(x);
y1 = min(y);
y2 = max(y);
plot(x, y, 'b*-', 'LineWidth', 2);
grid on;
hold on;
numInside = 0;
loopCounter = 0;
maxIterations = 1000;
% Keep looping and checking with inpolygon
% until we get the required number.
while numInside < 5 && loopCounter < maxIterations
xTrial = x1 + (x2-x1)*rand;
yTrial = y1 + (y2-y1)*rand;
if inpolygon(xTrial, yTrial, x, y)
xInTriangle = xTrial;
yInTriangle = yTrial;
plot(xInTriangle, yInTriangle, 'r+', 'MarkerSize', 16, 'LineWidth', 2);
numInside = numInside + 1
end
loopCounter = loopCounter + 1;
end
xInTriangle % Echo to command window.
yInTriangle
0 Kommentare
Star Strider
am 24 Jul. 2016
It’s not possible to use rand alone to put them inside the triangle, because rand only produces values on the interval [0,1], and those are outside the triangle. So a bit of code is needed to place them inside the triangle and plot the points and the triangle:
A=[5,60];
B=[50,90];
C=[50,30];
Mpatch = cat(1,A,B,C)'; % Patch Vertices
Lims = mean([min(Mpatch,[],2) max(Mpatch,[],2)],2); % Mean Of Coordinates
Mult = 10; % Multiplier To Separate Points (Optional)
pts = bsxfun(@plus,Mult*rand(5,2),Lims'); % Add Mean Of Coordinates To Random Matrix
figure(1)
patch(Mpatch(1,:), Mpatch(2,:), 'g')
hold on
scatter(pts(:,1), pts(:,2), 'bp')
hold off
axis([0 60 20 100])
I added a multiplier to separate the points, because otherwise they cluster together. The code will work without the multiplier.
A sample plot:
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!