I need help randomizing a triangle
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Voss
il 28 Nov 2022
Modificato: Voss
il 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
0 Commenti
Più risposte (1)
Vilém Frynta
il 28 Nov 2022
Modificato: Vilém Frynta
il 28 Nov 2022
3 Commenti
Vilém Frynta
il 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.
Vedere anche
Categorie
Scopri di più su Specifying Target for Graphics Output in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!