Implementation of largest-cover difference #23@title Implementation of largest-cover difference def rectangle_difference(r, t): "Computes the rectangle difference rt, and outputs the result as a list of rectangles." assert len(r) == len(t), "Rectangles have different dimensions" ### YOUR SOLUTION HERE In the above code, we notice how it paid off to encapsulate intervals in their own abstraction. Now that we need to reason about rectangles, we do not need to get bogged down into complicated considerations of how to perform difference; the comparisons between endpoints are all done in the context of intervals, where it is easier to reason about them. It is often the case that problems become easy, once you think at them in the appropriate context. It is much easier to develop the code for the difference of intervals, as we have done, and then move on to difference of rectangles, rather than trying to write code for rectangle difference directly. The result is also far more elegant. # Let us test visually what happens. r = Rectangle((0., 4.), (0., 4.), name="R") t = Rectangle((2., 6.), (2., 6.), name="T") d = rectangle_difference (r, t) draw rectangles (r, t) draw rectangles (*d) [ ] # Whooho!! Let's try with another example. Now T is inside R. r = Rectangle((0., 4.), (0., 4.), name="R") t = Rectangle((1.5, 3.5), (1., 3.), name="T") d = rectangle_difference (r, t) draw rectangles (r, t) draw rectangles (*d)

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
Implementation of largest-cover difference
#23@title Implementation of largest-cover difference
def rectangle_difference(r, t):
"Computes the rectangle difference rt, and outputs the result
as a list of rectangles."
assert len(r) == len(t), "Rectangles have different dimensions"
### YOUR SOLUTION HERE
In the above code, we notice how it paid off to encapsulate intervals in their own abstraction. Now that we need to reason about rectangles, we
do not need to get bogged down into complicated considerations of how to perform difference; the comparisons between endpoints are all
done in the context of intervals, where it is easier to reason about them. It is often the case that problems become easy, once you think at them
in the appropriate context. It is much easier to develop the code for the difference of intervals, as we have done, and then move on to difference
of rectangles, rather than trying to write code for rectangle difference directly. The result is also far more elegant.
# Let us test visually what happens.
r = Rectangle((0., 4.), (0., 4.), name="R")
t = Rectangle((2., 6.), (2., 6.), name="T")
d = rectangle_difference (r, t)
draw rectangles (r, t)
draw rectangles (*d)
[ ] # Whooho!! Let's try with another example. Now T is inside R.
r = Rectangle((0., 4.), (0., 4.), name="R")
t = Rectangle((1.5, 3.5), (1., 3.), name="T")
d = rectangle_difference (r, t)
draw rectangles (r, t)
draw rectangles (*d)
Transcribed Image Text:Implementation of largest-cover difference #23@title Implementation of largest-cover difference def rectangle_difference(r, t): "Computes the rectangle difference rt, and outputs the result as a list of rectangles." assert len(r) == len(t), "Rectangles have different dimensions" ### YOUR SOLUTION HERE In the above code, we notice how it paid off to encapsulate intervals in their own abstraction. Now that we need to reason about rectangles, we do not need to get bogged down into complicated considerations of how to perform difference; the comparisons between endpoints are all done in the context of intervals, where it is easier to reason about them. It is often the case that problems become easy, once you think at them in the appropriate context. It is much easier to develop the code for the difference of intervals, as we have done, and then move on to difference of rectangles, rather than trying to write code for rectangle difference directly. The result is also far more elegant. # Let us test visually what happens. r = Rectangle((0., 4.), (0., 4.), name="R") t = Rectangle((2., 6.), (2., 6.), name="T") d = rectangle_difference (r, t) draw rectangles (r, t) draw rectangles (*d) [ ] # Whooho!! Let's try with another example. Now T is inside R. r = Rectangle((0., 4.), (0., 4.), name="R") t = Rectangle((1.5, 3.5), (1., 3.), name="T") d = rectangle_difference (r, t) draw rectangles (r, t) draw rectangles (*d)
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning