How do i create a rectangular like this?

2 views (last 30 days)
Dark Shadow
Dark Shadow on 6 Apr 2020
Edited: Dark Shadow on 6 Apr 2020
Hello.
I need help with a school project in Matlab.
I have to create a rectangular shape (script) like the code below without using the ''rectangle'' command, and the dimensions of the rectangular are to be inserted with the ''input'' command and the center of the rectangular is the origin of XY axis.
Can you help me?
Looking forward to hearing from you as soon as possible. I hope you can help me.
Thank you!
clear all
close all
clc
a = input('Dim 1: '); % Width for ex: Dim 1: 5(cm)
b = input('Dim 2: '); % Length for ex: Dim 2: 10(cm)
width = a;
length = b;
x_center = 0;
y_center = 0;
xl = x_center - width/2;
yb = y_center - length/2;
rectangle('Position',[xl, yb, width, length]) % I have to eliminate this line and reformulate it without using "rectangle" command
grid on
xlim([-100, 100]);
ylim([-100, 100]);
xL = xlim;
yL = ylim;
line([0 0], yL)
line(xL, [0 0])
  1 Comment
Subhamoy Saha
Subhamoy Saha on 6 Apr 2020
As this is a school project, I'm not giving the direct solution. I'm sure you can do it by yourself.
You already have the answer hidden in your script. Just use 4 lines. You have drawn x and y axes using the same. Just modify those an you will have your rectangle.
Create end point coordinates from the dimension values. The following code will draw a line between point (x1, y1) to (x2, y2). By this way you have to write 4 lines of code to draw 4 lines.
line([x1, x2], [y1, y2])
However, you can also create a matrix to draw multiple lines in one go. See the matlab documentation for details.

Sign in to comment.

Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!