I am creating a simple drawing program using Zelle's graphics.py in Python
I have a rectangular drawing area and I am trying to get my drawing to stay within the confines of the rectangle without overlap. One of the challenges is that any drawn object is close to the line of the boundaries of the rectangle (although not technically not outside the drawing area) will draw anyway and cause overlap (see attached image)
Here is what I worked on (NOTE : I have not included this practice code because it is included in the main code):
PRACTICE and TESTING
- I researched the formula for determining all the points on the circumference of a circle
- Included the formula in a "for loop" to check through all 360 points
- Using test code program, I plotted out the points to ensure the formula worked properly and it drew a circle with all 360 points. A working model is complete.
I then took the formula into my drawing program to test if it would work. Right away, I had to use a clone() for the circle because the "for loop" was drawing the same object in the same place over and over giving an error.
After that was fixed, I still could not get the "for loop" and circle formula to work for me. I then thought about removing all items from the win.items[:] list IF the drawing was outside of the boundary of the rectangle. I tried this and still could not get it to work. Here is the code of the attempt.
from graphics import *
import random
import math
win = GraphWin("DRAG", 500, 500)
win.master.attributes('-topmost', True)
drawrect=Rectangle(Point(50,50),Point(450,300))
drawrect.setFill("white")
drawrect.draw(win)
testlist=[]
def motion(event):
global radius
x1, y1 = event.x, event.y
if (x1 > 50 and x1 < 450) and (y1 > 50 and y1 < 300):
circ = Circle(Point(x1, y1), radius)
circ.setFill("black")
circ1 = circ.clone()
cx = circ.getCenter().getX()
cy = circ.getCenter().getY()
for i in range(0, 360):
x = radius * math.cos(i) + cx
y = radius * math.sin(i) + cy
if (x > 50 and x < 450) and (y > 50 and y < 300):
circ1 = circ.clone()
circ1.draw(win)
print("drawing")
else:
circ1 = circ.clone()
testlist.append(circ1)
print(testlist)
print(win.items)
for item in win.items[1:]:
print("This is ITEM in win.items list:", item)
if item in testlist:
input("YES - input waiting for enter")
for i in win.items[1:]:
circ.undraw()
circ1.undraw()
win.update()
else:
input("NO - input waiting for enter")
radius = 40
win.bind('<B1-Motion>', motion)
win.mainloop()

If you want only full circles then you need only
like
Full code:
EDIT:
If you would to crop circles then it would be problem.
My first idea was to use directly
tkinter.Canvas()and it will crop elements. But it would need to write all code for drawingCircleand other objects.So I took code from
GraphicWin()which istkinter.Canvas()with all functions to drawCircleand other objects and little modify - and now you can put in window similar to other objects.EDIT:
This better shows that circles are croped.