Would you like some sauce for ur spaghetti code

mail@pastecode.io avatar
unknown
python
2 years ago
3.9 kB
7
Indexable
Never
from math import atan,degrees

inp_unp = """
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
"""


class Coordinate:
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

        self.m = (y2-y1)/(x2-x1) if x2 - x1 != 0 else 0

        self.b = y2 - (self.m * x2)


        # une autre approche que j'utilisais c'etait d'essayer de calculer un angle de +- 45 deg
        self.angle = degrees(atan(self.max_height()/self.max_width()))


    def max_height(self):
        return max(self.y1, self.y2)

    def min_height(self):
        return min(self.y1, self.y2)

    def max_width(self):
        return max(self.x1, self.x2)

    def min_width(self):
        return min(self.x1, self.x2)

    def __str__(self):
        return str((self.x1, self.y1, self.x2, self.y2))


class Drawing:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.board = {}
        for row in range(height+1):
            for col in range(width+1):
                self.board[str((row, col))] = 0

    def __add__(self, coord: Coordinate):
        max_w = coord.max_width()
        min_w = coord.min_width()
        max_h = coord.max_height()
        min_h = coord.min_height()
        width = max_w-min_w
        height = max_h-min_h
        for row in range(min_h, max_h+1):
            for col in range(min_w, max_w+1):
                # si c'est une ligne droite, ou bien le point est dans la "pente" de la ligne
                if str((row, col)) in self.board and \
                        (width == 0 or height == 0 
                         or (coord.m*row + coord.b == col 
                             or coord.m*row + coord.b == -col)):
                    self.board[str((row, col))] += 1
                    
                elif width == 0 or height == 0 \
                        or (coord.m*row + coord.b == col 
                            or coord.m*row + coord.b == -col):
                    self.board[str((row, col))] = 1

        return self

    def nb_points_overlap(self):
        nb_points = 0
        for key in self.board:
            if self.board[key] >= 2:
                nb_points += 1
        return nb_points

    def print(self):
        printout = []
        for i in range(0, self.width+1):
            line = ""
            for j in range(0, self.height+1):
                if self.board[str((i,j))] == 0:
                    line += "."
                else:
                    line += str(self.board[str((i,j))])
            printout.append(line)

        return printout



class Solution:
    def __init__(self, inp):
        # parse
        input_unparsed = inp.split("\n")
        coordinates = []
        for line in input_unparsed:
            line = line.strip()
            if line:
                coordinates.append([[int(y) for y in x.split(",")] for x in line.split(" -> ")])

        # create coordinate objects
        self.coordinates = []
        max_w = -99999999
        max_h = -99999999
        min_w = 100000000
        min_h = 100000000

        for item in coordinates:
            x1, y1, x2, y2 = item[0][0], item[0][1], item[1][0], item[1][1]
            self.coordinates.append(Coordinate(x1, y1, x2, y2))
            max_h = max(max_h, y1, y2)
            min_h = min(min_h, y1, y2)
            max_w = max(max_w, x1, x2)
            min_w = min(min_w, x1, x2)

        print(max_h, max_w, min_h, min_w)
        self.board = Drawing(max_w - min_w, max_h - min_h)

    def find_overlapping_lines(self):
        for coord in self.coordinates:
            self.board += coord
        arr = self.board.print()
        for i in arr:
            print(i)
        return self.board.nb_points_overlap()


sol = Solution(inp_unp)
print(sol.find_overlapping_lines())