Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLease see the attached oictures. Class Canvas represents a collection of Rectangles. It has 8 methods. In addition, to the constructor (i.e. __init__ method) and

PLease see the attached oictures.

Class Canvas represents a collection of Rectangles. It has 8 methods. In addition, to the constructor (i.e. __init__ method) and two methods that override python's object methods (and make your class user friendly as suggested by the test cases), your class should contain 5 more methods: add_one_rectangle, count_same_color, total_perimeter, min_enclosing_rectangle, and common_point. Here is a description of those methods whose job may not be obvious from the test cases. * The method total_perimeter: returns the sum of the perimeters of all the rectangles in the calling canvas. To compute total perimeter do not compute a perimeter of an individual rectangle in the body of total_perimeter method. Instead use get_perimeter method from the Rectangle class. * Method min_enclosing_rectangle: calculates the minimum enclosing rectangle that contains all the rectangles in the calling canvas. It returns an object of type Rectangle of any colour you prefer. To find minimum enclosing rectangle you will need to find the minimum x coordinate of all rectangles, the maximum x coordinate for all rectangles, the minimum y coordinate and the maximum y coordinate of all rectangles. * Method common_point: returns True if there exists a point that intersects all rectangles in the calling canvas. To test this (for axis parallel rectangles like ours), it is enough to test if every pair of rectangles intersects (according to a Helly's theorem for axis-alligned rectangles: http://en.wikipedia.org/wiki/Helly's_theorem ). *** Finally recall, from the beginning of the description of this assignment that each of your methods should have a type contract.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
> > > r1 r1=Rectangle(Point( ), Point( 1,1), "red" ) Rectangle(Point (0,0), Point(1,1), 'red' ) >>> rl.get_color( ) 'red' > > > r1.get_bottom_left( ) Point(0,0) >>> rl.get_top_right( ) Point(1, 1) > > > r1. reset_color( "blue" ) > > > 'blue rl.get_color( ) > >> r1 Rectangle(Point (0, 0), Point(1, 1), 'blue' ) > > > r1. move( 2, 3) > > > r1 Rectangle(Point (2, 3), Point(3,4), 'blue ' ) > > > > > > print(r1) I am a blue rectangle with bottom left corner at (2, 3) and top right corner at (2, 3) . > > > > > > r > > > r2 r2=eval(repr(r1) ) > > > Rectangle(Point (2, 3) , Point(3,4), 'blue' ) > > > r1 is r2 False > > > r1==r2 True > > > r3=Rectangle(Point( ), Point(2, 1), "red") > > > r3 . get_perimeter ( ) r4=Rectangle(Point(1, 1), Point(2, 2.5), "blue" ) > >> r4.get_area ( ) 1.5 > > > r5=Rectangle(Point (1, 1), Point(2, 2.5), "blue") > > > r4 == r5 True > > > r4 is r5 False > > > > > > > > > 1 r=Rectangle(Point(1,1), Point(2,5), "blue" ) > > > r. contains(1.5,1) True > > > r. contains(2, 2) True > > > r. contains(0, 0) > > > > > > > > > > > > ===== RESTART === > > > > > > >> > r1=Rectangle(Point (1, 1), Point(2, 2), "blue" ) r2=Rectangle(Point (2, 2.5), Point(3, 3), > > > r3=Rectangle(Point(1. 5, 0), Point(1.7, 3) , "red" ) , "blue" ) > > > > > > print(r3)I am a red rectangle with bottom Left corner at ( 1 . 5 , 6) and top right corner at ( 1 . 7 . 3 ) . > > > (1 . intersects ( 12 ) FALSE\\ > > > r2 . intersects ( -1 ) False\\ > > > rl . intersects ( 13) True > > > r2 . intersects ( 13) False = = = = = = = RESTART = = = = = = = = = = = > > > [ = Canvas! ) > > > Len ( C ) > > > r1 = Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2 ) . " > > > ( 2 = Rectangle ( Point ( 2, 2. 5) , Point ( 3 , 3 ) .` 1. " blue " ) . " blue " ) > > > > > C . add_ one_ rectangle ( 13 )| > > > [ . add_ one_ rectangle ( Rectangle ( Point ( 8 , 0 ) , Point ( lab , 198 ) , " orange " ] ] > > > len ( C ) Canvas ( [ Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2 ) , ' blue ' ] , Rectangle ( Point ( 2 , 2 . 5 ) , Point ( 3 , 3 ) , ' blue ' ) , Rectangle ( Point ( 1 . 5 , 6 ) , Point ( 1 . 7 , 3 ) , ' red ' ) , Rectangle ( Point ( 6 , 8 ) , Point ( 100 , 180 ) , ' orange ' ) ] ) > > > [ . count_ same_ color ( " blue " ) > > > [ . count_ same_ color ( " red " ! > > > [ . count_ same_ color ( " purple " ) = = = = = RESTART = = [ = Canvas (! ) > > > (1 = Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2 ) , " blue " ] > > > ( 2 = Rectangle ( Point ( 1 , 1 ) , Point ( 4 , 4 ) , " blue " ) > > > > > [ . add_ one_ rectangle ( -1 ) > > > [ . add_ one_ rectangle ( 1 2 ) > > > [ . add_ one_ rectangle ( 1 3 ) > > > [ . total_ perimeter ( !) 20 = = = = = = = RESTART = = = = = = > > > [ = Canvas ( ) > > > r1 = Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2 ) , " BLUE " > > > ( 2 = Rectangle ( Point ( 1 , 1 ) , Point ( 4 , 4 ) ,` }, " blue " ) > > > > > 1 4 = Rectangle ( Point ( 0, - 109 ) , Point ( 1 , 180 ) , " yellow " ) c . add one rectangle ( (1 )> > > [ . count_ same_ color ( " purple " ) B > > > = = = =ZZZ Z RESTART = = = = ` > > > [ = Canvas ( )| > > > (1 = Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2 ) , " blue " ) > > > ( 2 = Rectangle ( Point ( 1 , 1 ) , Point ( 4 , 4 ) , " blue " ) > > > > > [ . add_ one_ rectangle ( (1 ) > > > [ . add_ one_ rectangle ( 12 ) > > > [ . add one_ rectangle ( 13 ) > > > [ . total_ perimeter ( !) 20 = = = = = RESTART = = = = = ` = = = = = = = = = = > > > [ = Canvas ( )| > > > ( 1 = Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2 ) , " blue ") > > > ( 2 = Rectangle ( Point ( 1 , 1 ) , Point ( 4 , 4 ) , " blue "] > > > > > 1 4 = Rectangle ( Point ( @ , - 180 ) , Point ( 1 , 108 ) , " Yellow " ) > > > [ . add one_ rectangle ( (1 ) > > > [ . add_ one_ rectangle ( 12 ) > > > [ . add_ one_ rectangle ( 13 ) > > > [ . add_ one_ rectangle ( 14 ) > > > [ . min _ Enclosing_ rectangle ( ) Rectangle ( Point ( - 2 , - 180 ) , Point ( 4 , 180 ) , ' red ' ) = = RESTART = = = = > > > [ = Canvas ( ) > > > (1 = Rectangle ( Point ( 1 , 1 ) , Point ( 2 , 2) , " blue ' ) > > > r 2 = Rectangle ( Point ( 1 . 5 , 1 . 5 ) , Point ( 4 , 4 ) , " BLUE" ) > > > > > 1 4 = Rectangle ( Point ( 0, - 180 ) , Point ( 1 . 5 , 109 ) , " yellow " ) > > > [ . add one_ rectangle ( (1 ) > > > [ . add_ one_ rectangle ( 12 ) > > > [ . add_ one_ rectangle ( 13 ) > > > [ . add_ one_ rectangle ( 1 4 ) > > > [ . common_ point ( )| True = = = = = = = = = RESTART = = = = = = = = =` ` = = = = = = = = = = = = = = = = = = = = = > > > [ = Canvas ( )| > > > (1 = Rectangle ( Point ( - 2, - 2) , Point ( - 1 , 2 ) , " blue ") > > > ( 2 = Rectangle ( Point ( - 2, - 2) , Point ( 2 1 - 1 ) ,` . " LIVE ") > > > > > ( 4 = Rectangle ( Point ( - 2, 1 ) , Point ( 2 , 2 ) , " blue " ) > > > [ . add_ one_ rectangle ( (1 ) > > > [ . add_ one_ rectangle ( -2 ) > > > [ . add_ one_ rectangle ( 1 3) > > > [ . add_ one_ rectangle ( 14 ) > > > [ . common_ point ! ) Falseclass Point: 'class that represents a point in the plane' def init (self, xcoord=0, ycoord=0) : ( Point, number, number) -> None initialize point coordinates to (xcoord, ycoord) ''' self. x = xcoord self. y = ycoord def setx (self, xcoord) : ( Point , number) ->None Sets x coordinate of point to xcoord''' self. x = xcoord def sety (self, ycoord) : ( Point , number ) ->None Sets y coordinate of point to ycoord''' self. y = ycoord def get (self) : " ' ' ( Point) ->tuple Returns a tuple with x and y coordinates of the point''' return (self.x, self.y) def move (self, dx, dy) : ' ' ' ( Point , number , number ) ->None changes the x and y coordinates by dx and dy ''' self . x += dx self. y += dy def eq (self, other) : "'' ( Point , Point) ->bool Returns True if self and other have the same coordinates''' return self.x == other. x and self.y == other.y def repr (self) : "'' ( Point ) ->str Returns canonical string representation Point (x, y) ' '' return ' Point ( '+str (self.x) +' , '+str (self.y) +' ) ' def str (self) :initialize point coordinates to (xcoord, ycoord) ) ! 1 1 self. x = xcoord self. y = ycoord def setx (self, xcoord) : 1 1 1 ( Point , number) ->None Sets x coordinate of point to xcoord''' self. x = xcoord def sety (self, ycoord) : ' ' ' ( Point , number) ->None Sets y coordinate of point to ycoord''' self. y = ycoord def get (self) : " ' ' ( Point) ->tuple Returns a tuple with x and y coordinates of the point''' return (self.x, self.y) def move (self, dx, dy) : '' ' ( Point , number , number) ->None changes the x and y coordinates by dx and dy ''' self . x += dx self. y += dy def eq _(self, other) : "' ' ( Point, Point ) ->bool Returns True if self and other have the same coordinates''' return self. x == other. x and self.y == other .y def repr (self) : "'' ( Point) ->str Returns canonical string representation Point (x, y) ' '' return ' Point ( '+str (self. x) +', '+str (self.y) + ' ) ' def str (self) : "'' ( Point) ->str Returns nice string representation Point (x, y) . In this case we chose the same representation as in repr return ' Point ( '+str (self. x) +', '+str (self.y) +' ) '

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions

Question

In a Defined Benefit Plan who has the investment risk?

Answered: 1 week ago

Question

1. How do most insects respire ?

Answered: 1 week ago

Question

Who is known as the father of the indian constitution?

Answered: 1 week ago

Question

1.explain evaporation ?

Answered: 1 week ago

Question

Who was the first woman prime minister of india?

Answered: 1 week ago