class Point: """ A point in a two-dimensional coordinate plane """ def __init__(self, x, y): """ Create a point with an x and y coordinate """ self.x = x self.y = y def __str__(self): """ Generate a string representation of a point """
class Point:
"""
A point in a two-dimensional coordinate plane
"""
def __init__(self, x, y):
"""
Create a point with an x and y coordinate
"""
self.x = x
self.y = y
def __str__(self):
"""
Generate a string representation of a point
"""
return("(" + str(self.x) + "," + str(self.y) + ")")
# Part 1 - Rectangle Class
class Rectangle:
"""
A rectangle in a two-dimensional coordinate plane
"""
def __init__(self, bottom_left_x, bottom_left_y, top_right_x, top_right_y):
"""
Create a rectangle defined by its bottom left and top right corner
coordinates
"""
self.bottom_left = Point(bottom_left_x, bottom_left_y)
self.top_right = Point(top_right_x, top_right_y)
self.bottom_right = Point(top_right_x, bottom_left_y)
self.top_left = Point(bottom_left_x, top_right_y)
def __str__(self):
"""
Generate a string representation of a rectangle
"""
return ("Rectangle with corner coordinates " +
str(self.bottom_left) + ", " + str(self.top_right))
def move(self, horizontal_translation, vertical_translation):
"""
(Rectangle, int, int) -> None
Alters the location of a rectangle by translating the coordinates
of its bottom left and top right corner coordinates.
"""
self.bottom_left.x += horizontal_translation
self.bottom_left.y += vertical_translation
self.top_right.x += horizontal_translation
self.top_right.y += vertical_translation
self.bottom_right.x += horizontal_translation
self.bottom_right.y += vertical_translation
self.top_left.x += horizontal_translation
self.top_left.y += vertical_translation
def overlap(self, rectB):
"""
(Rectangle, Rectangle) -> bool
Checks whether two rectangles overlap
"""
return not (horizontal_clearance or vertical_clearance)
# Part 2 - Wind Turbine Class
class WindTurbine:
"""
A wind turbine placed in a two-dimensional area
"""
def __init__(self, id_number, placement_bottom_left_x, placement_bottom_left_y,
placement_top_right_x, placement_top_right_y):
"""
Create a wind turbine
"""
self.id_number = id_number
self.r1 = Rectangle(placement_bottom_left_x,placement_bottom_left_y,
placement_top_right_x, placement_top_right_y)
self.overlapping_turbines = []
self.placement = str(self.r1)
def __str__(self):
"""
Generate a string representation of a WindTurbine object
"""
return ("Wind Turbine ID: " + str(self.id_number) +
", Placement: " + str(self.placement))
def move(self, horizontal_translation, vertical_translation):
"""
(WindTurbine, int, int) -> None
Alters the location of a wind turbine by translating the coordinates
of its bottom left and top right corner coordinates. After moving the
turbine, the overlapping turbine list should be reset to an empty
list.
The change in the x and y coordinates are specified by the
horizontal_translation and vertical_translation parameters, respectively.
"""
## TODO complete the method
self.r1.move(horizontal_translation, vertical_translation)
self.placement = str(self.r1)
self.overlapping_turbines = []
def overlap(self, turbineB)-> bool:
"""
(WindTurbine, WindTurbine) -> bool
Checks for overlap between a wind turbine and another turbine (turbineB).
"""
return self.r1.overlap(turbineB)
def validate_placement(self, turbineB):
"""
(WindTurbine, list of WindTurbines) -> None
Check if the postion of a wind turbine is valid by checking for
overlapping areas with all other wind turbines.
"""
for t in turbineB:
if(self.overlap(t)) and self!=t:
self.overlapping_turbines.append(t)
## TODO complete the method
##########################################
# Part 3 - Testing Wind Turbine Placement
##########################################
def check_turbine_placements(turbines):
"""
(list of WindTurbines) -> int
Checks a list of wind turbines to identify turbines with invalid (overlapping)
placements. The function should return the number of turbines with
invalid placements.
All placements should be evaluated using the validate_placement method from
the WindTurbine class.
"""
## TODO complete the function
count =0
for i in turbines:
if (not i.validate_placement()):
count+=1
return count
(FIX ERRORS !)
Step by step
Solved in 4 steps with 1 images