Skip to content

Instantly share code, notes, and snippets.

@gerryjenkinslb
Created June 29, 2018 21:44
Show Gist options
  • Save gerryjenkinslb/8d433632ab541ad282f0c4fd49371b54 to your computer and use it in GitHub Desktop.
Save gerryjenkinslb/8d433632ab541ad282f0c4fd49371b54 to your computer and use it in GitHub Desktop.
import pygame as pg
import math
from pygame.locals import *
"""
thick_aaline(surface, color, point0, point1, width)
draw a anti-aliased line from point1 to point2 with given width
The algorithms computes the corners (a, b, c, and d)
that form the borders of the thick line and then draws a filled
polygon to accomplish that:
B-----------------------------------------C --+
0 line to 1 | W
A-----------------------------------------B --+
Based the the fact that the delta x and y of the line are
a fixed proportion of the delta x and y of the end lines AB CB
-- by Gerry Jenkins
youtube channel: www.youtube.com/user/gjenkinslbcc
"""
pg.init()
screen = pg.display.set_mode((500, 500))
pg.display.set_caption("aaline with width")
screen.fill(pg.Color('white')) # black background
# line from p0 to p1 with width w
def thick_aaline(display, color, point0, point1, w):
x0, y0 = point0
x1, y1 = point1
proportion = w / math.hypot(x1 - x0, y1 - y0) / 2
adjx = (x1 - x0) * proportion # x side
adjy = (y1 - y0) * proportion # y side
pts = ((x0 - adjy, y0 + adjx), # A
(x0 + adjy, y0 - adjx), # B
(x1 + adjy, y1 - adjx), # C
(x1 - adjy, y1 + adjx)) # D
pg.draw.aalines(display, color, True, pts, True) # outline
pg.draw.polygon(display, color, pts, 0) # fill it
# test it out
p0 = (50, 100)
p1 = (400, 200)
thick_aaline( screen, pg.Color('blue'), p0, p1, 5)
pg.display.update()
done = False
while not done:
for e in pg.event.get():
if e.type == pg.QUIT:
done = True
if e.type == KEYDOWN:
if e.key in (K_ESCAPE, K_SYSREQ):
done = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment