Python Forum
3D vector class with inheritance from 2D vector class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D vector class with inheritance from 2D vector class
#1
I am trying to make a scalar multiplication method which is the last method in the code. I am not sure why it is not working. I used "return D3Vector((self.x * num), (self.y * num), (self.z * num))" It is returning only the new x and y coordinates when I use print(v4.scalarMult(3)), for example.
Here is the full code.
import math
class Vector:
  def __init__(self, x, y):
    self.x = x
    self.y = y
  
  def get_x(self):
    return self.x
  
  def set_x(self, x):
    self.x = x
  
  def get_y(self):
    return self.y
  
  def set_y(self, y):
    self.y = y
  
  def __str__(self):
    return '(' + str(self.x) + ',' + str(self.y) + ')'
  
  def __add__(self, other):
    new_x = self.x + other.x
    new_y = self.y + other.y
    total = Vector(new_x, new_y)
    return total

  def __sub__(self, other):
    new_x = self.x - other.x
    new_y = self.y - other.y
    total = Vector(new_x, new_y)
    return total

  def __mul__(self, other):
    return (self.x * other.x) + (self.y * other.y)
  
  def __abs__(self):
    return math.sqrt((self.x**2) + (self.y**2))

  def __eq__(self, other):
    return self.x == other.x and self.y == other.y

class D3Vector(Vector):
  def __init__(self, x, y, z):
    Vector.__init__(self, x, y)
    self.z = z
  
  def __add__(self, other):
    return (self.x + other.x, self.y + other.y, self.z + other.z)
  
  def __sub__(self, other):
    return (self.x - other.x, self.y - other.y, self.z - other.z)
  
  def __mul__(self, other):
    return Vector.__mul__(self, other) + self.z * other.z
  
  def __abs__(self):
    return math.sqrt(self.x**2 + self.y**2 + self.z**2)

  def __eq__(self, other):
    return Vector.__eq__(self, other) and self.z == other.z

  def scalarMult(self, num):
    return D3Vector((self.x * num), (self.y * num), (self.z * num))


v = Vector(21, 56)
v1 = Vector(13, 24)
v2 = Vector(21, 56)
v3 = D3Vector(3, 6, 9)
v4 = D3Vector(7, 4, 8)
v5 = D3Vector(7, 4, 8)
buran write Dec-20-2020, 04:13 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
3D vector class with inheritance from 2D vector class - by buss0140 - Dec-20-2020, 02:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 536 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 305 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 335 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 439 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 616 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 780 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 833 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 544 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,880 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 684 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020