I need help randomizing a triangle

조회 수: 2 (최근 30일)
Amanda
Amanda 2022년 11월 28일
편집: Voss 2022년 11월 28일
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

채택된 답변

Voss
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

추가 답변 (1개)

Vilém Frynta
Vilém Frynta 2022년 11월 28일
편집: Vilém Frynta 2022년 11월 28일
  댓글 수: 3
Vilém Frynta
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.
Amanda
Amanda 2022년 11월 28일
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)]);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by