You could also use a ternary statement in the form of:
draw.ellipse(screen,(c),(x,y,radx,rady), sz2 if sz2 < max(radx, raxy) else 0)
Sincerely,
Another Massey student working on a Sunday night ;)
Pregunta
I am currently making changes to the way my ellipse tool works as it was not working the correct way previously. I am creating it for my paint program using python 2.7.5 and pygame. I have recently encountered this error:
Traceback (most recent call last):
File "C:\Users\Wisdom1\Desktop\Comp Science Files\Canvas.py", line 164, in <module>
draw.ellipse(screen,(c),(x,y,radx,rady),sz2)
ValueError: width greater than ellipse radius
This occurs when I try to create an ellipse going in every direction except downward to the right from a point. I understand the error I just do not know how to fix it. Here is my ellipse tool:
if mb[0] == 1 and canvas.collidepoint(mx,my):
screen.set_clip(canvas)
if tool == "ellipse":
screen.blit(copy,(0,0))
radx = max(mx-x,1)
rady = max(my-y,1)
draw.ellipse(screen,(c),(x,y,radx,rady),sz2)
screen.set_clip(None)
Sz2 is a size variable that begins at 10 and decreases or increases by 3 each time the mouse wheel is moved down or up. Any help is appreciated. Thank you
Solución
You could also use a ternary statement in the form of:
draw.ellipse(screen,(c),(x,y,radx,rady), sz2 if sz2 < max(radx, raxy) else 0)
Sincerely,
Another Massey student working on a Sunday night ;)
Otros consejos
If sz2
, the thickness of the curve, is greater than the minor radius of the ellipse, pygame raises a ValueError
. So you could protect against this by using an if-statement
:
if sz2 < min(radx, rady)//2:
pygame.draw.ellipse(self.screen, green, box, sz2)
else:
# sz2=0 fills the ellipse
pygame.draw.ellipse(self.screen, green, box, 0)
radx
, rady
is the width and height of the Rect
bounding the ellipse. So the minor radius is half the smaller of radx
and rady
.
Here is some runnable code showing that the if-statement
works:
"""Based on http://www.pygame.org/docs/tut/intro/intro.html"""
import sys
import pygame
pygame.init()
size = (width, height) = (320, 240)
screen = pygame.display.set_mode(size)
black = (0,0,0)
green = [0, 255, 0]
radx, rady = 50, 70
box = [160-radx//2, 120-rady//2, radx, rady]
width = 1
delta = 2
while True:
for event in pygame.event.get():
if ((event.type == pygame.QUIT) or
(event.type == pygame.KEYDOWN and
event.key == pygame.K_ESCAPE)):
sys.exit()
screen.fill(black)
if 0 < width < min(radx, rady)//2:
pygame.draw.ellipse(screen, green, box, width)
else:
if width > 0:
pygame.draw.ellipse(screen, green, box, 0)
delta *= -1
width += delta
pygame.display.flip()
pygame.time.delay(100)