How do you write a program do determine whether two rectangles are of identical dimensions?

Viewed 24

I will include the whole assignment and hopefully someone can help. The teachers won't help before the deadline, only after to explain what went wrong.

Identical rectangles

We will be writing a program to determine whether two rectangles are of identical dimensions. Looking at the example above, you can see that the rectangles do indeed seem to be identical. We can check this better if we have the exact coordinates of the rectangles.

In this case we do have some data, like the position of the left side of A (Ax1) and the right side of A (Ax2). When we subtract these two, the result is one of the dimensions of A.

Ax2 - Ax1 = 7

Using Ay1 and Ay2 we can calculate the other side of that rectangle. Then we can calculate the sides of rectangle B. Are the sides equal indeed?

Provide the x coordinates of A: 0,7

Provide the y-coordinates of A: 0,4

Provide the x-coordinates of B: 6,13

Provide the y-coordinates of B: 2,6

The rectangles are identical!

Write, in a file names rechthoeken.py, a program that calculates on the basis of the provided coordinates whether the rectangles have identical dimensions. In addition, it is possible that the rectangles are also square and of identical dimensions, and in that case it must also be noted in the output. If there’s not much interesting going on with the rectangles, that fact is what is printed as a result. You may assume that the use inputs pairs of two integers. Given the example above, the user would mean that Ax1 = 0 and Ax2 = 7.

You will write a main program that asks for input and tidies it for further use. In addition there are three helper functions that you will use from the main program.

As you can see, there are two TODOs in the code. The functions are supposed to return a single value that signifies whether the check passed or not (e.g. it is square, or it isn’t). What datatype fits perfectly for returning pass/fail information?

def is_same_rectangle(ax_length: int, ay_length: int, bx_length: int, by_length: int) -> TODO: """ Checks whether the lengths of the sides are equal """

def is_square(ax_length: int, ay_length: int, bx_length: int, by_length: int) -> TODO: """ Checks whether the rectangles are (identical) squares """

def calculate_length(c1: int, c2: int) -> int: """ Calculates the length of a side from two coordinates """

if __name__ == '__main__': <Main program>

Hints

You can’t perform calculations on strings, so one step of tidying your data is to convert the inputs into integers.

For the function calculate_length you don’t know in advance which coordinate is larger (think of the case: c1 = 5, c2 = 3). Ensure that the function’s output is always positive. You can write some code for this yourself or make use of a standard Python-function.

Examples

Ultimately, your program should work as demonstrated below.

Provide the x coordinates of A: 0,7 Provide the y-coordinates of A: 0,4 Provide the x-coordinates of B: 6,13 Provide the y-coordinates of B: 2,6 The rectangles are identical!

Provide the x coordinates of A:0,7
Provide the y-coordinates of A:0,7 Provide the x-coordinates of B:6,13 Provide the y-coordinates of B:2,9 The rectangles are identical! And they're square, too!

1 Answers

This isn't a complete answer, but helpful instead this is meant to be a helpful guide.

  1. To ensure that you get a value that is always positive, get a difference between two values and use an absolute function on it. Like abs(x-y)

  2. If you are given an input and you are unsure if it's going to be a string, then do a typecast on it to an int. int(input_a_)

  3. Break down how you'd do the process in terms of the basic math. Then try to write pseudocode to lay out the steps. For example, one step might be to find the ratio of the sides of the two figures.

    ratio_lengths = ax_length / bx_length

    Then you could think of the ratio of the widths being very similar.

    ratio_width = ay_width / by_width

  4. The type of data that you would like to return is a boolean imo. It tells you if the input passed the test or not.

Instead of doing this:

if value_a == value_b:
  return True
else:
  return False

You can do better this way:

return value_a == value_b

That returns the 'truthiness' of the statement.

Related