Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON PROGRAMMING . internal form: Graphics is a core foundation of computing technology. Although it is common to hear this word in gaming contexts, graphics

PYTHON PROGRAMMING

.

image text in transcribedimage text in transcribed

internal form:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Graphics is a core foundation of computing technology. Although it is common to hear this word in gaming contexts, graphics can be seen in every aspect of using a computer and it is needed as long as somebody is staring at a screen. Now, let's pretend that it is the mid-1980s and Python 3 apparently existed at that time. You are just hired for an entry-level programmer position at a firm who is at the forefront of research for 2D computer graphics. In your first few weeks, you are tasked together with fellow new co-workers to create a system that draws certain shapes on the screen. They are not yet interested in adding fill colors or having the ability to change the stroke width, but they are interested in drawing "primitive" shapes, such as ellipses, triangles, and quadrilaterals onto some "canvas" on the screen. The task is a tall order, so for now, it is alright for them to treat the canvas as a bag of shapes so that the total area and perimeter is the sum of all of these individual properties of the shapes. Hence, the position of the shapes are not important For the task, you need to first implement the following classes. These classes form the foundation of our system. Shape - generic shape . Point - 2D point Vector2D - 2D vector Polygon - general convex and closed polygon BagOfShapes - bag of shapes Since you are working with a team, you are also tasked to implement one of the following classes of Shapes: Quad - quadrilateral Triangle - triangle Ellipse - ellipse You are told by your supervisor to check this internal form to determine the specifications for the foundational classes and which of the Shape classes you are going to implement. As this is apparently also an internal coding test launched by your firm, your assigned problem statement includes a password that you have to supply as a global variable test_pass in your code. 3. Ellipse Shape Make a class Shape that represents a shape in our canvas. It should have the following properties, which can be initialized from_init__(**kwargs): area - float; area of shape; default to zero perimeter - float; perimeter of shape; default to zero name - string; name of shape; default to "shape" shape_type-string class property; type of shape; default to "shape" Printing a Shape should yield . The area of a vector is zero, its perimeter is its magnitude relative to the origin, and its type is vec2d. We should also be able to add and subtract two Vector2Ds, and invert (.e. negate) a vector. Finally, it should also have the following additional methods: from_point(point) - class method to convert a Point to Vector 2D dot(other) - dot product between itself and other; returns a float det(other) - signed magnitude of the cross product between itself and other; returns a float Printing a Vector2D should yield (end.x, end.y). Polygon Make a class Polygon that extends Shape representing a polygon. It represents a polygon as a list containing the Points of the polygon listed in counterclockwise (ccw) order. It is assumed that the polygon is closed and convex. The area of such a polygon can be computed via Green's Theorem, its perimeter by adding the lengths of the lines forming it, and its shape_type is poly. Note that on initialization, the list of points may not be in CCW order, so normalization may be needed for the internal list to be in ccw order. Bag of Shapes Make a class BagOfShapes representing the bag where we will put our Shapes. It has a property name denoting the name of the bag. It should also have the following methods: add(shape) - add Shape shape into the bag remove(name) - remove Shape with name name from the bag total area() - get the area of all Shapes in the bag total_perimeter() - get the perimeter of all Shapes in the bag It should also implement the following special methods: _add_(other) - merge this bag and BagOfShapes other such that the Shapes inside of the two bags are together. This should return a new BagOfShapes. _len_() - number of Shapes in the bag _getitem_(key) - get Shape with name key from the bag _setitem_(key, val)-add Shape with name key and shape val into the bag _contains_(item) - True if Shape with name item is in the bag Ellipse Make a class Ellipse that extends Shape. It represents an ellipse that has the float properties a and b representing the a and b semi-major and minor axes in the standard ellipse equation, respectively. Its type is ellipse. In addition to having an area and perimeter computed from its arc length, it has an additional float property e denoting its eccentricity. It shold also have the following methods: is_circle() - True if it is a circle Password: biluhaba_0 Input Format The first line of the input consists of a number T denoting the number of testcases that will follow. It will then be followed by T blocks of lines. Each testcase starts with a number n denoting the number of commands. The next n lines denote a command sent to the bag of shapes system. Each line has this format cmd args where cmd is a command keyword and args a space-separated list of command arguments. cmd will only be from one of the following examples: . - . add tri shape_name 0 0 2 0 1 2 - add an isosceles Triangle starting at (0,0) in cow order add quad shape_name 0 0 1 0 1 1 0 1 - add an equiangular and equilateral Quadrilateral starting at(0, 0) in cow order add ellipse shape_name 10 10 -add an Ellipse with the same semi-major and minor axes Please check the sample input(s) for more information. Constraints Input Constraints T b, a, b > 0 cmd {create_bag, select_bag, bags_add, add, remove, get, get_shape_prop, exec_shape_method, len, total_area, total_perimeter) shape_type E {point, vec2d, line_seg, poly, tri, quad, ellipse} Unless specified for a specific shape, numbers v supplied in args or shape_args are always integers such that 0 Create bag bag_a select_bag bag_a -> Select bag bag_a bags_add bag_c bag_a bag_b -> Add bags bag_c Add shape shape_name of type shape_type into bag selected_bag_name remove shape_name => Remove shape shape_name from bag selected_bag_name get shape_name -> shape_name: str(shape) get_shape_prop shape_name prop_name -> shape_name.prop_name: str(shape_name.prop_name) exec_shape_method shape_name method_name -> shape_name.method_name(): str(shape_name.prop_name) len > Bag selected_bag_name size: size total area -> Bag selected_bag_name area: total_area total_perimeter -> Bag selected_bag_name perimeter: total_perimeter Please check the sample output(s) for more information. Sample Input 0 1 15 create_bas baz_a select_bag baz_a add point point a 0 l add vec2d vec a 1 2 add line_seg ls_a 1050 add poly poly_a 4 00101101 get point_a get vec_a get ls_a get poly_a len get_area get_perimeter get_shape_prop ls_a start exec_shape_method ls_a vector Sample Output 0 Case #1: Create bag bag_a Select bag bag_a Add shape point_a of type point into bag bas_a Add shape vec_a of type vec2d into bag bas_a Add shape ls_a of type line_seg into bag bag_a Add shape poly_a of type poly into bag baz_a point_a: (0, 1) vec_a: ls_a: (1, 0) > (5,0) poly_a: Bag bas a size: 4 ls_a.start: (1, 0) ls_a.vector(): Sample Input 1 2 10 create_bag bag_a select_bag bag_a add point point_a @ 1 add point point_b 1 2 add point point_c 2 3 len remove point_b len remove point_a len 11 create_bag bag_a create_bag bag_b select_bag bas_a add point point_a Ol select_bag bag_b add poly poly_a 3 0 0 2012 len total_area select_bag bag_a len total_area Sample Output 1 Case #1: Create bag bag_a Select bas baz_a Add shape point_a of type point into bag baz_a Add shape point_b of type point into bag baz_a Add shape point_c of type point into bag bas_a Bag bas_a size: 3 Remove shape point_b from bag bag_a Bag bas a size: 2 Remove shape point_a from bag bas_a Bag bag a size: 1 Case #2: Create bag bas_a Create bag baz_b Select bag bag_a Add shape point_a of type point into bag baz_a Select bag bag_b Add shape poly_a of type poly into bag bas_b Bag bas_b size: 1 Bag bag_b area: 2.00 Select bag bag_a Bag bag a size: 1 Bag bag_a area: 0.00 import math # TODO: Set password here test_pass = None # No need to change or amend this class! class Shape: shape_type = 'shape! def __init__(self, **kwargs) : self. area = 0 self.perimeter = kwargs['perimeter'] if 'perimeter in kwargs else self.area = kwargs['area'] if 'area' in kwargs else e self.name = kwargs['name'] if 'name' in kwargs else 'shape! = def __str__(self): return f' # No need to change or amend this class! class Point(Shape): shape_type = 'point' def --init__(self, x, y, **kwargs) : super().-_init__(**kwargs) self.x self. y def --setitem__(self, idx, val): if idx == 0: self.x = val elif idx == 1: self. y = val else: raise IndexError("Out of bounds') def --getitem__(self, idx): if idx == 0: return self.x elif idx == 1: return self.y else: raise IndexError('Out of bounds) def --str__(self): return f'({self.x}, {self.y}) # FIXME: Fill in class Vector2D below class Vector2D(Point): shape_type = 'vec2d @classmethod def from_point(cls, point): pass # Somebody has filled in this part. :0 def __init__(self, x, y, **kwargs): super(). __init__(x, y, **kwargs) self.perimeter = math.dist((x, y), (0, 0)) def --add__(self, other): pass # Somebody has filled in this part too! :0 def __sub__(self, other): # Type dispatch if isinstance (other, Vector2D): return self + (-other) elif type (other) is tuple or type (other) is list: return self + Vector2D(-other[0], -other[1]) + def __neg__(self): pass def --str__(self): return # ... and this one! :0 def det (self, other): return self.x * other.y self.y * other.x def dot (self, other): pass # FIXME: Fill in class LineSegment below class LineSegment (Shape): shape_type = 'line_seg' # Who has been filling these parts? :0 def __init__(self, start, end, **kwargs): super().-_init__(**kwargs) # Type coercion if type (start) is tuple or type (start) is list: start = Point (start[o], start[1]) if type (end) is tuple or type (end) is list: end = Point(end[0], end[1]) self.start = start self.end = end self.perimeter = math.dist(self.start, self.end) def vector (self): pass def __str__(self): pass # FIXME: Amend class Polygon below # Feel free to add more methods inside this -class Polygon (Shape): shape_type = poly' def __init__(self, points_list, **kwargs) : super(). __init__(**kwargs) self.points_list = [] # FIXME: Amend class BagOfShapes below class BagofShapes: # Stop helping the newbie, creepy code hacker! >:( def __init__(self, shapes=None, name='bag_of_shapes'): self.shapes = () if type (shapes) is not dict else shapes self.name = name def add(self, shape): pass def remove (self, name): pass def total area (self): pass def total_perimeter (self): pass def __add__(self, other): pass def _len__(self): pass def --getitem__(self, key): pass def --setitem__(self, key, val): pass def --contains__(self, item): pass # TODO: Add any additional class(es) below this comment # NOTE: Do not change anything inside this main() function to # make your life easier. :> def main(): int(input) n_testcases = for t in range (n_testcases): shape_bags = print(f'Case #{t - 1}:') n_commands = int(input) current_bag_key for in range (n_commands): cmd_line input().split cmd - cmd_line[o] if cmd == 'create_bag': # CMD: create_bag bag_a # Create a BagofShapes with name bag_a shape_bags [cmd_line[1]] = BagofShapes (name=cmd_line[1]) print(f Create bag (cmd_line[1]}') elif cmd == 'select_bag': # CMD: select_bag bag_a # Select the BagofShapes bag a to manipulate current_bag_key cmd_line[1] = print(f'Select bag {cmd_line[1]}') elif cmd == 'bags_add': # CMD: bags_add bag_c bag_b bag_a # Merge BagofShapes bag_b and bag_a, and that resulting # bag will become bag_ dest = cmd_line[1] a, b = cmd_line[2], cmd_line [3] + shape_bags [b] shape_bags [dest] shape_bags[a] shape_bags (dest].name = dest print(f'Add bags {dest} 0: if cmd == 'len': # CMD: len # Get size of the currently selected BagofShapes print(f Bag {current_bag_key} size: {len (shape_bags [current_bag_key])}') elif cmd -- "total_area': # CMD: total_area # Get total area of the currently selected BagOfShapes print (f.Bag {current_bag_key} area: shape_bags [current_bag_key]. total area:.2f}') elif cmd == 'total_perimeter': # CMD: total perimeter # Get total perimeter of the currently selected BagofShapes print (f'Bag {current_bag_key] perimeter: shape_bags [current_bag_key].total_perimeter():.2f}') 1 if -_name__ == '__main__': main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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 Databases questions