如何创建随机矩形(自动)?

时间:2016-09-08 09:19:05

标签: matlab random rectangles

我想创建许多矩形。这应该自动完成。如果不在代码中键入数千个值,我该怎么做?有解决方案吗?

  • 在我的代码中,我在矢量“V”中手动编写了每个坐标点(每个矩形的4个点)。
  • 还有如何连接它们。 “F”
  • 每个矩形的值。 “C”

我的代码是

clc
clear all
figure;
V = [0,0;1,0;1,1;0,1;5,5;10,5;10,10;5,10;2,2;4,2;4,4;2,4];
F = [1,2,3,4;5,6,7,8;9,10,11,12];%Dieser Vektor sagt mir in welcher Reihenfolge die Punkte
C = [50;24;99];

patch('Faces',F,'Vertices',V,'FaceVertexCData',C,'FaceColor','flat','EdgeColor','none') %Befehl fürs "zeichnen"
colormap(parula)
colorbar

1 个答案:

答案 0 :(得分:0)

您可以使用以下函数创建一个随机矩形,通过随机生成左下角的(x,y)位置,并随机生成宽度和高度 -

function rect = createRandomRectangle(maxX, maxY, minHeight, maxHeight, minWidth, maxWidth)

    bottom = maxY * rand;
    left   = maxX * rand;

    height = minHeight + rand * (maxHeight - minHeight);
    width  = minWidth  + rand * (maxWidth  - minWidth);

    rect = [
        left, bottom
        left, bottom + height
        left + width, bottom + height
        left + width, bottom
    ];

end

然后,您只需要处理创建VFC矩阵(通过在循环中调用createRandomRectangle)并绘制它们。

相关问题