コード例 #1
0
class DrawLine:
    def __init__(self, win, arr):
        pygame.init()
        self.win = win
        self.set = Settings()
        self.arr = arr
        self.printr = Printr(self.win, self.set)
        self.c1 = self.set.light_grey
        self.c2 = self.set.light_blue

    def draw_line(self, pixel_points, arr_points):
        self.pixel_start, self.pixel_mid, self.pixel_end = pixel_points
        self.arr_start, self.arr_mid = arr_points

        pygame.draw.line(self.win, self.c1, tuple(self.pixel_start),
                         tuple(self.pixel_end), 2)

        ## Draw mid circle
        pygame.draw.circle(self.win, self.c1, self.pixel_mid, 6, 2)
        pygame.draw.circle(self.win, self.set.white, self.pixel_mid, 4, 0)

        ## Draw start circle
        pygame.draw.circle(self.win, self.set.object2_538, self.pixel_start, 6,
                           0)

        self.draw_mid_coord()

    def draw_mid_coord(self):
        arr_x, arr_y = self.arr_mid
        pixel_x, pixel_y = self.pixel_mid
        x, y = self.pixel_mid

        arr_text = str((round(arr_x, 1), round(arr_y, 1)))
        pixel_text = str((x, y))

        y -= 11
        self.printr.coord_printr(arr_text, x + 15, y, self.c1)
        #self.printr.coord_printr(pixel_text, x+15, y+15, self.c2)

    def draw_intercepts(self, show_intercepts, intercepts, error):
        if show_intercepts:

            arr_intercepts, pixel_intercepts, pixels_of_array = intercepts

            for i in range(len(pixel_intercepts)):
                x, y1 = pixels_of_array[i]
                x, y2 = pixel_intercepts[i]

                pygame.draw.circle(self.win, self.set.red, (x, y2), 3, 0)
                pygame.draw.line(self.win, self.set.red, (x, y1), (x, y2), 1)

                y = int((y2 - y1) / 2 + y1)
                error_text = m.sqrt(error[i])
                error_text = "error: " + str(round(error_text, 2))
                self.printr.coord_printr(error_text, x + 15, y, self.set.red)
コード例 #2
0
ファイル: draw.py プロジェクト: Biuku/KNN_Game
class Draw:
    def __init__(self, win, pixels):
        pygame.init()
        self.win = win
        self.printr = Printr(self.win)
        self.set = Settings()

        self.pixels = pixels

        self.c0 = self.set.grey
        self.c1 = self.set.light_grey
        self.c2 = self.set.ultra_light_grey

    def draw_plot(self, norm_arr):
        self.draw_axes()
        self.draw_x_axis_labels()
        self.draw_y_axis_labels()
        self.draw_array(norm_arr)
        #self.draw_origin()

    """ DRAW PLOT """

    def draw_array(self, norm_arr):

        for i, arr_coord in enumerate(norm_arr):
            x, y, survived, euclid, nn = arr_coord  ## Last two cols track nn status
            px_coord = self.pixels[i]

            text = "X"
            if survived:
                text = "O"

            c = self.c1
            if nn:
                c = self.set.red
                if survived:
                    c = self.set.blue

            text = self.set.small_font.render(text, True, c)
            self.win.blit(text, px_coord)

    def draw_axes(self):
        x, y = self.false_axes_origin
        right = x + self.false_axis_w
        top = y - self.false_axis_h

        pygame.draw.line(self.win, self.c1, (x, y), (x, top), 2)
        pygame.draw.line(self.win, self.c1, (x, y), (right, y), 2)

    def draw_x_axis_labels(self):
        ### Draw x scale
        y = self.false_axes_origin[1]
        origin_x = self.pixel_origin[0]

        for i in range(len(self.pixel_scale)):
            pixel_x = self.pixel_scale[i, 0]

            ### Draw dot on axis
            pygame.draw.circle(self.win, self.set.grey, (pixel_x, y + 1), 2, 0)

            ### Draw labels
            offset_x = pixel_x - 12

            arr_label = "£" + str(int(self.arr_scale[i, 0]))
            pixel_label = str(int(self.pixel_scale[i, 0]))

            self.printr.coord_printr(arr_label, offset_x, y + 10, self.c0)
            #self.printr.coord_printr(pixel_label, offset_x, y + 25, self.set.blue)

    def draw_y_axis_labels(self):
        ### Print y scale
        x = self.false_axes_origin[0]
        origin_y = self.pixel_origin[1]

        for i in range(len(self.pixel_scale)):
            pixel_y = self.pixel_scale[i, 1]

            ### Draw dot on axis
            pygame.draw.circle(self.win, self.set.grey, (x + 1, pixel_y), 2, 0)

            ### Draw labels
            offset_y = pixel_y - 8
            arr_label = "Age: " + str(int(self.arr_scale[i, 1]))
            pixel_label = str(int(self.pixel_scale[i, 1]))

            self.printr.coord_printr(arr_label, x - 45, offset_y, self.c0)
            #self.printr.coord_printr(pixel_label, x - 40, offset_y + 12, self.set.blue)

    """ DRAW CIRCLE """

    def draw_circle(self, arr_mid, pixel_mid, radius, survived):
        pygame.draw.circle(self.win, self.c2, pixel_mid, radius, 1)
        pygame.draw.circle(self.win, self.set.dark_grey, pixel_mid, 3, 0)

        self.draw_circle_coord(arr_mid, pixel_mid, radius, survived)

    def draw_circle_coord(self, arr_mid, pixel_mid, radius, survived):
        arr_x, arr_y = arr_mid
        px_x, px_y = pixel_mid

        arr_x = "Fare: £" + str(int(arr_x))
        arr_y = "Age: " + str(int(arr_y))

        survive_text = "Prediction: Die"
        if survived:
            survive_text = "Prediction: Live"

        px_coord = "(" + str(px_x) + ", " + str(px_y) + ")"

        y = px_y - 23
        x = px_x + radius + 12

        ## Draw white rect
        pygame.draw.rect(self.win, self.set.background,
                         pygame.Rect(x - 7, y - 5, 92, 53), 0)

        self.printr.coord_printr(arr_x, x, y, self.set.grey)
        self.printr.coord_printr(arr_y, x, y + 15, self.set.grey)
        self.printr.coord_printr(survive_text, x, y + 30, self.set.grey)
        #self.printr.coord_printr(px_coord, x, y + 30, self.set.blue)

    """ Meh """
    """ UTILITY """

    def configure_scales(self, arr, px):
        self.arr_scale = arr
        self.pixel_scale = px
        self.pixel_origin = (px[0, 0], px[0, 1])

        self.configure_false_axes()

    def configure_false_axes(self):
        buffer = 50
        x, y = self.pixel_origin

        self.pixel_w = self.pixel_scale[-1, 0] - self.pixel_scale[0, 0]
        self.pixel_h = self.pixel_scale[0, 1] - self.pixel_scale[-1, 1]

        self.false_axes_origin = (x - buffer, y + buffer)
        self.false_axis_w = self.pixel_w + (2 * buffer)
        self.false_axis_h = self.pixel_h + (2 * buffer)
コード例 #3
0
class Plot(Arr):
    def __init__(self, win):
        pygame.init()
        super().__init__()
        self.win = win
        self.printr = Printr(self.win, self.set)
        self.show_centroid = False
        self.c0 = self.set.grey
        self.c1 = self.set.light_grey  ## Arr coord's
        self.c2 = self.set.light_blue  ## Pixel coords

    def update(self, show_centroid):
        self.show_centroid = show_centroid

    def draw(self):
        self.draw_axes()
        self.draw_x_axes_labels()
        self.draw_y_axes_labels()
        self.draw_centroid()
        self.draw_array()

    def draw_array(self):

        for arr_coord in self.arr:
            px_coord = self.convert_to_pixels(arr_coord)
            pygame.draw.circle(self.win, self.c2, px_coord, 5, 0)

            x, y = px_coord
            arr_text = str((round(arr_coord[0], 1), round(arr_coord[1], 1)))
            pixel_text = str((x, y))

            self.printr.coord_printr(arr_text, x - 35, y + 5, self.c2)

    def draw_centroid(self):

        if self.show_centroid:
            c = self.set.object2_538

            x, y = self.arr_centroid
            text = "Centroid: " + str((round(x, 1), round(y, 1)))

            x, y = self.pixel_centroid

            ### Draw horizontal line, vertical lines
            pygame.draw.line(self.win, c, (self.pixel_x_min, y),
                             (self.pixel_x_max, y), 1)
            pygame.draw.line(self.win, c, (x, self.pixel_y_max),
                             (x, self.pixel_y_min), 1)

            ### Draw centroid circle and coordinates
            pygame.draw.circle(self.win, c, self.pixel_centroid, 5, 0)
            self.printr.coord_printr(text, x + 10, y - 18, self.c1)

    def draw_axes(self):
        c = self.set.light_grey
        x, y = self.false_axes_origin
        right = x + self.false_axis_w
        top = y - self.false_axis_h

        pygame.draw.line(self.win, self.c1, (x, y), (x, top), 2)
        pygame.draw.line(self.win, self.c1, (x, y), (right, y), 2)

    def draw_x_axes_labels(self):

        ### Draw x scale
        y = self.false_axes_origin[1]
        origin_x = self.pixel_origin[0]

        for i in range(self.x_num_labels):
            pixel_x = self.pixel_x_scale[i]

            ### Draw dot on axis
            pygame.draw.circle(self.win, self.set.grey, (pixel_x, y + 1), 2, 0)

            ### Draw labels
            offset_x = pixel_x - 12
            arr_label = str(round(self.arr_x_scale[i], 1))
            #pixel_label = str( int(self.pixel_x_scale[i]) )

            self.printr.coord_printr(arr_label, offset_x, y + 10, self.c0)
            #self.printr.coord_printr(pixel_label, offset_x, y + 25, self.set.blue)

    def draw_y_axes_labels(self):
        ### Print y scale
        x = self.false_axes_origin[0]
        origin_y = self.pixel_origin[1]

        for i in range(self.y_num_labels):
            pixel_y = self.pixel_y_scale[i]

            ### Draw dot on axis
            pygame.draw.circle(self.win, self.set.grey, (x + 1, pixel_y), 2, 0)

            ### Draw labels
            offset_y = pixel_y - 9
            arr_label = str(round(self.arr_y_scale[i], 1))
            #pixel_label = str( int(self.pixel_y_scale[i]) )

            self.printr.coord_printr(arr_label, x - 30, offset_y, self.c0)