My end goal right now is to take points that are read from a text file, and turn them into 3d objects. They do not need to be visualized, but they need to be stored in objects instead of just a string containing the x, y, and z values. The file gives me six numbers, two of each x, y, and z, and I was wondering how I would go about creating a point class/object that will take all three variables and then a line object/class that will take two of the points.
Just define a Point and a Line class:
class Point(object):
def __init__(self, x=0, y=0 ,z=0):
self.x = x
self.y = y
self.z = z
class Line(object):
def __init__(self, point1=None, point2=None):
self.point1 = point1 or Point() # (0,0,0) by default
self.point2 = point2 or Point() # (0,0,0) by default
To create points and lines objects:
>>> p1 = Point(1, 2, 3)
>>> p2 = Point(4, 5, 6)
>>> line = Line(p1, p2)
Once you have got the data from the file (for this Regular Expressions are applicable), you will want to input that into a class which is defined as to store the two points (which can be objects themselves) e.g.
class Point(tuple):
@property
def x:
return self[0]
@property
def y:
return self[1]
@property
def z:
return self[2]
class Vector(object):
def __init__(self, x1, y1, z1, x2, y2, z2):
self._a = Point(x1, y1, z1)
self._b = Point(x2, y2, z2)
@property
def a(self):
return self._a
@property
def b(self):
return self._b
# Other methods here e.g.
@property
def i(self):
return self.b.x - self.a.x
@property
def j(self):
return self.b.y - self.a.y
@property
def k(self):
return self.b.z - self.a.z
def length(self):
return ( self.i**2 + self.j**2 + self.k**2 ) ** (1/2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With