Borrar filtros
Borrar filtros

How can I put 5 points inside of the triangular by using rand (5,2)?

4 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Image Analyst
Image Analyst el 24 de Jul. de 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);

Más respuestas (3)

the cyclist
the cyclist el 24 de Jul. de 2016
Editada: the cyclist el 24 de Jul. de 2016
For each of the 5 points, do this
t = sqrt(rand());
s = rand();
P = (1-t)*A + t*((1-s)*B+s*C)
I found that method in this thread, after a very brief google keyword search.
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 comentario
the cyclist
the cyclist el 24 de Jul. de 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.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 24 de Jul. de 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

Star Strider
Star Strider el 24 de Jul. de 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:

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by