def draw(self, graph_visual): '''All the drawing behaviours for the visual node''' self.shape.draw() self.border.draw() #If the nodes has a parent we will draw a line from the parent to this node if self.node.parent is not None: par = graph_visual.get_visual(self.node.parent) line = pygame.draw.lines( self.shape.draw_surface, (0, 255, 0), True, [[ self.shape.position.x_pos + self.shape.scale[0] / 2, self.shape.position.y_pos + self.shape.scale[1] / 2 ], [ par.shape.position.x_pos + par.shape.scale[0] / 2, par.shape.position.y_pos + par.shape.scale[1] / 2 ]], 1) #Drawing of the text to the screen #Will only draw if the node was used in the algorithm and show_scores is true if (self.is_open or self.is_closed or self.is_goal or self.is_start) and self.show_scores: text = Text(Vector2(5, 5), (0, 0, 0), str(self.node.f_score), 12, self.shape.draw_surface) text.draw_on_surface(self.shape.rect) text2 = Text(Vector2(25, 20), (0, 0, 0), str(self.node.h_score), 12, self.shape.draw_surface) text2.draw_on_surface(self.shape.rect) text3 = Text(Vector2(5, 20), (0, 0, 0), str(self.node.g_score), 12, self.shape.draw_surface) text3.draw_on_surface(self.shape.rect)
def build_labels(self): intervals = Intervals() intervals.interval_type = self.interval_type intervals.x = self.x intervals.start = self.start intervals.finish = self.finish intervals.resolution = self.resolution intervals.week_start = self.week_start date = Date() date.week_start = self.week_start date.separator = self.separator date.date_format = self.date_format label = Text() label.text_y = self.y label.text_translate_y = self.text_y label.text_translate_x = self.text_x label.font_fill_color = self.font_fill label.font_size = self.font_size label.font_weight = self.font_weight label.font_family = self.font_family label.font_style = self.font_style labels = str() if self.label_type == 'hidden': label.text_visibility = 'hidden' for i in intervals.get_intervals(): label.text_x = i[0] if i[1] < self.min_label_width: label.text = str( ) # you could set visibility to hidden but more verbose elif self.label_type == 'count' and i[3] == 0: label.text = str( ) # you could set visibility to hidden but more verbose elif self.label_type == 'date': date.ordinal_date = i[2] label.text = date.get_date() else: label.text = i[3] # references count in intervals entry labels += label.svg return labels
def text3d(txt, x_y_z, **kwds): r""" Display 3d text. INPUT: - ``txt`` -- some text - ``(x,y,z)`` -- position tuple `(x,y,z)` - ``**kwds`` -- standard 3d graphics options .. note:: There is no way to change the font size or opacity yet. EXAMPLES: We write the word Sage in red at position (1,2,3):: sage: text3d("Sage", (1,2,3), color=(0.5,0,0)) Graphics3d Object We draw a multicolor spiral of numbers:: sage: sum([text3d('%.1f'%n, (cos(n),sin(n),n), color=(n/2,1-n/2,0)) ....: for n in [0,0.2,..,8]]) Graphics3d Object Another example:: sage: text3d("Sage is really neat!!",(2,12,1)) Graphics3d Object And in 3d in two places:: sage: text3d("Sage is...",(2,12,1), color=(1,0,0)) + text3d("quite powerful!!",(4,10,0), color=(0,0,1)) Graphics3d Object """ (x, y, z) = x_y_z if 'color' not in kwds and 'rgbcolor' not in kwds: kwds['color'] = (0, 0, 0) G = Text(txt, **kwds).translate((x, y, z)) G._set_extra_kwds(kwds) return G
def addText(self, xy, text, color="black", fontsize=12, offset=[0, 0], halignment='center', valignment='center', bbox={}, mplprops={}, latex=True, pixel=False, add=True): t = Text.Text(self.cust_float(xy), text, color, self.cust_float(fontsize), self.cust_float(offset), halignment, valignment, bbox, latex, pixel, mplprops, self) if add: self.drawOrder.append(t) return t
def frame_labels(lower_left, upper_right, label_lower_left, label_upper_right, eps = 1, **kwds): """ Draw correct labels for a given frame in 3-D. Primarily used as a helper function for creating frames for 3-D graphics viewing - do not use directly unless you know what you are doing! INPUT: - ``lower_left`` - the lower left corner of the frame, as a list, tuple, or vector. - ``upper_right`` - the upper right corner of the frame, as a list, tuple, or vector. - ``label_lower_left`` - the label for the lower left corner of the frame, as a list, tuple, or vector. This label must actually have all coordinates less than the coordinates of the other label. - ``label_upper_right`` - the label for the upper right corner of the frame, as a list, tuple, or vector. This label must actually have all coordinates greater than the coordinates of the other label. - ``eps`` - (default: 1) a parameter for how far away from the frame to put the labels. Type ``line3d.options`` for a dictionary of the default options for lines, which are also available. EXAMPLES: We can use it directly:: sage: from sage.plot.plot3d.shapes2 import frame_labels sage: frame_labels([1,2,3],[4,5,6],[1,2,3],[4,5,6]) This is usually used for making an actual plot:: sage: y = var('y') sage: P = plot3d(sin(x^2+y^2),(x,0,pi),(y,0,pi)) sage: a,b = P._rescale_for_frame_aspect_ratio_and_zoom(1.0,[1,1,1],1) sage: F = frame_labels(a,b,*P._box_for_aspect_ratio("automatic",a,b)) sage: F.jmol_repr(F.default_render_params())[0] [['select atomno = 1', 'color atom [76,76,76]', 'label "0.0"']] TESTS:: sage: frame_labels([1,2,3],[4,5,6],[1,2,3],[1,3,4]) Traceback (most recent call last): ... ValueError: Ensure the upper right labels are above and to the right of the lower left labels. """ x0,y0,z0 = lower_left x1,y1,z1 = upper_right lx0,ly0,lz0 = label_lower_left lx1,ly1,lz1 = label_upper_right if (lx1 - lx0) <= 0 or (ly1 - ly0) <= 0 or (lz1 - lz0) <= 0: raise ValueError("Ensure the upper right labels are above and to the right of the lower left labels.") # Helper function for formatting the frame labels from math import log log10 = log(10) nd = lambda a: int(log(a)/log10) def fmt_string(a): b = a/2.0 if b >= 1: return "%.1f" n = max(0, 2 - nd(a/2.0)) return "%%.%sf"%n # Slightly faster than mean for this situation def avg(a,b): return (a+b)/2.0 color = (0.3,0.3,0.3) fmt = fmt_string(lx1 - lx0) T = Text(fmt%lx0, color=color).translate((x0,y0-eps,z0)) T += Text(fmt%avg(lx0,lx1), color=color).translate((avg(x0,x1),y0-eps,z0)) T += Text(fmt%lx1, color=color).translate((x1,y0-eps,z0)) fmt = fmt_string(ly1 - ly0) T += Text(fmt%ly0, color=color).translate((x1+eps,y0,z0)) T += Text(fmt%avg(ly0,ly1), color=color).translate((x1+eps,avg(y0,y1),z0)) T += Text(fmt%ly1, color=color).translate((x1+eps,y1,z0)) fmt = fmt_string(lz1 - lz0) T += Text(fmt%lz0, color=color).translate((x0-eps,y0,z0)) T += Text(fmt%avg(lz0,lz1), color=color).translate((x0-eps,y0,avg(z0,z1))) T += Text(fmt%lz1, color=color).translate((x0-eps,y0,z1)) return T
sage: sum([text3d('%.1f'%n, (cos(n),sin(n),n), color=(n/2,1-n/2,0)) \ for n in [0,0.2,..,8]]) Another example :: sage: text3d("Sage is really neat!!",(2,12,1)) And in 3d in two places:: sage: text3d("Sage is...",(2,12,1), rgbcolor=(1,0,0)) + text3d("quite powerful!!",(4,10,0), rgbcolor=(0,0,1)) """ if not kwds.has_key('color') and not kwds.has_key('rgbcolor'): kwds['color'] = (0,0,0) G = Text(txt, **kwds).translate((x,y,z)) G._set_extra_kwds(kwds) return G class Point(PrimitiveObject): """ Create a position in 3-space, represented by a sphere of fixed size. INPUT: - ``center`` - point (3-tuple) - ``size`` - (default: 1)
def calculate_shapes(self): """returns a list of shapes for plotting""" shapes = [] # calculate the range of x, y if self.curves: x_max = max([max(curve['x']) for curve in self.curves]) x_min = min([min(curve['x']) for curve in self.curves]) y_max = max([max(curve['y']) for curve in self.curves]) y_min = min([min(curve['y']) for curve in self.curves]) self.x_range = (x_min, x_max) self.y_range = (y_min, y_max) # calculate axis y_axis_start = (self.margin + 10, self.margin) y_axis_end = (y_axis_start[0], y_axis_start[1] + self.height - 2 * self.margin) y_axis = Line(self.axis_color, y_axis_start, y_axis_end) shapes.append(y_axis) x_axis_start = y_axis_end x_axis_end = (x_axis_start[0] + self.width - 2 * self.margin, x_axis_start[1]) x_axis = Line(self.axis_color, x_axis_start, x_axis_end) shapes.append(x_axis) # values on the axis anchor_length = 2 anchors = 5 anchor_distance = int((y_axis_end[1] - y_axis_start[1]) / 5) mid_point = (0, 0) for i in range(anchors): start_pos = (y_axis_start[0], y_axis_start[1] + anchor_distance * (i + 1)) end_pos = (start_pos[0] - anchor_length, start_pos[1]) anchor_shape = Line(self.axis_color, start_pos, end_pos) shapes.append(anchor_shape) if i == 0: mid_point = start_pos # add value text for the anchor if self.y_range[1] - self.y_range[0]: anchor_value = int(self.y_range[1] * 0.25 * (anchors - 1 - i)) rendered_text = self.digit_font.render(str(anchor_value), True, self.text_color) anchor_text = Text(rendered_text, (end_pos[0] - 17, end_pos[1] - 3)) shapes.append(anchor_text) elif i == 0: anchor_value = int(self.y_range[1]) rendered_text = self.digit_font.render(str(anchor_value), True, self.text_color) anchor_text = Text(rendered_text, (end_pos[0] - 17, end_pos[1] - 3)) shapes.append(anchor_text) # add points and constants last_x, last_y = (0, 0) for curve in self.curves: x_values = curve['x'] y_values = curve['y'] for i in range(len(x_values)): if not self.x_range[1] - self.x_range[0]: x, y = mid_point else: try: x = int(x_axis_start[0] + (x_axis_end[0] - x_axis_start[0] - 20) / (self.x_range[1] - 1) * (x_values[i] - 1)) except ZeroDivisionError: x = mid_point[0] try: y = int(y_axis_end[1] - anchor_distance * (anchors - 1) / self.y_range[1] * y_values[i]) except ZeroDivisionError: y = mid_point[1] # connect points with line if i: line = Line(curve['color'], (last_x, last_y), (x, y)) shapes.append(line) last_x, last_y = x, y # add label to curve if curve['label']: label_text = self.label_font.render(curve['label'], True, curve['color']) label = Text(label_text, (last_x + 5, last_y - 3)) shapes.append(label) if self.constants: for constant in self.constants: start_x = x_axis_start[0] end_x = x_axis_end[0] - 20 if self.y_range[1] - self.y_range[0] and constant[ 'value'] - self.y_range[0]: y = int(y_axis_end[1] - anchor_distance * (anchors - 1) / self.y_range[1] * constant['value']) else: y = mid_point[1] line = DashLine(constant['color'], (start_x, y), (end_x, y), dash_length=5) shapes.append(line) # add label to constant if constant['label']: constant_text = self.label_font.render( constant['label'], True, constant['color']) constant = Text(constant_text, (end_x + 5, y - 3)) shapes.append(constant) # add caption if self.caption: rendered_text = self.caption_font.render(self.caption, True, self.text_color) caption_text = Text(rendered_text, (self.margin + 40, 0)) shapes.append(caption_text) self._add_offset(shapes) return shapes
'''Main file for testing drawing utils''' #pylint: disable=E1101 import pygame from shapes import Rectangle from shapes import Circle from shapes import Text from vector2 import Vector2 pygame.init() SCREEN = pygame.display.set_mode((1080, 720)) SCREEN.fill((255, 255, 255)) RECT = Rectangle(Vector2(100, 100), (0, 255, 0), (100, 100), SCREEN) CIRC = Circle(Vector2(400, 400), (255, 0, 0), 50, SCREEN) TEXT = Text(Vector2(10, 10), (255, 0, 0), "Hello World", 24, SCREEN) RUNNING = True while RUNNING: for event in pygame.event.get(): if event.type == pygame.QUIT: RUNNING = False RECT.draw() CIRC.draw() TEXT.draw() pygame.display.flip()