I know of pygame.draw.polygon()
but that can only handle colors with no alpha value. Is there an analogous function somewhere that can? I searched for a bit and did not find anything, so I tried writing my own. somehow it misses pixels occasionally (it's worth noting it only needs to work for convex quadrilaterals). here it is, apologies in advance for the poorly written inefficient code:
def fill_quad(A, B, C, D, color=(0, 255, 100, 100)):
M = quad_center(A, B, C, D);
dxA, dyA = A[0] - M[0], A[1] - M[1];
dxB, dyB = B[0] - M[0], B[1] - M[1];
dxC, dyC = C[0] - M[0], C[1] - M[1];
dxD, dyD = D[0] - M[0], D[1] - M[1];
dist = max(math.hypot(dxA, dyA), math.hypot(dxB, dyB), math.hypot(dxC, dyC), math.hypot(dxD, dyD));
dxA, dyA, dxB, dyB, dxC, dyC, dxD, dyD = map(lambda d: d / dist, (dxA, dyA, dxB, dyB, dxC, dyC, dxD, dyD));
for i in range(0, int(dist)+1, 1):
connect(A, B, C, D, color=color);
A = A[0] - dxA, A[1] - dyA;
B = B[0] - dxB, B[1] - dyB;
C = C[0] - dxC, C[1] - dyC;
D = D[0] - dxD, D[1] - dyD;
quad_center
returns the center of the polygon formed by four points and connect
connects points like pygame.draw.lines
except it can do transparency. The trouble seems to be with dxA
, dxB
, etc. and dyA
, dyB
, etc. (as in they are too big of steps) which is why it misses pixels. The issue is not with my connect
function because I have the same problem when using pygame's builtins. In order to run this on your computer just replace connect(A, B, C, D, color=color)
with pygame.draw.lines(screen, color, True, (A, B, C, D))
and use this for quad_center
:
def midpoint(A, B):
return (A[0] + B[0]) / 2, (A[1] + B[1]) / 2;
def quad_center(A, B, C, D):
return midpoint(midpoint(A, B), midpoint(C, D));
Just for completeness here is an example of what I mean when I say "missing pixels":
Each one of those quadrilaterals is drawn with my function. You can see the little blue streaks everywhere, that's what I'm referring to.
If a function like this already exists, obviously I prefer that, but if not any help with the function that I wrote is appreciated. thanks for any help.