I need help randomizing a triangle

2 views (last 30 days)
Amanda
Amanda on 28 Nov 2022
Edited: Voss on 28 Nov 2022
I have written a code that is suppose to randomize the triangle as well as the markers/colors/edgecolors but I cant get the triangle itself to randomize. The markers and colors randomize but the triangle only changes when i run the code. I dont know how to fix this. Below is what I have:
figure;
hold on;
xlim([0,1]);
ylim([0,1]);
point_A = rand(2,1);
point_B = rand(2,1);
point_C = rand(2,1);
run('TrianglePlot.m');
for x = 1:10
point_A = rand(2,1);
point_B = rand(2,1);
point_C = rand(2,1);
marker_types = 'o+*.x';
random_index = randi(5);
random_marker = marker_types(random_index);
markerSize_types = 50:100;
random_index = randi(5);
random_markerSize = markerSize_types(random_index);
markerEdgeColor_types = 'brgym';
random_index = randi(5);
random_markerEdgeColor = markerEdgeColor_types(random_index);
markerFaceColor_types = 'myrgb';
random_index = randi(5);
random_markerFaceColor = markerFaceColor_types(random_index);
triangle_handle.Marker= random_marker;
triangle_handle.MarkerSize = random_markerSize;
triangle_handle.MarkerEdgeColor = random_markerEdgeColor;
triangle_handle.MarkerFaceColor = random_markerFaceColor;
pause(1)
end

Accepted Answer

Voss
Voss on 28 Nov 2022
Edited: Voss on 28 Nov 2022
It is not enough to generate a new random point_A, point_B, and point_C on each iteration. You need to make those points take effect, so update the existing triangle's XData and YData based on the new point_A, point_B, and point_C:
figure;
hold on;
xlim([0,1]);
ylim([0,1]);
point_A = rand(2,1);
point_B = rand(2,1);
point_C = rand(2,1);
% run('TrianglePlot.m'); % no need to "run", just call the script
TrianglePlot
% the sets of markers, sizes, etc., don't change throughout the 10
% iterations, so they can be defined before the loop:
marker_types = 'o+*.x';
markerSize_types = 50:100;
markerEdgeColor_types = 'brgym';
markerFaceColor_types = 'myrgb';
for x = 1:10
point_A = rand(2,1);
point_B = rand(2,1);
point_C = rand(2,1);
random_index = randi(5);
random_marker = marker_types(random_index);
random_index = randi(5);
random_markerSize = markerSize_types(random_index);
random_index = randi(5);
random_markerEdgeColor = markerEdgeColor_types(random_index);
random_index = randi(5);
random_markerFaceColor = markerFaceColor_types(random_index);
% update the XData and YData to have a "new" triangle:
triangle_handle.XData = [point_A(1), point_B(1), point_C(1), point_A(1)];
triangle_handle.YData = [point_A(2), point_B(2), point_C(2), point_A(2)];
triangle_handle.Marker= random_marker;
triangle_handle.MarkerSize = random_markerSize;
triangle_handle.MarkerEdgeColor = random_markerEdgeColor;
triangle_handle.MarkerFaceColor = random_markerFaceColor;
pause(1)
end

More Answers (1)

Vilém Frynta
Vilém Frynta on 28 Nov 2022
Edited: Vilém Frynta on 28 Nov 2022
  3 Comments
Vilém Frynta
Vilém Frynta on 28 Nov 2022
I do not know what is inside of “TrianglePlot.m”. You might need to use rng there if you use randomizing inside.
Amanda
Amanda on 28 Nov 2022
triangleplot.m contains this
triangle_handle = plot([point_A(1), point_B(1), point_C(1), point_A(1)], [point_A(2),point_B(2), point_C(2), point_A(2)]);

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!