def __init__(self, position, **kwargs): """Create a brick using its position.""" points = self.getCornersPointsFromPosition(position) form = Form(points) form.makeSparse() form = MaterialForm.createFromForm(form) # material_points=[MaterialPoint.createFromPoint(point) for point in points] material_points = form.getMaterialPoints() super().__init__(material_points, **kwargs)
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 random(cls, nf=3, np=5, nfp=3, **kwargs): """Create a random complex form.""" points = Form.random(n=np).points l = len(points) forms = [] for j in range(nf): f = Form([], area_color=mycolors.random(), fill=True) d = [] for i in range(nfp): d.append(random.randint(0, l - 1)) forms.append((d, f)) return cls(points, forms, **kwargs)
def getCompleteForm(self, point_mode=None, point_radius=None, point_width=None, side_width=None, fill=None, area_color=None, point_color=None, side_color=None): """Return the object under a Form type by conversion.""" if not point_mode: point_mode = self.point_mode if not point_radius: point_radius = self.point_radius if not point_width: point_width = self.point_width if not side_width: side_width = self.side_width if not fill: fill = self.fill if not area_color: area_color = self.area_color if not point_color: point_color = self.point_color if not side_color: side_color = self.side_color points = [point.abstract for point in self.points] return Form(points, fill=fill, point_mode=point_mode, point_radius=point_radius, point_width=point_width, side_width=side_width, point_color=point_color, side_color=side_color, area_color=area_color)
def random(corners=[-1, -1, 1, 1], n=5): """Create a random body.""" form = Form.random(corners, n) motion = Motion.random(n=2, d=3) moment = Motion.random(n=2, d=3) print(motion.acceleration) return Body(form, motion, moment)
def createRandomBody(): form=5*Form.random(n=5) form.side_color=mycolors.RED form.area_color=mycolors.BLACK form.fill=True motion=Motion(10*Vector.random(),Vector.random(),Vector.null()) moment=Motion(Vector([1]),Vector([0.1])) return Body(form,motion,moment)
def cross(cls, r1, r2, **kwargs): """Return Rectangle of the intersection of the rectangles r1 and r2.""" if isinstance(r1, cls) and isinstance(r2, cls): r = Rect.cross(r1, r2) if r is not None: return cls(*r, **kwargs) elif isinstance(r1, Form) and isinstance(r2, Form): return Form.cross(r1, r2)
def __init__(self,position,size=5): """Create an asteroid.""" form=size*Form.random(n=5) form.side_color=mycolors.RED form.area_color=mycolors.DARKGREY form.fill=True motion=Motion(Vector(*position),Vector.random(),Vector.null()) moment=Motion(Vector([0]),Vector([random.uniform(-1,1)])) super().__init__(form,motion,moment)
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 makeAnatomy(self, **kwargs): """Return the anatomy of a boid.""" if not "area_color" in kwargs: kwargs["area_color"] = mycolors.RED if not "fill" in kwargs: kwargs["fill"] = True anatomy = Form.createFromTuples([(1, 0), (-1, -1), (-0.5, 0), (-1, 1)], **kwargs) anatomy.recenter() return anatomy
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 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 reactKeyDown(self, key): super().reactKeyDown(key) if self.focus: if key == pg.K_BACKSPACE: self.remove() elif key == pg.K_SPACE: if self.focus in self.selection: self.selection.remove(self.focus) else: self.selection.append(self.focus) elif key == pg.K_a: f = Form(self.selection) self.group.append(f) self.selection = [] elif key == pg.K_s: data = pickle.dumps([self.group, self.points]) pickle.dump(data, open(self.path, "wb")) elif key == pg.K_l: data = pickle.load(open(self.path, "rb")) group, points = pickle.loads(data) self.points.extend(points) self.group.extend(group)
def getAbstract(self): """Return the object under a Form by conversion.""" return Form([p.getAbstract() for p in self.points])
def random(corners=[-1, -1, 1, 1], n=5): """Create a random material form.""" form = Form.random(corners, n=n) return MaterialForm.createFromForm(form)
acceleration = property(getAcceleration, setAcceleration, delAcceleration, "Representation of the acceleration of the form.") #abstract_center=property(getAbstractCenter,setAbstractCenter,delAbstractCenter,"Representation of the abstract center of the material form.") #abstract_points=property(getAbstractPoints,setAbstractPoints,delAbstractPoints,"Representation of the abstract points of the material form.") steps = property(getSteps, setSteps, delSteps, "Representation of the steps of the material form.") FallingForm = lambda: MaterialForm( [mymaterialpoint.FallingPoint() for i in range(5)]) if __name__ == "__main__": from mycontext import Surface surface = Surface() c1 = [-10, -10, 10, 10] f1 = Form.random(c1, n=5) f1 = MaterialForm.createFromForm(f1) f1.velocity = Vector(0, 1) f1.acceleration = Vector(0, -0.01) print(f1) c2 = [-10, -10, 10, 10] f2 = Form.random(c2, n=5) f2 = MaterialForm.createFromForm(f2) #print(form[0].forces) #print(form.getMass()) origin = Point.origin() while surface.open: surface.check() surface.clear()
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: window.check() window.clear() window.draw.control() v1.rotate(0.1) v2 = v1 % (pi / 2) v2.color = GREEN v2.rotate(0.1)
segments=[Segment(p.getPosition(),p.getNextPosition) for p in self.points] return segments def affectFriction(self,frixion=None): """Reduce th velocity of the object according to its frixion.""" f=self.factor for entity in self.entities: entity.velocity=[f*entity.velocity[0],f*entity.velocity[1]] if __name__=="__main__": from mysurface import Surface surface=Surface() c1=[-10,-10,10,10] f1=Form.random(c1) f1=MaterialForm.createFromForm(f1,[Force(0.001,0),Force(0,0.001)]) c2=[-10,-10,10,10] f2=Form.random(c2) f2=MaterialForm.createFromForm(f2,[Force(0.001,0),Force(0,0.001)]) #print(form[0].forces) #print(form.getMass()) origin=Point(0,0) while surface.open: surface.check() surface.clear() surface.control() surface.show() f1.update(t=0.1)
points.append(p) return points 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)
from myabstract import Form, Vector from mycontext import Context context = Context() f = Form.random() v = Vector.random() while context: context.check() context.control() context.clear() v *= 1.01 #f.move(v) f.show(context) context.flip()
def getForm(self): """Return the form of the perlin asteroid.""" return Form.createFromTuples(self.points, color=self.color)
def __init__(self,position): """Create body using form and optional name.""" form=Form([Point(x,y) for (x,y) in [(0,1),(-1,-1),(1,-1)]]) motion=Motion(position,Vector.null(d=2),Vector.null(d=2)) moment=Motion.null(d=1,n=2) super().__init__(form,motion,moment)
def startForm(self, screen_position, window): """Start a new form to create using screen_position and window.""" position = self.getFromScreen(screen_position, window) point = Point(position) self.selection = len(self.forms) self.forms.append(Form([point]))
from mycontext import Surface from mybody import Body from myabstract import Form,Point,Vector from mymotion import Motion import mycolors surface=Surface() dt=1 form=Form([Point(0,1),Point(0,0),Point(1,0),Point(1,1)],area_color=mycolors.BLUE,fill=True) #form=Circle(copy.deepcopy(Point.origin()),radius=1,fill=True,color=mycolors.BLUE) body=Body(form) missile=None def createRandomBody(): form=5*Form.random(n=5) form.side_color=mycolors.RED form.area_color=mycolors.BLACK form.fill=True motion=Motion(10*Vector.random(),Vector.random(),Vector.null()) moment=Motion(Vector([1]),Vector([0.1])) return Body(form,motion,moment) n=10 bodies=[createRandomBody() for i in range(n)]
def getForm(self): """Return the form of the perlin asteroid.""" #points=[Point(*p) for p in self.getPoints()] return Form.createFromTuples(self.getPoints(), conversion=self.conversion, radius=self.radius, side_color=self.color)
v1 = Vector.createFromTwoPoints(p1, p2) v1 = ~v1 v2 = v1 % self p3 = v2(p2) s1 = Segment(p1, p2) s2 = Segment(p2, p3) s1.show(surface) s2.show(surface) #I need to show arc of circles which im too lazy to do now if __name__ == "__main__": from mysurface import Surface surface = Surface() corners = [-10, -10, 10, 10] f = Form.random(corners, number=5, side_width=3, side_color=mycolors.RED) p = f.points while surface.open: surface.check() surface.control() surface.clear() surface.show() f.show(surface) f.rotate(0.001) l = len(f) for i in range(l): h = (i + l - 1) % l j = (i + 1) % l angle = Angle.createFromThreePoints([p[h], p[i], p[j]]) angle.show(surface, [p[h], p[i]]) c = Circle.createFromPointAndRadius(p[i], 1, fill=Falses)
def affectOneVelocity(self, v1, v2, m1, m2): return (m1 - m2) / (m1 + m2) * v1 + (2 * m2) / (m1 + m2) * v2 def rotate2(self, velocity, angle): vx, vy = velocity nvx = vx * cos(angle) - vy * sin(angle) nvy = vx * sin(angle) + vy * cos(angle) return [nvx, nvy] if __name__ == "__main__": from mysurface import Surface surface = Surface(name="Material Form Handler") ps1 = [Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)] f1 = Form(ps1) f1 = MaterialForm.createFromForm(f1) f1.velocity = Vector(1, 1) ps2 = [Point(0, 0), Point(0, 2), Point(2, 2), Point(2, 0)] f2 = Form(ps2) f2 = MaterialForm.createFromForm(f2) forms = [f1, f2] handler = MaterialFormHandler(forms) while surface.open: surface.check() surface.control() surface.clear() surface.show() handler.update(1) handler.rotate(0.1)
def __init__(self, *args, **kwargs): self.form = Form.random(n=10) self.focus = 0 super().__init__(*args, **kwargs)
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
from myabstract import Form,Point from mybody import Body from mycontext import Context import copy import mycolors ps=[Point(10,2),Point(12,5),Point(15,-2)] f=Form(ps) b=Body.createFromAbsolute(f) context=Context() while context.open: context.check() context.control() context.clear() context.show() p=Point(*context.point()) nb=copy.deepcopy(b) nba=copy.deepcopy(nb.absolute) nba.points.append(p) for point in nba.points: point.show(context,color=mycolors.BLUE) nb.absolute=nba nb.show(context) nb.absolute.center.show(context,color=mycolors.RED) context.flip()