Python - Pygame - Fill screen with triangles without gaps
-
I am new to this page and am hoping I have this in the right place. I am using python combined with pygame to try and generate a screen that is full of random triangles that completely fill the screen with no gaps while each triangle has random length of sides. So it would be partially random while also taking into account the remaining spaces when generating so that it leaves no space uncovered but no overlapping either. IE. Black background and white triangles, the screen actually consists of 100 triangles but because they are all white the screen just looks solid white. I originally tried to do this with random vertices and trying to connect the sides but that was... messy to say the least and didn't play nice. Does anyone know any path I could look into for figuring this out? I'm not looking for full code of having someone do it for me, I just need to figure this out and get an idea on either a method I'm not thinking of or some math I can look at for generation. Forgive the long winded explanation.
-
I am new to this page and am hoping I have this in the right place. I am using python combined with pygame to try and generate a screen that is full of random triangles that completely fill the screen with no gaps while each triangle has random length of sides. So it would be partially random while also taking into account the remaining spaces when generating so that it leaves no space uncovered but no overlapping either. IE. Black background and white triangles, the screen actually consists of 100 triangles but because they are all white the screen just looks solid white. I originally tried to do this with random vertices and trying to connect the sides but that was... messy to say the least and didn't play nice. Does anyone know any path I could look into for figuring this out? I'm not looking for full code of having someone do it for me, I just need to figure this out and get an idea on either a method I'm not thinking of or some math I can look at for generation. Forgive the long winded explanation.
This is an OK place to ask such a question, but you could also have used the Algorithms forum (because your question isn't specifically about coding in python). If you scatter a random set of points around the screen and want to turn them into a triangular mesh, then Delaunay triangulation[^] is your friend.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
This is an OK place to ask such a question, but you could also have used the Algorithms forum (because your question isn't specifically about coding in python). If you scatter a random set of points around the screen and want to turn them into a triangular mesh, then Delaunay triangulation[^] is your friend.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
Thanks for the location for posting thoughts on that! Also huge thanks on the idea of the Delaunay Triangulation. I was able to make an algorithm for creating a 500 point mesh with 1 point in each corner (to leave no gaps) and the rest randomized and implemented the triangulation and now I have a solid white screen of triangles and have them added to a list as they generate so that each triangle can be interacted with independently. You have been of amazing assistance. -Tairros
-
Thanks for the location for posting thoughts on that! Also huge thanks on the idea of the Delaunay Triangulation. I was able to make an algorithm for creating a 500 point mesh with 1 point in each corner (to leave no gaps) and the rest randomized and implemented the triangulation and now I have a solid white screen of triangles and have them added to a list as they generate so that each triangle can be interacted with independently. You have been of amazing assistance. -Tairros
You're welcome. If you can implement the triangulation in a few hours, you obviously have the coding skills. Often it is just a word or two needed to point someone in the right direction. If only there were a taxonomy of algorithms, rather than a bunch of personal names!
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
I am new to this page and am hoping I have this in the right place. I am using python combined with pygame to try and generate a screen that is full of random triangles that completely fill the screen with no gaps while each triangle has random length of sides. So it would be partially random while also taking into account the remaining spaces when generating so that it leaves no space uncovered but no overlapping either. IE. Black background and white triangles, the screen actually consists of 100 triangles but because they are all white the screen just looks solid white. I originally tried to do this with random vertices and trying to connect the sides but that was... messy to say the least and didn't play nice. Does anyone know any path I could look into for figuring this out? I'm not looking for full code of having someone do it for me, I just need to figure this out and get an idea on either a method I'm not thinking of or some math I can look at for generation. Forgive the long winded explanation.
Generating a screen full of random triangles that completely fill the screen with no gaps can be quite challenging. One approach to solve this problem is to use a recursive algorithm that divides the screen into triangles of random sizes and positions. Here's one way you could implement such an algorithm using Pygame:
import pygame
import randomSet up the Pygame window
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Random Triangles")Define the function to recursively draw triangles
def draw_triangles(x, y, width, height, depth):
if depth == 0:
return# Generate three random points within the bounds of the rectangle x1 = random.randint(x, x + width) y1 = random.randint(y, y + height) x2 = random.randint(x, x + width) y2 = random.randint(y, y + height) x3 = random.randint(x, x + width) y3 = random.randint(y, y + height) # Draw the triangle color = (255, 255, 255) # white pygame.draw.polygon(screen, color, \[(x1, y1), (x2, y2), (x3, y3)\]) # Recursively draw triangles in the three sub-rectangles draw\_triangles(x, y, width // 2, height // 2, depth - 1) draw\_triangles(x + width // 2, y, width // 2, height // 2, depth - 1) draw\_triangles(x, y + height // 2, width // 2, height // 2, depth - 1)
Draw the triangles
draw_triangles(0, 0, 800, 600, 5)
Update the display
pygame.display.flip()
Wait for the user to close the window
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()In this example, we define a recursive function draw_triangles() that takes as input the position and dimensions of a rectangle, as well as a depth value that determines how many levels of recursion to use. The function generates three random points within the rectangle and uses pygame.draw.polygon() to draw a white triangle using those points. It then recursively calls itself on the three sub-rectangles that result from dividing the original rectangle in half horizontally and vertically.
The depth parameter controls the level of detail in the final image. You can adjust this value to get more or fewer triangles in the final image. You can also modify the colors of the triangles or the size of the window to suit your needs.
I hope this helps you get started on your project!