Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame import gfxdraw
- from random import randint
- from win32api import GetSystemMetrics
- from time import time
- import math
- import numpy as np
- width = GetSystemMetrics(0)
- height = GetSystemMetrics(1)
- def generate_original_points_coordinates(circle_size_px):
- generating = True
- while generating:
- x = randint(0,width)
- y = randint(0,height)
- if y < height-circle_size_px and y > circle_size_px and x < width-circle_size_px and x > circle_size_px:
- points.append([x, y])
- generating = False
- def generate_points_coordinates(count, spacing, circle_size_px):
- for i in range(0,count-1):
- generating = True
- while generating:
- x = points[-1][0]+randint(-spacing,spacing)
- y = points[-1][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(circle_size_px)
- generate_points_coordinates(count, spacing, circle_size_px)
- return points
- running = True
- while running:
- pygame.font.init()
- font = pygame.font.SysFont('freesansbold.ttf', 32)
- text = font.render('NotBezier', True, (0,0,255))
- points = []
- gen_points = generate_original_and_p(9,800,234)
- size = [width, height]
- screen = pygame.display.set_mode(size,0,32)
- screen.fill((33,33,33))
- curve = []
- # Draw bezier curves from points in a list
- for i in range(0,len(gen_points)-2):
- 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))
- dist_x = gen_points[i+2][0] - gen_points[i][0]
- dist_y = gen_points[i+2][1] - gen_points[i][1]
- if dist_s < 234/2:
- line = []
- print(dist_s)
- gfxdraw.line(screen,gen_points[i][0], gen_points[i][1], gen_points[i+2][0], gen_points[i+2][1],(0,0,255))
- m = (gen_points[i][1] - gen_points[i+2][1]) / (gen_points[i][0] - gen_points[i+2][0])
- b = np.linalg.norm(np.diff([gen_points[i],gen_points[i+2]], axis=0), axis=1).tolist()[0]
- for ratio in np.linspace(0,1,10):
- print("ratio = "+str(ratio)+", m = "+str(m)+", b = "+str(b)+", dist_x = "+str(dist_x)+", dist_y = "+str(dist_y))
- line.append([gen_points[i][0]+(ratio * dist_x), gen_points[i][1] + (m * (ratio * dist_y) + b)])
- curve.append(line)
- if i+2 == len(gen_points)-1:
- gfxdraw.filled_circle(screen, gen_points[i+2][0], gen_points[i+2][1],5,(0,255,0))
- screen.blit(font.render(str(i+2), True, (0,255,0)), (gen_points[i+2][0],gen_points[i+2][1]+10))
- gfxdraw.filled_circle(screen, gen_points[i][0], gen_points[i][1],5,(0,255,0))
- screen.blit(font.render(str(i), True, (0,255,0)), (gen_points[i][0],gen_points[i][1]+10))
- pygame.display.flip()
- # Place points on such curves and lines
- print(gen_points)
- print(curve)
- print(len(curve))
- i = input("quit?: ")
- pygame.display.quit()
- if i == "quit":
- running = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement