How do i create a rectangular like this?

1 次查看(过去 30 天)
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 个评论
Subhamoy Saha
Subhamoy Saha 2020-4-6
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

产品


版本

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by