Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Edit only the class definition. DO NOT CHANGE the code given under 'main' please.

Steps:

Additionally implement any Python Magic/Dunder methods such that instances of the class minimally:
1. Support addition, subtraction, equality operations and the built-in abs function
2. Are Iterable i.e., support for loops and star arguments for unpacking into function calls
3. Support a string representation that displays the class name and coordinates stored by the instance: i.e. for an object initialized as: Vector(0, 3), the string representation should be ‘Vector(0, 3)’
The code given under main tests for each of the program requirements and subsequently uses the turtle module to plot randomly generated points rotated. A screenshot for a sample run of the program is attached.

 

Template.py:

from math import hypot, pi
from random import uniform
import turtle as t


class Vector2D:
    ...


if __name__ == '__main__':
    # Test Vector class
    a, b = Vector2D(0, 3), Vector2D(0, -3)
    tests = [(str(a), 'Vector2D(0, 3)'),
             (repr(a), 'Vector2D(0, 3)'),
             (a+b, Vector2D(0, 0)),
             (a-b, Vector2D(0, 6)),
             (abs(a), hypot(*a)),
             (a.dot(b), -9),
             (a.scale(3), Vector2D(0, 9)),
             (a.direction(), Vector2D(0.0, 1)),
             (a.distance(b), 6),
             (a.rotate(pi), b),
             ([x for x in a], list(a)),
             (tuple(a), (0, 3))]

    for i, test in enumerate(tests, 1):
        assert test[0] == test[1], f'Test {i}: FAIL {test[0]} != {test[1]}'

    # All tests passed !!!
    # Start drawing
    t.setworldcoordinates(-150, -150, 150, 150)
    t.title('We Spin')
    t.tracer(1000)
    t.pu()

    # generate 100 points on y-axis between -50 and 50
    points = [Vector2D(uniform(-50, 50), 0) for i in range(100)]

    # generate 20000 new points by transforming initial points
    spin_points = [p.rotate(400/i*pi)+p.scale(2) for p in points for i in range(1, 201)]

    # plot spin points
    for p in spin_points:
        t.goto(*p)
        t.pencolor(uniform(0, 1), uniform(0, 1), uniform(0, 1))
        t.dot(5)
    t.done()

 

We Spin
X
expand button
Transcribed Image Text:We Spin X
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education