Physics, asked by prasad6987, 1 year ago

Write a program to display a rectangle along with two diagonals.

Answers

Answered by Student213
3

That is actually not a trivial problem at all!

You see, the diagonals (when we imagine them to be lines of the same width as the circumference lines) don't correspond well to integer coordinates. So if you use '*' characters, either you'll have too many - several per one line - or too few, with breaks in the line.

You probably want to use Bresenham's line drawing algorithm for this problem.

Or - if you want to be simplistic, and place one '*' per line, use the formula for the diagonal, with rounding, to pick the best value irrespective of other lines:

major_diagonal_y = (x - start_x) * ((float) rectangle_y_dim) / rectangle_x_dim

(assuming x is the vertical dimension and y is the horizontal one; you used other symbols but I found them a bit confusing.)

For the opposite (minor) diagonal, use

minor_diagonal_y = rectangle_y_dim - major_diagonal_y

If you're willing to use other characters, you could consult this tutorial on ASCII art, with a section on diagonals. It demonstrates how to vary the use of characters to effectively have "sub-character" resolution.

Similar questions