필터 지우기
필터 지우기

Write a function drawBox which draws boxes on the screen

조회 수: 2 (최근 30일)
Khalid Khan
Khalid Khan 2016년 11월 30일
답변: Abu Yamen 2016년 12월 3일
I got a task but I am confused. This is My task The function is called with a character (ch) as the first argument and two positive integers which represent the width and the height of the box to be drawn. It automatically draws a box with the character (ch) by the specified width and height. Can Anyone help me out with the task.
  댓글 수: 1
Abu Yamen
Abu Yamen 2016년 12월 3일
function []= drawBox('s',w,h)
ch =input('ch-ch ','s'); w =input('width : '); h =input('hight : ');
x = repmat(ch, h, w); x(2:end-1, 2:end-1) = char(32); disp(x); end

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

채택된 답변

KSSV
KSSV 2016년 11월 30일
편집: KSSV 2016년 11월 30일
function drawBox(str,w,h)
ch = [0. 0.] ;
%%get four corners of box
N = 20 ;
x = linspace(ch(1),ch(1)+w,N) ;
y = linspace(ch(2),ch(2)+h,N) ;
[X,Y] = meshgrid(x,y) ;
X(2:end-1,2:end-1) = NaN ;
Y(2:end-1,2:end-1) = NaN ;
x = X(~isnan(X)) ;y = Y(~isnan(Y)) ;
plot(x,y,'.w') ; hold on
text(x,y,str)
axis off
  댓글 수: 4
Khalid Khan
Khalid Khan 2016년 11월 30일
편집: Khalid Khan 2016년 11월 30일
Thank You Sir It worked
rim
rim 2016년 12월 2일
편집: rim 2016년 12월 2일
its not working for me , please help me with this question its showing error in functio drawBox

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

추가 답변 (2개)

KSSV
KSSV 2016년 11월 30일
what is ch? Actually to draw a box four numbers are needed. First two position/ origin of the box and then width, height. I think ch is a 1x2 array with two numbers which represent position of the box.
ch = [1. 1.] ;
h = 1. ;
w = 0.5 ;
box = [ch w h] ;
%%get four corners of box
p = [ch(1) ch(2) ;
ch(1)+w ch(2) ;
ch(1)+w ch(2)+h ;
ch(1) ch(2)+h ;
ch(1) ch(2)] ;
plot(p(:,1),p(:,2),'r')
  댓글 수: 3
feg bog
feg bog 2016년 11월 30일
편집: feg bog 2016년 11월 30일
the given code do not give the wanted output is there is wrong on the code , or what !! i am confused i.e. is necessary to use plot or normal printf code?

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


Abu Yamen
Abu Yamen 2016년 12월 3일
function []= drawBox('s',w,h)
ch =input('ch-ch ','s'); w =input('width : '); h =input('hight : ');
x = repmat(ch, h, w); x(2:end-1, 2:end-1) = char(32); disp(x); end

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by