r/learnpython • • 9h ago

How come randomly placed drawing don't go to the side of my canva?

Long story short, my assignement is to create a "forest". I already created many functions to draw the trees and a window with a button that allows the user to choose how many trees will be drawn in it. My biggest problem is that I'll have points deducted if the randomly placed trees touch the border. I thought just making sure that my trees coordinates where withing the range that would make them not touch the canva but fr some reason they are always a bit to much to the side and I don't know what to do, because everything I try makes them "escape" the canva. I tried to create a "formula" that'll subtract the width and height of the trees to the x and y coordinates to make sure they don't touch the border but with no success. I thank you guys for any help or advice you might be able to bring!

Here's the relevant code:

from random import*
from turtle import*
from tkinter import*
from math import*

#Screen
screen = Screen()
screen.setup(900,600)
colormode(255)
screen.bgcolor(0, 46, 122)
screen.tracer(0)
hideturtle()
master= screen.getcanvas().master 

#Functions needed to make the tree
def triangle(x,y,cote):
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    colormode(255)
    color(25, 87, 5)
    for i in range(3):
        forward(cote)
        left(120)
    end_fill()
    penup()

def tronc(cote2):
    colormode(255)
    color(66, 33, 1)
    begin_fill()
    for i in range(2):
        forward(cote2)
        right(90)
        forward(cote2*1.5)
        right(90)
    end_fill()
    penup()


def sapin():
    cote= randint(10,100)
    cote_tronc= cote*0.3
#What might be the problem
    x=randint(-abs(425-cote),425-cote)#The abs() is there to make sure it stays within
    y=randint(-abs(225-ceil(cote*1.1)),225-ceil(cote*1.1))
#The coordinates are purposefully in a range smaller than the canva in an attempt
#to have them not "escape" it.
    x2 = x+cote*0.375
    penup()
    goto(x2,y)
    tronc(cote_tronc)
    deplacementX= cote*0.085
    deplacementY= cote*0.3
    for i in range(3):
        triangle(x,y,cote)
        x += deplacementX
        y += deplacementY
        cote *= 0.8

def foret(n):
    for i in range(n):
        sapin()

foret(200)

master.update()
exitonclick()

master.mainloop()
1 Upvotes

6 comments sorted by

2

u/Kevdog824_ 8h ago

Can you explain how you’re attempting to keep them in bounds verbally? This logic looks rather complicated, and I want to see if what you think it’s doing differs from what it’s actually doing

•

u/L_Y_H_ 50m ago

What I tried previously to do to make sure the trees aren't touching the border is substract the length of their width to the number of pixels in a half of the canva (by half I mean the side with the negative x coordinates and the one with the positive ones) thinking that if the coordinates of a tree is always in a range less than that keeps them but It doesn't work no matter what I do.

1

u/lfdfq 8h ago

There's a lot of code there that's a bit hard to read (too many random 'magic numbers')

Your described solution, of subtracting the width/height from the coordinates sounds like it's on the right track. The way you draw your trees means, however, that the coordinates (x2,y) is in the middle of the tree (at the North-West corner of the trunk, since the turtle starts pointing East, and you move it to the trunk, then draw the trunk forward-right-forward-right etc). So the formula isn't just (x2 - width, y - height) it has to be more complicated than that. There's also the fact this is backwards for the opposite edge, where you need to move the tree in the opposite direction! You might want to rethink where you draw the tree from, if it started from the bottom of the trunk it'd be easier to calculate the height (for example).

Your other attempt, of only randomly choosing from inside a smaller box also makes sense, but since the size of your trees are also randomly chosen, you don't know what that should be. You can try calculate the maximum width and height of a tree and subtract that, but this still doesn't quite work because the (x2,y) is in the center of the tree (as said earlier).

1

u/mopslik 8h ago

Not sure what the problem is exactly. I ran your code 20 times, and all of those times, no trees touched the edge of your canvas (I assume the entire window?), nor did they surpass it. Are you seeing different behavoiur on your end? That is, I see:

+----------+
|          |
|   t  t   |
|  tt    t |
|      t   |
|          |
+----------+

rather than

+----------+
|          |
|T     t   |
|  tt    t |
|          |
|     T    |
+----------+

where T would be problem trees.