Burn Badrobo
Burn Badrobo

Reputation: 63

How to put if and then statements while creating snowflakes in python

--im a beginner ..so im not sure how to make sure that the snowflakes don't overlap. Thanks!

import turtle

turtle.right(90)

turtle.penup()

turtle.goto(-700,300)

turtle.pendown()

def snowflakebranch(n):

    turtle.forward(n*4)
    for i in range(3):
        turtle.backward(n)
        turtle.right(45)
        turtle.forward(n)
        turtle.backward(n)
        turtle.left(90)
        turtle.forward(n)
        turtle.backward(n)
        turtle.right(45)

def snowflake(n):

    for i in range(8):
        snowflakebranch(n)
        turtle.backward(n)
        turtle.right(45)

import random

turtle.colormode(255)

turtle.tracer(0)

for i in range(35):

    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    turtle.color(r, g, b)
    x = random.randint(-500, 500)
    y = random.randint(-500, 500)
    d = random.randint(6, 16)
    snowflake(d)
    turtle.penup()
    turtle.goto(x, y)
    #turtle.forward(250)
    turtle.pendown()


    turtle.update()

Upvotes: 3

Views: 246

Answers (1)

cdlane
cdlane

Reputation: 41903

One approach would be to calculate a bounding rectangle (or circle) for each snowflake. Save these as a list or a set. Whenever you plan to make a new snowflake, first check if its bounding rectangle (or circle) overlaps with the bounds of any previous snowflakes. If it does, don't draw it. If it doesn't, draw it and save its bounds too. An incomplete outline of this approach:

import turtle
import random

def snowflakebranch(n):

    turtle.forward(n * 4)

    for _ in range(3):
        turtle.backward(n)
        turtle.right(45)
        turtle.forward(n)
        turtle.backward(n)
        turtle.left(90)
        turtle.forward(n)
        turtle.backward(n)
        turtle.right(45)

def snowflake(n):

    for _ in range(8):
        snowflakebranch(n)
        turtle.backward(n)
        turtle.right(45)

def overlapping(bounds_list, bounds):
    for previous in bounds_list:
        if overlap(previous, bounds):
            return True

    return False

def overlap(b1, b2):
    # return True or False if these two rectanges or circles overlap
    pass

turtle.penup()

turtle.colormode(255)

turtle.tracer(0)

previous_bounds = []

i = 0

while i < 35:

    x = random.randint(-500, 500)
    y = random.randint(-500, 500)
    turtle.goto(x, y)

    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    turtle.color(r, g, b)

    turtle.pendown()

    d = random.randint(6, 16)

    # work out the bounding rectangle or circle based on 'd', 'x' & 'y'
    # e.g. (x, y, width & height) or (x, y, radius)
    bounds = ( ... )  

    if not overlapping(previous_bounds, bounds):

        snowflake(d)

        turtle.update()

        previous_bounds.append(bounds)

        i += 1

    turtle.penup()

turtle.done()

An image of non-overlapping snowflakes using the above logic with the bounding circles also displayed:

enter image description here

I actually like the look of your overlapping snowflakes. Even if you want overlap, the above logic will allow you to control how much overlap.

Upvotes: 1

Related Questions