Тема: pygame | координати перетину двох ліній
Доброго дня.
Почув цікаву штучку (щось про визначені інтеграли)
Вирішив зробити щось подібне.
Ось: (прикріплені файли)
Синя полоска то місце куди я нажав мишкою
 
проблема: неможу знайти точку перетину двох ліній
спроби:
Зайшов у вікіпедію, почитав про точки перетину і як їх знаходити за координатама, щось не вийшло
def get_cord(rect1_cord, rect2_cord) -> float:
    x1 = rect1_cord[0][0]
    y1 = rect1_cord[0][1]
    x2 = rect1_cord[1][0]
    y2 = rect1_cord[1][1]
    x3 = rect2_cord[0][0]
    y3 = rect2_cord[0][1]
    x4 = int(rect2_cord[1][0])
    y4 = int(rect2_cord[1][1])
    
    matrix = [
        [x2 - x1, x4 - x3, x3 - x1],
        [y2 - y1, y4 - y3, y3 - y1]
    ]
    delta_x = matrix[0][2] * matrix[1][1] - matrix[0][1] * matrix[1][2]
    delta_y = matrix[1][2] * matrix[0][0] + matrix[1][0] * matrix[0][2]
    delta   = matrix[1][1] * matrix[0][0] - matrix[1][0] * matrix[0][1]
    if delta_x > 0 and delta_y > 0 and delta > 0:
        return (delta_x / delta, delta_y / delta)Вирішив, чому б не знайти попіксельно де вони перетинаються за допомогою цикла for і функції яка каже чи перетинаються фігури
    def __get_cord(self):
        x = self.rect2_cord[0][0]
        for y in range(int(self.y_1 * 9)):
            if y < self.y_1:
                continue
            rect = pygame.draw.rect(self.screen, self.lucky_color, ((x * self.x_1, y), (1, 1)))
            if rect.colliderect(self.rect1):
                return (x - 1, 9 - (y // self.y_1))теж невдача, воно чомусь зупиняєтся коли доходить до y = FUNCTION_END_POS[1]
також шукав в інтернеті метод який би вирішив цю проблему
результат негативний =(
можливо я для цеї задачки вибра не ту бібліотеку, а може навіть і не ту мову ?(
upd: забув код скинути :D :D :D
файлами можна кинути лише 1 файл, тому...
DecardSystem.py
import pygame
import consts
class DecardSystem():
    def __init__(
        self, *,
        main_color         = consts.MAIN_COLOR,
        decard_y           = consts.DECARD_Y,
        decard_x           = consts.DECARD_X,
        size_font          = consts.SIZE_FONT
        ):
        self.main_color    = pygame.Color(main_color)
        self.decard_y      = decard_y
        self.decard_x      = decard_x
        self.size_font     = size_font
    def draw(self, screen, font) -> None:
        pygame.draw.rect(screen, self.main_color, self.decard_y)
        pygame.draw.rect(screen, self.main_color, self.decard_x)
        lens_y = self.decard_y[1] +  self.decard_y[0] - (self.size_font/4)
        lens_x = self.decard_x[0] - (self.size_font/4)
        for i in range(9):
            text = font.render(str(i), True, self.main_color)
            #for y
            lens_y -= self.decard_y[0]
            screen.blit(text, [self.decard_y[0] - (self.size_font/2), lens_y])
            if i == 0: continue #skip x == 0 but y == 0 too
            #for x
            lens_x += self.decard_x[0]
            screen.blit(text, [lens_x, (self.decard_x[0] * 9) + (self.size_font/4)])Function.py
import pygame
import consts
class Function():
    def __init__(
        self, *,
        x_1                     = consts.X_1,
        y_1                     = consts.Y_1,
        main_color              = consts.MAIN_COLOR,
        red_color               = consts.RED_COLOR,
        green_color             = consts.GREEN_COLOR,
        function_start_pos      = consts.FUNCTION_START_POS,
        function_end_pos        = consts.FUNCTION_END_POS,
        ):
        self.x_1                = x_1
        self.y_1                = y_1 * 9
        self.main_color         = pygame.Color(main_color)
        self.red_color          = pygame.Color(red_color)
        self.green_color        = pygame.Color(green_color)
        self.function_start_pos = function_start_pos
        self.function_end_pos   = function_end_pos
    #ЗЕЛЕНИЙ ПРЯМОКУТНИК
    def __bottom_defined_integral(self) -> pygame.Rect:
        if self.function_end_pos[1] > self.function_start_pos[1]:
            color = self.red_color
        else:
            color = self.green_color
        pygame.draw.rect(
            self.screen,
            color,
            (
                self.function_start_pos[0],
                self.function_end_pos[1],
                self.function_end_pos[0] - self.function_start_pos[0],
                self.y_1 - self.function_end_pos[1]
            ),
            5 #len green rect
        )
    #ЧЕРВОНИЙ ПРЯМОКУТНИК
    def __upper_defined_integral(self) -> pygame.Rect:
        if self.function_end_pos[1] < self.function_start_pos[1]:
            color = self.red_color
        else:
            color = self.green_color
        pygame.draw.rect(
            self.screen,
            color,
            (
                self.function_start_pos[0],
                self.function_start_pos[1],
                self.function_end_pos[0] - self.function_start_pos[0],
                self.y_1 - self.function_start_pos[1]
            ),
            3 #len red rect
        )
    def draw(self, screen) -> pygame.Rect:
        self.screen = screen
        rect1 = pygame.draw.aaline(
            self.screen,
            self.main_color,
            self.function_start_pos,
            self.function_end_pos
        )
        self.__bottom_defined_integral()
        self.__upper_defined_integral()
        return rect1Mouse.py
import pygame
import consts
class Mouse():
    def __init__(
        self, *,
        x_1                     = consts.X_1,
        y_1                     = consts.Y_1,
        blue_color              = consts.BLUE_COLOR
        ):
        self.x_1                = x_1
        self.y_1                = y_1
        self.blue_color         = pygame.Color(blue_color)
        self.size_line          = 7
        self.y_start = 0
        self.y_end   = 8
    #МАЛЮВАННЯ СИНЬОЇ ЛІНІЇ
    def draw_click(self, screen, mouse_pos) -> pygame.Rect:
        add0 = 0
        if (mouse_pos[0] % self.x_1) > (self.x_1 / 2):
            add0 = self.x_1 
        
        mouse = (mouse_pos[0] - (mouse_pos[0] % self.x_1) + add0, self.y_1)
        rect2 = pygame.draw.line(screen, self.blue_color, mouse, (mouse[0], self.y_1 * 9), self.size_line)
        return rect2, ((mouse[0] // self.x_1, self.y_start), (mouse[0] // self.x_1, self.y_end)) consts.py
WIDTH              = 800
HEIGHT             = WIDTH
SIZE               = 1
SIZE_FONT          = 30
FPS                = 30 #Frame pres second
CORDS_FIRST        = (1, 1) #first dot
CORDS_LAST         = (5, 5) #last dot
#DONT CHANGE
X_1                = WIDTH / 10
Y_1                = HEIGHT / 10
LEN_LINE_Y         = X_1 * 8
LEN_LINE_X         = Y_1 * 8
#координата Х, координата У, ширина, довжина
DECARD_Y           = (X_1, Y_1*9, LEN_LINE_Y, SIZE)
DECARD_X           = (X_1, Y_1,   SIZE, LEN_LINE_X)
FUNCTION_START_POS = (X_1 + X_1 * CORDS_FIRST[0], HEIGHT - Y_1 - Y_1 * CORDS_FIRST[1])
FUNCTION_END_POS   = (X_1 + X_1 * CORDS_LAST[0], HEIGHT - Y_1 - Y_1 * CORDS_LAST[1])
BACKGROUND_COLOR = "#000000"
MAIN_COLOR       = "#FFFFFF"
RED_COLOR        = "#FF0000"
GREEN_COLOR      = "#00FF00"
BLUE_COLOR       = "#0000FF"
LUCKY_COLOR      = (80, 80, 80)
CAPTION          = "TEST"main.py 4.02 kb, 199 downloads since 2021-09-09


