def __call__(self, t): """Return the point.""" points = self.points if len(points) == 0: return None elif len(points) == 1: return points[0] elif len(points) == 2: segment = Segment(points[0], points[1]) point = segment.center() return point elif len(points) == 3: p1, p2, p3 = points s1 = Segment(p1, p2) s2 = Segment(p2, p3) ps1 = s1(t) ps2 = s2(t) s = Segment(ps1, ps2) return s(t) else: while len(points) > 3: segments = [ Segment(points[i], points[i + 1]) for i in range(len(points) - 1) ] points = [segment(t) for segment in segments] b = BezierCurve(points) return b(t)
def getInstant(self, steps): """Return the minimum of the instant of the point to the begining of the segments.""" st1 = steps[0] st2 = steps[1] p = st1.crossSegment(st2) t1 = Segment(st1.p1, p).length / st1.length t2 = Segment(st1.p2, p).length / st2.length return min(t1, t2)
def show(self, surface, points): """Show the angle between the points.""" p1, p2 = points #Need to be able to print arc of circles using pygame 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)
def segments(self): """Return 2 segments instead of 1 to avoid crossing the circle of the cursor.""" rv = self.relative_value r = self.radius l = self.length x1 = max(rv - r / l, 0) x2 = min(rv + r / l, 1) p1 = self(x1) p2 = self(x2) s1 = Segment(self.p1, p1, color=self.color) s2 = Segment(self.p2, p2, color=self.color) return (s1, s2)
def show(self, surface, point): """Show the ray on the surface and stop at the given point.""" if point: object = Segment(self.point, point) else: object = HalfLine(self.point, self.angle) object.show(surface)
def show(self,surface): """Show the branch to the surface.""" p=self.point() for c in self.children(): pc=c.point() s=Segment(p,pc) s.show(surface)
def show(self, context): """Show all the objects on screen.""" for object in self.objects: object.center.showMotion(context) object.show(context) l = len(self.objects) for i in range(l): for j in range(i + 1, l): points = self.objects[i].abstract | self.objects[j].abstract if points != []: self.objects[i].velocity *= 0 print(points) l1 = Segment(*points[:2]) l1.color = mycolors.GREEN l1.show(context) p = l1.center p.show(context, mode="cross", color=mycolors.RED) c1 = self.objects[i].center.abstract v = Vector.createFromTwoPoints(c1, p) v.color = mycolors.GREEN v.show(context, c1) v.norm = 1 v.color = mycolors.BLUE v.show(context, p) v.showText(context, p, 'up') v.rotate(math.pi / 2) v.show(context, p) v.showText(context, p, 'uo')
def cross(self, e1, e2): """Determine the point of collision.""" points = e1.cross(e2) p1, p2 = points s = Segment(p1, p2) l = Line(s.middle, s.angle+math.pi/2) pts1 = l.projectPoints(e1.points) pts2 = l.projectPoints(e2.points) pass
def getSegments(self): """Return the segments determined by the points and the network established.""" segments = [] l = len(self.points) for j in range(l): for i in range(j): if self.network[i][j]: segment = Segment(self.points[i], self.points[j]) segments.append(segment) return segments
def show(self, surface, p=50): """Show the arrow on the screen.""" points = [self(i / p) for i in range(p)] #p=p+1 ? segments = [Segment(points[i], points[i + 1]) for i in range(p - 1)] vectors = [ Vector(points[i], points[i + 1]) for i in range(0, p - 1, 5) ] self.showPoints(surface, points) self.showSegments(surface, segments) self.showVectors(surface, vectors)
def getConstruction(self, t): """Return the construction segments of the form.""" points = self.points construction = [] while len(points) >= 2: segments = [ Segment(points[i], points[i + 1]) for i in range(len(points) - 1) ] points = [segment(t) for segment in segments] construction.append(segments) return construction
def showBar(self, context, t, l): """Show the bar.""" v = self.getVector() v *= l p = self(t) v.rotate(math.pi / 2) p1 = v(p) v.rotate(math.pi) p2 = v(p) s = Segment(p1, p2) s.color = self.color s.show(context)
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 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 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 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 __call__(self, t): """Evaluate the trajectory by t between 0 and 1.""" if len(self.points) <= 1: raise Exception( "It is impossible to interpolate without at least 2 points.") length = self.length ns = len(self.segments) tsl = 0 for i in range(ns): s = Segment(self.points[i], self.points[i + 1]) if t * length - tsl <= s.length: st = (t * length - tsl) / s.length return tuple(s(st).components) tsl += s.length return tuple(s(1)) #Its a fix that isn't always true
def getRegions(self): """Decompose the complex forms in multiple normal forms which cannot be cut by a segment.""" vectors = [ Vector.createFromSegment(segment) for segment in self.segments() ] for j in range(l): for i in range(j): if self.network[i][j]: segment = Segment(self.points[i], self.points[j]) vector = x segments.append(segment) for vector in vectors: pass return segments
def fastSample(self, n, include=True): """Sample n points of the trajectory at equal distance but fast. It is also possible to include the last one if wanted.""" length = self.length ns = len(self.segments) k = 0 l = [] tsl = 0 for i in range(ns): s = Segment(self.points[i], self.points[i + 1]) if t * length - tsl <= s.length: st = (t * length - tsl) / s.length l.append(tuple(s(st).components)) k += 1 t = k / n tsl += s.length if include: l.append(self.points[-1]) return l
def getSegments(self): """Return the segments that connect the points with their connections.""" return [Segment(self.points[c[0]], self.points[c[1]],\ color=self.segment_color) for c in self.connections]
def getSegment(self, t=1, **kwargs): """Return the direction made of the actual point and the future one.""" p1 = self.abstract p2 = self.getNext(t).abstract return Segment(p1, p2, **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()
def getSegments(self): """Return the segments of the trajectory of the curve.""" return [ Segment(self.points[i], self.points[i + 1]) for i in range(len(self.points) - 1) ]
def show(self, surface, p=50): """Show the bezier curve on the surface.""" points = [self(i / p) for i in range(p + 1)] segments = [Segment(points[i], points[i + 1]) for i in range(p)] self.showPoints(surface, points) self.showSegments(surface, segments)