def _calc_triangle(self, level, top, left, right): if level == 0: triangle = sf.VertexArray(sf.PrimitiveType.LINES_STRIP) triangle.append(sf.Vertex(top, sf.Color.BLACK)) triangle.append(sf.Vertex(left, sf.Color.BLACK)) triangle.append(sf.Vertex(right, sf.Color.BLACK)) triangle.append(sf.Vertex(top, sf.Color.BLACK)) self._triangles.append(triangle) else: left_mid = self._mid_point(top, left) right_mid = self._mid_point(top, right) top_mid = self._mid_point(left, right) self._calc_triangle(level - 1, top, left_mid, right_mid) self._calc_triangle(level - 1, left_mid, left, top_mid) self._calc_triangle(level - 1, right_mid, top_mid, right)
def draw(self, target, states): target.draw(self.sprite) if debug_mode: if self.usable: lines = sf.VertexArray( len(self.x_usable) - 1, sf.PrimitiveType.POINTS) #lines = [] for x, y in zip(self.x_usable, self.y_usable): lines.append(sf.Vertex([x, y], sf.Color.RED)) window.draw(lines)
def _calculate_bezier_points(self): points = self._basic_bezier_points if len(points) > 1: n = len(points) - 1 k = self._spline_degree() m = n + k + 1 _t = 1.0 / (m - k * 2) t = k * [0] + [t_ * _t for t_ in xrange(m - (k * 2) + 1)] + [1] * k bezier_curve = pycurve.Bspline(points, t, k) step = 1.0 / self._step_number for i in xrange(self._step_number): x, y = bezier_curve(i * step) self._draw_bezier_points.append(sf.Vertex((int(x), int(y)), sf.Color.BLUE))
def update(self, append=False, color=False, offset=False): if append: self._vertexarray.append( sf.Vertex(position=self._points[-1], color=self._color)) if color: for i in range(self.point_count): self._vertexarray[i].color = self._color if offset: for vertex in self._vertexarray: vertex.position += offset if not color and not append and not offset: self._vertexarray.resize(self.point_count) for i in range(self.point_count): self._vertexarray[i].position = self._points[i] self._vertexarray[i].color = self._color
def on_load(self): # create the points self.points.primitive_type = sf.PrimitiveType.POINTS for i in range(40000): x = randint(0, 32767) % 800 y = randint(0, 32767) % 600 r = randint(0, 32767) % 255 g = randint(0, 32767) % 255 b = randint(0, 32767) % 255 self.points.append(sf.Vertex(sf.Vector2(x, y), sf.Color(r, g, b))) try: # load the shader self.shader = sf.Shader.from_file("data/storm.vert", "data/blink.frag") except: pass return True
def __init__(self, n): super(KochSnowflake, self).__init__() self._koch = sf.VertexArray(sf.PrimitiveType.LINES_STRIP) angles = [math.radians(60 * x) for x in range(6)] sines = [math.sin(x) for x in angles] cosin = [math.cos(x) for x in angles] def L(angle, *_): return (angle + 1) % 6 def R(angle, *_): return (angle + 4) % 6 def F(angle, coords, jump): coords.append((coords[-1][0] + jump * cosin[angle], coords[-1][1] + jump * sines[angle])) return angle decode = dict(L=L, R=R, F=F) def grow(steps, length=500, startPos=(80, 480)): pathcodes = "FRFRF" for i in xrange(steps): pathcodes = pathcodes.replace("F", "FLFRFLF") jump = float(length) / (3**steps) coords = [startPos] angle = 0 for move in pathcodes: angle = decode[move](angle, coords, jump) return coords showflake = grow(n) for coord in showflake: self._koch.append( sf.Vertex((int(coord[0]), int(coord[1])), sf.Color.BLACK))
def _calculate_basic_points_connections(self): if self._connect: for basic_point in self._basic_bezier_points: self._basic_point_connections.append(sf.Vertex(basic_point, sf.Color.GREEN))
def __init__(self, a, b, color): super(Line, self).__init__() self._rep = sf.VertexArray(sf.PrimitiveType.LINES_STRIP) self._rep.append(sf.Vertex(a, color)) self._rep.append(sf.Vertex(b, color))