Advertisement
Guest User

test.py

a guest
Feb 12th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import pygame
  2. from pygame import gfxdraw
  3. from random import randint
  4. from win32api import GetSystemMetrics
  5. from time import time
  6. import math
  7. import numpy as np
  8.  
  9. width = GetSystemMetrics(0)
  10. height = GetSystemMetrics(1)
  11.  
  12. def generate_original_points_coordinates(circle_size_px):
  13.     generating = True
  14.     while generating:
  15.         x = randint(0,width)
  16.         y = randint(0,height)
  17.         if y < height-circle_size_px and y > circle_size_px and x < width-circle_size_px and x > circle_size_px:
  18.             points.append([x, y])
  19.             generating = False
  20.  
  21. def generate_points_coordinates(count, spacing, circle_size_px):
  22.     for i in range(0,count-1):
  23.         generating = True
  24.         while generating:
  25.             x = points[-1][0]+randint(-spacing,spacing)
  26.             y = points[-1][1]+randint(-spacing,spacing)
  27.             if y < height-circle_size_px and y > circle_size_px and x < width-circle_size_px and x > circle_size_px:
  28.                 generating = False
  29.                 points.append([x, y])
  30.        
  31. def generate_original_and_p(count, spacing, circle_size_px):
  32.     generate_original_points_coordinates(circle_size_px)
  33.     generate_points_coordinates(count, spacing, circle_size_px)
  34.     return points
  35.  
  36. running = True
  37. while running:
  38.     pygame.font.init()
  39.     font = pygame.font.SysFont('freesansbold.ttf', 32)
  40.     text = font.render('NotBezier', True, (0,0,255))
  41.     points = []
  42.     gen_points = generate_original_and_p(9,800,234)
  43.     size = [width, height]
  44.     screen = pygame.display.set_mode(size,0,32)
  45.     screen.fill((33,33,33))
  46.     curve = []
  47.     # Draw bezier curves from points in a list
  48.     for i in range(0,len(gen_points)-2):
  49.         dist_s = math.sqrt(math.pow(gen_points[i+2][0]-gen_points[i][0],2)+math.pow(gen_points[i+2][1]-gen_points[i][1],2))
  50.         dist_x = gen_points[i+2][0] - gen_points[i][0]
  51.         dist_y = gen_points[i+2][1] - gen_points[i][1]
  52.         if dist_s < 234/2:
  53.             line = []
  54.             print(dist_s)
  55.             gfxdraw.line(screen,gen_points[i][0], gen_points[i][1], gen_points[i+2][0], gen_points[i+2][1],(0,0,255))
  56.             m = (gen_points[i][1] - gen_points[i+2][1]) / (gen_points[i][0] - gen_points[i+2][0])
  57.             b = np.linalg.norm(np.diff([gen_points[i],gen_points[i+2]], axis=0), axis=1).tolist()[0]
  58.             for ratio in np.linspace(0,1,10):
  59.                 print("ratio = "+str(ratio)+", m = "+str(m)+", b = "+str(b)+", dist_x = "+str(dist_x)+", dist_y = "+str(dist_y))
  60.                 line.append([gen_points[i][0]+(ratio * dist_x), gen_points[i][1] + (m * (ratio * dist_y) + b)])
  61.             curve.append(line)
  62.         if i+2 == len(gen_points)-1:
  63.             gfxdraw.filled_circle(screen, gen_points[i+2][0], gen_points[i+2][1],5,(0,255,0))
  64.             screen.blit(font.render(str(i+2), True, (0,255,0)), (gen_points[i+2][0],gen_points[i+2][1]+10))
  65.         gfxdraw.filled_circle(screen, gen_points[i][0], gen_points[i][1],5,(0,255,0))
  66.         screen.blit(font.render(str(i), True, (0,255,0)), (gen_points[i][0],gen_points[i][1]+10))
  67.  
  68.     pygame.display.flip()
  69.  
  70.     # Place points on such curves and lines
  71.  
  72.     print(gen_points)
  73.     print(curve)
  74.     print(len(curve))
  75.     i = input("quit?: ")
  76.     pygame.display.quit()
  77.     if i == "quit":
  78.         running = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement