def drawSample(self, index, color): """Draw the points that were sampled for the fourier transform.""" t = Trajectory.createFromTuples(self.graphs[index]) l = t.sampleSegments(self.sample_number, include=self.include) for e in l: p = Point(*e, radius=5, conversion=False) p.show(self.context)
def getPoints(self): """Return the points that correspond to the extremities of the rectangle.""" xmin, ymin, xmax, ymax = self.getCorners() p1 = Point(xmin, ymin) p2 = Point(xmax, ymin) p3 = Point(xmax, ymax) p4 = Point(xmin, ymax) return [p1, p2, p3, p4]
def getPoints(self): """Return the points of the case.""" xmin, ymin, xmax, ymax = self.getCorners() p1 = Point(xmin, ymin) p2 = Point(xmax, ymin) p3 = Point(xmax, ymax) p4 = Point(xmin, ymax) return [p1, p2, p3, p4]
def getForm(self): """Return the form corresponding to the area of the painting.""" xmin, ymin, xmax, ymax = self.getCorners() ps = [ Point(xmin, ymin), Point(xmax, ymin), Point(xmax, ymax), Point(xmin, ymax) ] return Form(ps)
def events(self): """Deal with the events.""" keys=self.context.press() p=Point(*self.context.point()) v=Vector(*(p-Point(*self.spaceships[0].position)))/10 self.spaceships[0].velocity=v self.context.draw.plane.position=copy.deepcopy(self.spaceships[0].position.components) missile=self.spaceships[0].shoot(self.context) if missile is not None: self.missiles.append(missile)
def show(self, surface, position): """Show the pixel on screen.""" x, y = position sx, sy = self.size p1 = Point(x, y) p2 = Point(x + sx, y) p3 = Point(x + sx, y + sy) p4 = Point(x, y + sy) points = [p1, p2, p3, p4] form = Form(points, fill=True, area_color=self.color, point_show=False) form.show(surface)
def selectUp(self, button, position): if button == 3: focus = self.focusing(Point(*position)) if focus is None: focus = Point(*position) self.points.append(Point(*position, radius=5, conversion=False)) if self.focus: if self.focus != focus: # We don't want to create segments whose extremities are the same points self.group.append(Segment(self.focus, focus)) self.context.console("new segment:", self.group[-1]) self.dragging = False self.buffer = []
def shoot(self,context): """Return a missile.""" #logging.warning("This function is only a test and should not be included in the body class but in a child class instead.""") keys=context.press() point=Point(*context.point()) if keys[K_SPACE]: center=Point(*self.position) direction=Vector.createFromTwoPoints(center,point) direction.norm=2 origin=Point.origin() form=Segment(origin,direction(origin)) position=copy.deepcopy(self.position+direction(origin)) velocity=direction+copy.deepcopy(self.velocity) return Missile(form,position,velocity)
def focusing(self, cursor): focus = None for point in self.points: if Point.distance(point, cursor) < self.focus_radius: focus = point break return focus
def rotate(self, angle=math.pi, center=Point.origin()): """Rotate the point using an angle and the point of rotation.""" self.abstract.rotate(angle, center) point = self.abstract point.rotate(angle, center) vector = Vector.createFromPoint(point) self.motion.setPosition(vector)
def getForm(self, fill=None, area_color=None, side_color=None): """Return the abstract form associated with the case.""" if not fill: fill = self.fill if not area_color: area_color = self.color if not side_color: side_color = mycolors.WHITE xmin, ymin, xmax, ymax = self.getCorners() p1 = Point(xmin, ymin) p2 = Point(xmax, ymin) p3 = Point(xmax, ymax) p4 = Point(xmin, ymax) points = [p1, p2, p3, p4] return Form(points, fill=fill, side_color=side_color, area_color=area_color, point_show=False)
def update(self, surface): """Update the emiter using the surface.""" p = surface.point() if p: self.point = Point(p) vx, vy = self.view self.rays = [Ray(self.point, a) for a in np.linspace(vx, vy, self.n)]
def getAbsolute(self): """Return a copy of the form in absolute coordonnates.""" p = Point(*tuple(copy.deepcopy(self.position))) f = copy.deepcopy(self.form) f.center = p f.rotate(self.moment.position[0]) return f
def control(self): """Control the body.""" keys=self.context.press() if keys[K_SPACE]: shooting=True else: shooting=False cursor=Point(*self.context.point()) spawn=keys[K_r] return (cursor,shooting,spawn)
def __init__(self): #super().__init__() #self.l=Line(Point.null(),math.pi/2+1e-1) self.s = Segment.createFromTuples((-1, 0), (-1, 1), color=mycolors.GREEN, width=2) print(self.s.angle == math.pi / 2) print(self.s.line.angle, self.s.line.point) self.p = Point.null(color=mycolors.RED, fill=True) self.projection = None
def events(self): """Deal with the user input.""" cursor = copy.deepcopy(Point(*self.context.point())) click = self.context.click() for event in pygame.event.get(): if event.type == QUIT: self.context.open = False if event.type == KEYDOWN: if event.key == K_ESCAPE: self.context.open = False if event.type == MOUSEBUTTONDOWN: if event.button == 1: self.focus_index = None for body in self.bodies: if cursor in body.absolute: self.focus_index = self.bodies.index(body) if self.focus_index: self.bodies[self.focus_index].position = Vector( *cursor) if event.button == 3: c = copy.deepcopy(cursor) if self.focus_index == None: self.focus_index = len(self.bodies) f = Form([c]) self.bodies.append(Body.createFromForm(f)) else: fa = copy.deepcopy( self.bodies[self.focus_index].absolute) fa.points.append(c) self.bodies[self.focus_index].absolute = fa if event.type == MOUSEMOTION: if self.focus and click: self.focus.position = Vector(*cursor) keys = pygame.key.get_pressed() if keys[K_DOWN]: self.context.draw.plane.position[1] -= 1 if keys[K_UP]: self.context.draw.plane.position[1] += 1 if keys[K_LEFT]: self.context.draw.plane.position[0] -= 1 if keys[K_RIGHT]: self.context.draw.plane.position[0] += 1 if keys[K_LSHIFT]: self.context.draw.plane.zoom([0.9, 0.9]) if keys[K_RSHIFT]: self.context.draw.plane.zoom([1.1, 1.1])
def reactMouseMotion(self, position): if self.dragging and self.focus: position = self.context.getFromScreen(position) cursor = Point(*position, radius=5, conversion=False) focus = self.focusing(cursor) if self.button == 1: if focus: cursor = focus self.focus.set(cursor) elif self.button == 3: if focus and focus != self.focus: cursor = focus s = Segment(cursor, self.focus, width=2) self.buffer = [cursor, s]
def collide(self, object1, object2): """Deal with the collisions of two objects 'object1' and 'object2'.""" #I've got no clue how to do such a thing #I just know that i need the motions of the forms, the coordonnates of its points and their masses. ap1 = object1.points bp1 = [ Point.createFromVector(p1.getNextPosition(self.time)) for p1 in ap1 ] ls1 = [Segment(a1.abstract, b1) for (a1, b1) in zip(ap1, bp1)] ap2 = object2.points bp2 = [ Point.createFromVector(p2.getNextPosition(self.time)) for p2 in ap2 ] ls2 = [Segment(a2.abstract, b2) for (a2, b2) in zip(ap2, bp2)] points = [] for s1 in ls1: for s2 in ls2: print(s1, s2) point = s1.crossSegment(s2) if point: print(point) points.append(point) return points
def updateValue( self, position, ): """Update the value.""" l = self.line p = l.projectPoint(Point(*position)) s = Segment(self.p1, p) if abs(s.angle - self.angle) <= self.error: length = s.length else: length = -s.length self.relative_value = length / self.length self.relative_value = max(self.relative_value, 0) self.relative_value = min(self.relative_value, 1)
def getFormToScreen(self, plane_form, window): """Create a new form according screen coordonnates using a form and the window.""" #This function deals with an issue which is that the forms that are #defined uses screen coordonnates instead of the plane's one. #This problem has been fixed in more recent versions by changing the #nature of the windows given to the show functions of the forms by #defining a new type of window which is called 'Surface' for now... points = [ Point(self.getToScreen(point, window), radius=1) for point in plane_form ] for point in points: point.truncate() screen_form = deepcopy(plane_form) screen_form.points = points return screen_form
def random(cls, n=10): """Create a random graph. By default the points are placed on the unit circle.""" points = [] d = 2 * math.pi r = 1 for i in range(n): a = d * i / n x = r * math.cos(a) y = r * math.sin(a) points.append(Point(x, y)) connections = [] for i in range(n): for j in range(i + 1, n): if random.randint(0, 1) == 0: connections.append((i, j)) return cls(points, connections)
def __init__(self, n=5, radius=10, rotation=0.001, posts_radius=2, post_radius=3, **kwargs): """Create a spider base using the number of posts.""" self.posts_radius = posts_radius self.post_radius = post_radius self.rotation = rotation points = [] for i in range(n): angle = 2 * math.pi * i / n x = radius * math.cos(angle) y = radius * math.sin(angle) points.append(Point(x, y)) super().__init__(points, **kwargs)
def show(self, context, point=Point(0, 0), angle=0): """Show the moment.""" if len(self) >= 1: mp = self.position v = Vector.createFromPolar(mp.norm, angle) v.color = mp.color v.show(context, point) if len(self) >= 2: angle += math.pi / 2 mv = self.velocity v = Vector.createFromPolar(mv.norm, angle) v.color = mv.color v.show(context, point) if len(self) >= 3: angle += math.pi / 2 ma = self.acceleration a = Vector.createFromPolar(ma.norm, angle) a.color = ma.color a.show(context, point)
# This function should be able to determine which proportion of the object is contained in the force # field in order to apply some of the force pass def exert(self, body): """Exert the force of the force field to the object.""" pass down = Vector([0, -1]) gravity = Force(0, -9.81, color=mycolors.RED) if __name__ == "__main__": zero = Vector([0, 0]) propulsion = Force(0, 0) o = Point.origin() random_force = Force.random() # print(random_force) random_force += gravity # print(random_force) result = Force.sum([gravity, propulsion, random_force]) # print("Force.sum:",result) x, y = result # print(x,y) #Unpacking is compatible for vectors f = gravity print(result)
def getPoint(self): """Return the points associated with the particle.""" return Point(*self.position)
from myabstract import Form, Segment, Line, Vector, Point, HalfLine from mysurface import Surface surface = Surface(name="Test") #line=Line.random() #segment=Segment.random() #print(line|segment) import mycolors import math ps = [Point(-1, -1), Point(1, -1), Point(1, 1), Point(-1, 1)] f = Form(ps) while surface.open: surface.check() surface.control() surface.clear() surface.show() position = tuple(surface.point()) p = Point(*position) h = HalfLine(p, 0) ps = f.crossHalfLine(h) print(ps) f.color = mycolors.WHITE if p in f: f.color = mycolors.RED
#Show interpolations being made while moving the points. #Impors from mycontext import Context from myabstract import Point from myinterpolation import PolynomialInterpolation context = Context() l = 10 points = [Point(2 * x, random.randint(-5, 5)) for x in range(l)] n = 0 ncp = 50 #number construction points while context.open: context.check() context.control() context.clear() context.show() Point.turnPoints([1 / 1000 for i in range(l)], points) p = PolynomialInterpolation(points) p.show(context) n = (n + 1) % (ncp + 1) p1 = b(n / ncp) p2 = t(n / ncp) #l1=Line.createFromTwoPoints(p1,p2) p1.show(context, color=mycolors.YELLOW, radius=0.1, fill=True) p2.show(context, color=mycolors.YELLOW, radius=0.1, fill=True)
def delPosition(self): """Set the position of the material form to the origin.""" self.setCenter(Point.origin())
def delCenter(self): """Set the center to the origin.""" self.setCenter(Point.origin())
def rotate(self, angle=math.pi, center=Point.origin()): """Rotate the form by rotating its points.""" for point in self.points: point.rotate(angle, center)