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 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 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 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 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 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 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 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 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 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 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)
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 showName(self, context): """Show the name of the astre.""" c = Point(*(udd * self.position)) c.showText(context, self.name, size=10, color=self.color)
from math import sqrt, atan, pi, cos, sin, pi from cmath import polar from mycolors import * from myabstract import Point, Form, Vector import random import time if __name__ == "__main__": real_window = Window(size=[1440, 900], fullscreen=True) draw = Draw(window=real_window) window = Surface( 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:
def getAbstract(self): """Return the abstract point that correspond to the point.""" return Point(self.motion.position.components)
def createFromTuples(tuples, **kwargs): """Create a trajectory using tuples and optional arguments.""" pts = [Point(*t) for t in tuples] return Trajectory(pts, **kwargs)
from myabstract import Segment, Point, Vector from mycontext import Surface import mycolors p1 = Point(-1, -1) p2 = Point(1, 1) p3 = Point(0, 1) s1 = Segment(p1, p2, width=3) s2 = Segment(p2, p3, width=3) e = 10e-10 surface = Surface(name="Segment Demonstration") while surface.open: surface.check() surface.control() surface.clear() surface.show() p = Point(list(surface.point())) #if p in surface: s1.p2 = p if s1.crossSegment(s2): s1.color = mycolors.RED s2.color = mycolors.RED else: s1.color = mycolors.WHITE s2.color = mycolors.WHITE l1 = s1.getLine()
from myabstract import Form, Segment, Line, Vector, Point from mysurface import Surface surface = Surface(name="Test") #line=Line.random() #segment=Segment.random() #print(line|segment) import mycolors import math vector = Vector(1, 0) point = Point(0, 0) while surface.open: surface.check() surface.control() surface.clear() surface.show() vector.rotate(0.1) a = vector.angle wl = mycolors.bijection(a, [-math.pi, math.pi], [380, 780]) c = mycolors.setFromWavelength(wl) vector.show(surface, point, color=c) surface.console("angle: ", str(a), nmax=20) #surface.print('something',(10,10),size=100,conversion=False) surface.flip()
def __init__(self, position=[0, 0], n=50, view=[0, 2 * math.pi]): """Create an emiter object.""" x, y = position self.point = Point(x, y) self.n = n self.view = view
def update(self, surface): """Update the ray caster.""" self.emiter.update(surface) if __name__ == "__main__": from mysurface import Surface from myzone import Zone surface = Surface(name="RayCasting", plane=Zone()) #forms=[Form.random([-10,-10,10,10],number=5,side_color=mycolors.RED) for i in range(10)] forms = [ Form.random([10 * (i - 5), -5, 10 * (i - 4), 5], number=5, side_color=mycolors.RED) for i in range(10) ] #forms=[Segment.random([10*(i-5),-5,10*(i-4),5],number=5,side_color=mycolors.RED) for i in range(10)] emiter = Emiter() caster = RayCaster(emiter, forms) origin = Point(0, 0) while surface.open: surface.check() surface.control() surface.clear() surface.show() for form in forms: form.rotate(0.1, origin) caster.update(surface) caster.show(surface) surface.flip()