How to draw a cube with nested for loops?

Hello all,
I'm trying to draw a cube, however need help figuring out what algorithm would be.
So far I have:
function [ cubic ] = Cube( sideLength )
%UNTITLED12 Summary of this function goes here
% Detailed explanation goes here
for i = 1:sideLength
cubic = sideLength
fprintf('+', sideLength)
end
I need to write a function that will take one integer and print it like this:
a. Example 1.
>> cube(2)
+----+
|\ \
| +----+
+ | |
\| |
+----+
b. Example 2.
>> cube(4)
+--------+
|\ \
| \ \
| +--------+
| | |
+ | |
\ | |
\| |
+--------+

3 件のコメント

Guillaume
Guillaume 2017 年 10 月 6 日
It's unclear what problem you have (other than not having put any effort). Your short attempt does not even try to solve the problem asked.
Aaron Grubbs
Aaron Grubbs 2017 年 10 月 6 日
The question asks for:
3. Draw a cube. Write a function (cube) that takes a single integer argument (side length of cube) and draws a 2-dimensional representation of a cube. The cube should be drawn using + for corners, - for horizontal lines, | for vertical lines, and \ for diagonals. A cube of size
Cedric
Cedric 2017 年 10 月 6 日
編集済み: Cedric 2017 年 10 月 6 日
Yes but you surely tried more than this loop that does not even use FPRINTF correctly.
Show us that you made some effort.
Try to split the approach in a series of small steps:
  1. Am I able to print just a '+'? Yes/no, if no I read the doc of FPRINTF, look for examples online, etc.
  2. Am I able to repeat '-' a given number of times? Yes/no, if no I google repeat elements in MATLAB, I read about REPELEM or REPMAT and train.
  3. Using this, am I able to print a horizontal edge without the '+'? ...
  4. .. and what about one with the '+'?
  5. Using this, am I able to print a row of vertical edges of just a square for starting? ...
  6. ...

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

質問済み:

2017 年 10 月 6 日

編集済み:

2017 年 10 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by