-1

In my game I would like to create smaller green circles inside a larger oblong shape but they have to be inside it. So far my code has them generated randomly which works for a lot of them but there is almost always one which generates on or outside of the oblong. The game has various cells and all but 2 of them are circular. The non-circular ones are this shape:

from tkinter import Tk,Canvas
import random as r

WOODEN="#CDA678"

root=Tk()

canv=Canvas(master=root,width=80,height=180,bg=WOODEN)
pt1=canv.create_arc(2,2,78,78,start=0,extent=180)
pt2=canv.create_arc(2,102,78,178,start=180,extent=180)
pt3=canv.create_line(2,40,78,40,fill=WOODEN)
pt4=canv.create_line(2,140,78,140,fill=WOODEN)
pt5=canv.create_line(2,40,2,140)
pt6=canv.create_line(78,40,78,140)
canv.pack()


for x in range(6):   #Inside this for loop is where I am struggling
  x=r.randint(0,60)
  y=r.randint(0,60)
  canv.create_oval((x,y,x+20,y+20),outline="black",fill="green")

root.mainloop()

Is there a way to do this

  • How is this question different from your [other question](https://stackoverflow.com/q/77014395/13629335) ? – Thingamabobs Aug 31 '23 at 16:53
  • The previous question was a circle but I can't see how the answer that was given there helps with this question which is an oblong shape instead – Shaken not Stirred Aug 31 '23 at 17:00
  • 1
    I can't see how these questions belong to [so]. You might want to visit a different site for math problems. [Startingpoint](https://math.stackexchange.com/questions/tagged/geometry) – Thingamabobs Aug 31 '23 at 17:04
  • I would argue this is still programming based because the answer doesn't have to be maths related, the circle one happened to be because it was the easiest way to do it with tkinter. Additionally, a lot of programming problems involve maths to figure out – Shaken not Stirred Aug 31 '23 at 17:14
  • Just because code is involved doesn't mean it is a programming matter. Your question can be rephrased to "What is the formula to achieve X". – Thingamabobs Aug 31 '23 at 17:25
  • One possible solutions to this is to use a while loop and to check for collision but the issue is if they are inside the circle but don't touch it, they would get flagged incorrectly. Unless there is an option that corrects for that. That doesn't involve maths. – Shaken not Stirred Aug 31 '23 at 17:29
  • Look, if you had done some research on this topic and you came up with an algorithm that apparently doesn't work, I would call it fine, it's in the grey-zone, even though these kind of questions are less welcomed by the community. But let's be honest, all you did was asking for someone to do the math for you. Have a look at [this question](https://stackoverflow.com/q/71271840/13629335) for example. – Thingamabobs Aug 31 '23 at 17:40
  • 1
    I know you probably aren't intending to be rude but it is coming across a little bit like that although I can see your frustration with my question. I haven't used this platform much and it is difficult to know how much to write in a question because the last time I put a lot of code in, someone commented that it was too much even though the info that was relevant to the question was all across the code. – Shaken not Stirred Aug 31 '23 at 17:49
  • We do expect people to have an **honest attempt** to solve their issue. That includes that you already been focused on the issue, everything else needs to be stripped away. That's a [mre]. – Thingamabobs Aug 31 '23 at 17:56

0 Answers0