I need help randomizing a triangle
조회 수: 3 (최근 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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!