示例#1
0
    def LoadFile(self, FileName):
        fp = open(str(FileName), 'r')
        self.FileName = FileName

        lines = fp.readlines()
        NumAtom = string.atoi(lines[0])
        self.NumAtom = NumAtom
        AtomStr = [] * 4
        self.AtomName = [None] * NumAtom
        self.AtomId = [None] * NumAtom
        self.AtomPos = [None] * NumAtom
        self.AtomVel = [None] * NumAtom

        x = 0.0
        y = 0.0
        z = 0.0

        for i in range(NumAtom):
            self.AtomId[i] = i + 1
            AtomStr = string.split(lines[i + 2])[0:4]
            self.AtomName[i] = AtomStr[0]
            x = string.atof(AtomStr[1])
            y = string.atof(AtomStr[2])
            z = string.atof(AtomStr[3])
            self.AtomPos[i] = Vector(x, y, z)
            self.AtomVel[i] = Vector()
示例#2
0
 def getPointsWithinCorners(self, corners):
     """Return the segment made of the poins of the line which are in the area
     delimited by the corners."""
     xmin, ymin, xmax, ymax = corners
     p1 = Point(xmin, ymin)
     p2 = Point(xmax, ymin)
     p3 = Point(xmax, ymax)
     p4 = Point(xmin, ymax)
     v1 = Vector(p1, p2)
     v2 = Vector(p2, p3)
     v3 = Vector(p3, p4)
     v4 = Vector(p4, p1)
     l1 = Line.createFromPointAndVector(p1, v1)
     l2 = Line.createFromPointAndVector(p2, v2)
     l3 = Line.createFromPointAndVector(p3, v3)
     l4 = Line.createFromPointAndVector(p4, v4)
     lines = [l1, l3]
     points = []
     for line in lines:
         cross = self.crossLine(line)
         if cross:
             points.append(cross)
     if not points:
         lines = [l2, l4]
         for line in lines:
             cross = self.crossLine(line)
             if cross:
                 points.append(cross)
     print("points: ", points)
     return points
示例#3
0
 def rotate(self,angle=pi,point=[0,0]):
     """Rotate the point using the angle and the center of rotation.
     Uses the origin for the center of rotation by default."""
     v=Vector(self.x-point[0],self.y-point[1])
     v.rotate(angle)
     new_point=v(point)
     self.x,self.y=new_point
示例#4
0
 def next(self, t=1):
     """Return the next motion using its actual one using optional time t."""
     next_acceleration = Vector([a for a in self.acceleration])
     next_velocity = Vector(
         [v + a * t for (v, a) in zip(self.velocity, self.acceleration)])
     next_position = Vector(
         [p + v * t for (p, v) in zip(self.position, self.velocity)])
     next_motion = Motion(next_position, next_velocity, next_acceleration)
     return next_motion
示例#5
0
 def previous(self, t=1):
     """Return the previous motion using its actual one using optional time t."""
     previous_acceleration = Vector([a for a in self.acceleration])
     previous_velocity = Vector(
         [v - a * t for (v, a) in zip(self.velocity, self.acceleration)])
     previous_position = Vector(
         [p - v * t for (p, v) in zip(self.position, self.velocity)])
     previous_motion = Motion(previous_position, previous_velocity,
                              previous_acceleration)
     return previous_motion
示例#6
0
 def createFromTwoPoints(self,
                         point1,
                         point2,
                         width=1,
                         color=mycolors.WHITE):
     """Create a line using two points with optional features."""
     vector = Vector(point1, point2)
     angle = vector.angle()
     line = Line(point, angle, width, color)
     return line
示例#7
0
 def rotate(self,angle,C=None):
     """Rotate the form by rotating its points from the center of rotation.
     Use center of the shape as default center of rotation.""" #Actually not working
     if not C:
         C=self.center()
     for i in range(len(self.points)):
         P=self.points[i]
         v=Vector(P.x-C.x,P.y-C.y)
         v.rotate(angle)
         self.points[i]=v(C)
示例#8
0
 def __init__(self,
              position=Vector([0., 0.]),
              velocity=Vector([0., 0.]),
              acceleration=Vector([0., 0.])):
     """Create a motion using optional position, velocity and acceleration vectors."""
     position.color = mycolors.GREEN
     velocity.color = mycolors.BLUE
     acceleration.color = mycolors.RED
     self.position = position
     self.velocity = velocity
     self.acceleration = acceleration
示例#9
0
 def getSparse(self): #as opposed to makeSparse which keeps the same form and return nothing
     """Return the form with the most sparsed points."""
     center=self.center()
     cx,cy=center[0],center[1]
     list1=[]
     for point in self.points:
         px,py=point.x,point.y
         vector=Vector(px-cx,py-cy)
         angle=vector.polar()[1]
         list1.append((angle,point))
     list1=sorted(list1,key=lambda x:x[0])
     points=[element[1] for element in list1]
     return Form(points)
示例#10
0
 def convex(self):
     """Return the bool (the form is convex)."""
     center=self.center()
     cx,cy=center[0],center[1]
     angles=[]
     l=len(self.points)
     for i in range(l-1):
         A=self.points[(i+l-1)%l]
         B=self.points[i%l]
         C=self.points[(i+1)%l]
         u=Vector(A.x-B.x,A.y-B.y)
         v=Vector(C.x-B.x,C.y-B.y)
         angle=v^u
         if angle>pi:
             return True
     return False
示例#11
0
 def update(self,t=1):
     """Update the motion of the material point."""
     force=Force.sum(self.forces)
     print("MyMaterialPoint:force:",force)
     x,y=force
     acceleration=Vector(x,y)/self.mass
     self.motion.setAcceleration(acceleration)
     self.motion.update(t)
示例#12
0
 def area(self):
     """Return the area of the form using its own points."""
     l=len(self.points)
     if l==0 or l==1:
         return 0
     elif l==3:
         a,b,c=[Vector(segment) for segment in self.sides()]
         A=1/4*sqrt(4*a.norm()**2*b.norm()**2-(a.norm()**2+b.norm()**2-c.norm()**2)**2)
         return A
     else:
         area=0
         C=self.center()
         for i in range(l):
             A=self.points[i]
             B=self.points[(i+1)%l]
             triangle=Form([A,B,C])
             area+=Form.area(triangle)
         return area
示例#13
0
 def area(self):
     """Return the area of the form using its own points."""
     l = len(self.points)
     if l < 3:  #The form has no point, is a single point or a segment, so it has no area.
         return 0
     elif l == 3:  #The form is a triangle, so we can calculate its area.
         a, b, c = [Vector(segment) for segment in self.sides()]
         A = 1 / 4 * sqrt(4 * a.norm()**2 * b.norm()**2 -
                          (a.norm()**2 + b.norm()**2 - c.norm()**2)**2)
         return A
     else:  #The form has more points than 3, so we can cut it in triangles.
         area = 0
         C = self.center()
         for i in range(l):
             A = self.points[i]
             B = self.points[(i + 1) % l]
             triangle = Form([A, B, C])
             area += Form.area(triangle)
         return area
示例#14
0
 def sum(forces):
     """Return the force that correspond to the sum of the forces."""
     resulting_force = Force(Vector())
     for force in forces:
         resulting_force += force
     return resulting_force
示例#15
0
 def random(min=-1, max=1):
     """Create a random motion using optional minimum and maximum."""
     position = Vector.random(min, max)
     velocity = Vector.random(min, max)
     acceleration = Vector.random(min, max)
     return Motion(position, velocity, acceleration)
示例#16
0
 def update(self, t=1):
     """Move entity according to its acceleration, velocity and position."""
     self.velocity = Vector(
         [v + a * t for (v, a) in zip(self.velocity, self.acceleration)])
     self.position = Vector(
         [p + v * t for (p, v) in zip(self.position, self.velocity)])
示例#17
0
 def AddAtom(self, Name, x, y, z):
     self.AtomId.append(self.NumAtom + 1)
     self.AtomName.append(Name)
     self.AtomPos.append(Vector(x, y, z))
     self.NumAtom = len(self.AtomId)
示例#18
0
 def getUnitVector(self):
     """Return the unit vector of the line."""
     vector = Vector.createFromPolarCoordonnates(1, self.angle)
     return vector
示例#19
0
 def setAcceleration(self,acceleration):
     """Set the acceleration of the material point."""
     self.motion.acceleration=Vector(acceleration)
示例#20
0
 def setVelocity(self,velocity):
     """Set the velocity of the material point."""
     self.motion.velocity=Vector(velocity)
示例#21
0
 def setPosition(self,position):
     """Set the position of the material point."""
     self.motion.position=Vector(position)
示例#22
0
 def getVector(self):
     """Return the vector that goes from p1 to p2."""
     return Vector.createFromTwoPoints(p1,p2)
示例#23
0
 def angle(self):
     """Return the angle of the segment."""
     vector=Vector.createFromSegment(self)
     return vector.angle()
示例#24
0
if __name__ == "__main__":
    window = Surface2()
    p1 = Point(15, 62)
    p2 = Point(250, 400)
    p3 = Point(800, 500)
    p4 = Point(400, 400, color=(0, 255, 0))
    points = [p1, p3, p2, p4]
    f = Form([
        Point(random.randint(-10, 10), random.randint(-10, 10))
        for i in range(10)
    ])
    #f.show(window)
    f2 = f.getSparse()
    p1.show(window)
    p2.show(window)
    v = Vector(p2[0] - p1[0], p2[1] - p1[1], color=(255, 0, 0))
    center = f2.center()
    while window.open:
        window.check()
        window.clear()
        window.draw.control()
        window.draw.show()
        #v.rotate(0.1)
        v.show(center, window)
        f2.rotate(0.1)
        time.sleep(0.1)
        f2.show(window)
        window.flip()
    #Segment(f[0],f[1],color=(255,0,0)).center().show(window)
    #print(p4 in f)
    #window.clear()
示例#25
0
        """Print the name of the force and its vector."""
        text = "Force:name:" + self.name + ",vector:" + str(self.vector)
        return text

    __repr__ = __str__

    def __iter__(self):
        """Iterate using its internal vector."""
        return iter(self.vector)

    def __next__(self):
        """Return next component depending on the components of the internal vector."""
        return next(self.vector)


down = Vector([0, -1])
gravity = Force(9.81 * down)

if __name__ == "__main__":
    zero = Vector([0, 0])
    propulsion = Force(zero)

    random_force = Force.random()
    random_force += gravity
    print(random_force)

    result = Force.sum([gravity, propulsion])
    print(result)

    x, y = result
    print(x, y)  #Unpacking is compatible for vectors
示例#26
0
 def __contains__(self, point):
     """Determine if a point belongs to the line."""
     v1 = self.vector()
     v2 = Vector(self.point, point)
     scalar = v1.scalar(v2)
     return scalar
示例#27
0
 def random(min=-1, max=1):
     """Return a random force."""
     vector = Vector.random(min, max)
     return Force(vector)
示例#28
0
        draw)  #Create not a real window but a surface to display on screen.
    #window=Window(fullscreen=True)
    p1 = Point(1, -6)
    p2 = Point(-2, 4)
    p3 = Point(8, 5)
    p4 = Point(4, 4, color=(0, 255, 0))
    points = [p1, p3, p2, p4]
    f = Form([
        Point(random.randint(-10, 10), random.randint(-10, 10))
        for i in range(10)
    ])
    #f.show(window)
    f2 = f.getSparse()
    p1.show(window)
    p2.show(window)
    v1 = Vector(p2[0] - p1[0], p2[1] - p1[1], color=(255, 0, 0))

    while window.open:
        window.check()
        window.clear()
        window.draw.control()
        v1.rotate(0.1)
        v2 = v1 % (pi / 2)
        v2.color = GREEN
        v2.rotate(0.1)
        f2.rotate(0.1)
        center = f2.center()
        center.color = BLUE
        #center.radius=0.1
        A = v1(center)
        window.draw.show()