Search code examples
matlabplotdraw

Matlab - Plotting rectangle without using rectangle function


How do i plot a rectangle using the plot function. I cant use rectangle(). I currently am using

theta2 = linspace(0,2*pi,5);
plot(radius.*cos(theta2)+xCentre,radius.*sin(theta2)+yCentre,'k');

but this plots a diamond rather than a rectangle.


Solution

  • Here is the code for drawing rectangle without using rectangle()

    length=5;
    width=2;
    xCentre=0;
    yCentre=0;
    
    u=[-1 1 1 -1];
    x=[u u(1)].*(length/2);
    u=circshift(u,[0 -1]);
    y=[u u(1)].*(width/2);
    plot(x,y,'k');
    xlim([x(1)-2,x(2)+2]);
    ylim([y(3)-2,y(2)+2])