代写CS python算法代写 简单的一个作业代写Homework A: Classes

0 Comments

Homework A: Classes

CS-UY 1114 NYU Tandon

Download the code file hwa.py and rename it to YourNetID_hwa.py. You will modify this file as specified in the problems below, and submit it to Gradescope.

Modify the file only as specified in the problems, by replacing the pass lines as instructed. Do not modify any other areas of the file.

Introduction

Read the provided code file thoroughly.

In it, we present two classes: Point, for representing a point in two-dimensional space; and Rectangle, for representing a rectangle.

Each instance of Point contains a pair of floats. This should be intuitive to you.

Each Point knows how to draw itself using the draw method. For example, I can create a Point and make it draw with the following code:

>> p = Point (70.0 , 10.0)
>> p. draw ()

Each Point can also move itself with its move method. In the following code, I create a new Point instance, print out its coordinates, move it, and then print out its new coordinates.

>>> p = Point (10.0 , 20.0)
>>> print (p.x, p.y) 10.0 20.0
>>> p. move (7.0 , -5.0)
>>> print (p.x, p.y) 17.0 15.0

Note that the move method updates the value stored in the instance of Point, but it does not change the position of any point drawn on the screen. Therefore, drawing the point, then moving it, then drawing it again, will result in two distinct points appearing on the screen.

Read and then run the point_move_test function from the file to observe the effect of various methods on Point.

Problems

Problem 1

You have been given an incomplete implementation of a Rectangle class, which describes a rectangle on a two-dimensional plane.

Rectangle is defined in terms of Point. Each instance of Rectangle has two member

, and the other at , we can identify the following Rectangle:

Note that the position of the remaining two corners of the rectangle can be inferred from the position of the two given.

Complete the height and area methods of Rectangle, according to the specification given in the code.

Your area method must be implemented by calling the Rectangle’s height and width

methods. Do not access the underlying Points directly.

Problem 2

Complete the diagonal_length method of Rectangle, according to the specification given in the code.

You must implement diagonal_length by calling Point’s distance method. You may

not use the math.sqrt or the exponentiation operators directly in your code.

Test your code by running the function rectangle_area_test. If you’ve implemented

height, area, and diagonal_length correctly, you should get this output:

>>> rectangle_area_test () r1 has width 110.0
r1 has height 110.0 r1 has area 12100.0
r1 has diagonal length : 155.56349186104046

(The final value is approximate; your answer may have fewer decimal places.)

Problem 3

Complete the move and draw methods of Rectangle, according to the specification given in the code.The draw method draws a rectangle on the turtle canvas according to the current values stored in the Rectangle instance. You must implement draw by using only the following turtle functions: turtle.penup, turtle.pendown, and turtle.goto.The move method just changes the location of the rectangle (i.e. of both of its points) by the horizontal and vertical offsets specified in its parameters. The function does not draw,

and your implementation may not use turtle at all. You must implement move by calling Point’s move method. Test your code by running the function rectangle_move_test. If you’ve implemented the methods correctly, you should get this output:

Problem 4

Complete the overlaps method of Rectangle, according to the specification given in the code.

The overlaps method should return a bool indicating if a given Rectangle (identified by the parameter other) overlaps (i.e. shares area) with the present rectangle (identified by the parameter self). It should return a bool.

Hint: consider overlapping in the following manner.

• If a rectangle is entirely to the left of another rectangle (i.e. its rightmost point is to the left of the other’s leftmost point), then they don’t overlap.

• If a rectangle is entirely to the right of another rectangle (i.e. its leftmost point is to the right of the other’s rightmost point), then they don’t overlap.

• If a rectangle is entirely above another rectangle (i.e. its bottommost point is above the other’s topmost point), then they don’t overlap.

• If a rectangle is entirely below another rectangle (i.e. its topmost point is below the other’s bottommost point), then they don’t overlap.

• If none of the above conditions apply, then they overlap.

Test your code by running the function overlap_test. If you’ve implemented the meth- ods correctly, you should get this output:

>>> overlap_test ()
r1 and r2 overlap ? True r3 and r4 overlap ? False

Problem 5

Complete the intersection method of Rectangle, according to the specification given in the code.The intersection method should return a new Rectangle instance, identifying the area that is shared between other and self. If they don’t overlap, then it should return an “empty” Rectangle.This function has already been partly implemented for you. Notice that it calls overlaps, so make sure that you’ve correctly implemented that function first.

Hint: you may want to use the min and max functions.

Test your code by running the function intersection_test. If you’ve implemented the methods correctly, you should get this output:

Area of interesection : 600.0

Notice the small red rectangle formed by the intersection of the black rectangles.

Problem 6

You have been given an incomplete implementation of the Line class. Complete the Line

class according to the following specification.

Each instance of Line should identify a line on a two-dimensional plane. It must have the following methods:

• The constructor (def init (self, first, second)) must take self as well as two additional parameters, both of type Point.

• The method draw (def draw(self)) takes no parameters except self and returns None. It should draw the line on the turtle canvas, using only the following functions: turtle.penup, turtle.pendown, and turtle.goto.

• The method slope (def slope(self)) takes no parameters except self and re- turns a float identifying the slope of the current line. If the line has no slope, raise a ValueError with an appropriate message.

Test your code by running the function line_slope_test. If you’ve implemented the methods correctly, you should get this output:

>>> line_slope_test () l1 has slope 1.0
l2 has slope -0.5

Comments