Download presentation
Presentation is loading. Please wait.
Published byGertrude Horton Modified over 9 years ago
1
1. MORE TRIG (AND BASIC VECTORS) 2. INHERITANCE (CHAPTER 9) Lecture 10 (Last one!)
2
Part I: More Trig Recall: Angles (orientation and rotation) Polar vs. Cartesian Coordinates (and offsets) Polar => Cartesian conversion for “asteroids-style” movement (vs. normal “pacman” style movement) What we still need Cartesian => Polar conversion (and applications) Generalize the idea of a vector (and look at a few properties) Define acceleration and velocity and their applications.
3
Cartesian => Polar coordinates Motivation: Control orientation with the mouse. Previously we knew H and θ; We wanted to calculate Δx and Δy Δx is the amount to change our x-position. Δy is the amount to change our y-position. Now, however, want to calculate θ (from Δx and Δy) X rotSurf = pygame.transform.rotate( origSurf, degrees)
4
Cartesian => Polar, cont. θ A O H X We know initially: The position of the plane (x, y) The position of the mouse (mx, my) We want to calculate: The angle θ First step: We can calculate A and O below using our givens: A = mx – x O = my - y Note: it does make a difference which order you do the sub. Think of both as directions negative = left. positive = right. Similar for O
5
Example Cartesian => Polar (here using arcsin) Givens: Plane at (50, 500) Target at (200, 150) Step1: Calculate offsets towards target Note how we take destination minus source Step2: Calculate hypotenuse length Step3: Calculate theta 200-50 = +150 150-500 = -350 (50,500) (200,150)
6
Cartesian => Polar, Problem with asin ( and acos ) θ 200-50 = +150 150-500 = -350 (50,500) (200,150) φ (-100)-50 = - 150 150-500 = -350 (50,500) (-100,150) WRONG! ψ should be ≈ 113 degrees Asin ignores the sign of the horizontal direction. δ If we're in quadrant II or III, we need the complement of ψ (180 – ψ).
7
Cartesian=>Polar, problem with atan.
8
Cartesian => Polar
9
Conversions finished
10
Example [Making object point towards a point]
11
Part III. Inheritance Overview Basically, basing a new (derived) class from an existing one (base). You "inherit" all methods (and attributes) from the base class. You then: Add new methods Replace existing methods Although you can still call the base class's version
12
Abstract example class Base(object): def __init__(self, x): self.num = x def foo(self, string): return string * self.num class Derived(Base): def bar(self): return self.num ** 2 def foo(self, string): # REPLACES "inherited" foo return string * self.bar() A = Derived(4) # Note, we're using Base's __init__ method. print(A.foo("ABC")) # Calling the replaced method print(A.bar()) # Call a new method. One problem: If we want to add new attributes to Derived…
13
Calling (hidden) base class methods. class A(object): def f(self): print("A.f") class B(A): def f(self): print("B.f") obj = B() obj.f() # Print's B.f B.f(obj) # Same as above It appears as if we've replaced A's version of f, but it's actually just hidden.
14
Calling (hidden) base class methods, cont. class A(object): def f(self): print("A.f") class B(A): def f(self): A.f(self) # Calls the "hidden" f. print("B.f") obj = B() obj.f() # Print's 'A.f' then 'B.f' When you call base class methods like this, it is the only time self isn't passed automatically.
15
Adding attributes to a new class class Base(object): def __init__(self, x): self.num = x def foo(self, string): return string * self.num class Derived(Base): def __init__(self, x, y): Base.__init__(self, x) self.num2 = y def bar(self): return self.num ** 2 + self.num2 ** 2 A = Derived(4, 6) # Calling the new __init__ method. print(A.foo("ABC")) # Calling the inherited method print(A.bar()) # Call a new method.
16
isinstance function Used to determine if an object is of a given type isinstance(5, int) # True isinstance("5", int) # False isinstance("5", str) # True class Foo(object): … x = Foo() isinstance(x, Foo) # True
17
isinstance and inheritance A derived class object is considered: an instance of the derived class (no surprise) an instance of the base class class A(object): … class B(A): … x = A() y = B() isinstance(x, A) # True isinstance(x, B) # False isinstance(y, B) # True isinstance(y, A) # True
18
Example [Make the spaceship into a base class and add enemies that fire on the player]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.