jeudi 11 février 2021

Generate lines instead of bezier if distance between 2 points is lower than specified

I wanted to generate a line if the distance between 2 points was lower than the amount of pixels I specified (which is the circle size) except it doesn't do what I want and instead creates weird lines that don't connect enter image description here

as you can see, it's doesn't work as wished, (not saying intended as it's possible I did something wrong with my code causing this issue) here is my code rn:

import pygame
from pygame import gfxdraw
from random import randint
from win32api import GetSystemMetrics
from time import time
import math

class circles(pygame.sprite.Sprite):
    def __init__(self,surface,x,y,cs):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("./assets/circle.png").convert_alpha()
        size = (round(2.25*(109-(9*cs))),round(2.25*(109-(9*cs))))
        self.image = pygame.transform.scale(self.image, size)
        self.draw = surface.blit(self.image,(x-((size[0])/2),y-((size[1])/2)))

width = GetSystemMetrics(0)
height = GetSystemMetrics(1)

def generate_original_points_coordinates():
    points.append([randint(0,width), randint(0,height)])

def generate_points_coordinates(count, spacing, circle_size_px):
    for i in range(0,count):
        generating = True
        while generating:
            x = points[0][0]+randint(-spacing,spacing)
            y = points[0][1]+randint(-spacing,spacing)
            if y < height-circle_size_px and y > circle_size_px and x < width-circle_size_px and x > circle_size_px: 
                generating = False
        points.append([x, y])

def generate_original_and_p(count, spacing, circle_size_px):
    generate_original_points_coordinates()
    generate_points_coordinates(count, spacing, circle_size_px)
    return points

def draw_circles(surface,x,y,r,rgb_code):
    gfxdraw.filled_circle(surface,x,y,r,rgb_code)


running = True
while running:
    start = time()
    points = []
    gen_points = generate_original_and_p(10,800,234)
    size = [width, height]
    screen = pygame.display.set_mode(size,0,32)
    screen.fill((255,255,255))
    circles_list = [circles(screen, gen_points[i][0], gen_points[i][1], 4) for i in range(0,len(gen_points))]

    # Draw bezier curves from points in a list

    for i in range(2,len(gen_points)):
        if math.sqrt(math.pow(gen_points[i-1][0]-gen_points[i-2][0],2)+math.pow(gen_points[i-1][1]-gen_points[i-2][1],2)) > 234:
            gfxdraw.line(screen,gen_points[i-2][0], gen_points[i-2][1], gen_points[i-1][1], gen_points[i-1][1],(0,0,255))
    
    [gfxdraw.bezier(screen, [gen_points[i-2], gen_points[i-1], gen_points[i]],4500,(255,0,0)) for i in range(2, len(gen_points))]

    # Place points on such curves and lines

    
    pygame.display.flip()
    print(gen_points)
    end = time()
    print(end-start)
    i = input("quit?: ")
    pygame.display.quit()
    if i == "quit":
        running = False

I also tried math.hypo but that gave me the same result so I'm short on ideas




Aucun commentaire:

Enregistrer un commentaire