I need help randomizing a triangle
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2022-11-28
编辑:Voss
2022-11-28
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 个评论
更多回答(1 个)
Vilém Frynta
2022-11-28
编辑:Vilém Frynta
2022-11-28
3 个评论
Vilém Frynta
2022-11-28
I do not know what is inside of “TrianglePlot.m”. You might need to use rng there if you use randomizing inside.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!