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 __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 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 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 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 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)
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
def getAbstract(self): """Return the object under a Form by conversion.""" return Form([p.getAbstract() for p in self.points])
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)
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 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 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]))
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)