After many trials and errors, the best way to do this would be:
1) First we define the center of the shape defined by the starting points X0_ {x, y} and X1_ {x, y} of the line.
center_L1 = (X0 + X1) / 2.
2) Then find the slope (angle) of the line.
length = 10
3) Using the parameters of the slope and shape, you can calculate the following coordinates of the end of the box.
UL = (center_L1[0] + (length / 2.) * cos(angle) - (thickness / 2.) * sin(angle), center_L1[1] + (thickness / 2.) * cos(angle) + (length / 2.) * sin(angle)) UR = (center_L1[0] - (length / 2.) * cos(angle) - (thickness / 2.) * sin(angle), center_L1[1] + (thickness / 2.) * cos(angle) - (length / 2.) * sin(angle)) BL = (center_L1[0] + (length / 2.) * cos(angle) + (thickness / 2.) * sin(angle), center_L1[1] - (thickness / 2.) * cos(angle) + (length / 2.) * sin(angle)) BR = (center_L1[0] - (length / 2.) * cos(angle) + (thickness / 2.) * sin(angle), center_L1[1] - (thickness / 2.) * cos(angle) - (length / 2.) * sin(angle))
4) Using the calculated coordinates, we draw a smooth polygon (thanks to @martineau) and then fill it as suggested on the gfxdraw website.
pygame.gfxdraw.aapolygon(window, (UL, UR, BR, BL), color_L1) pygame.gfxdraw.filled_polygon(window, (UL, UR, BR, BL), color_L1)
source share