Thanks MontBlanc2012 for the thorough explanation and code!
If someone else tries to use the code, there is a small typo when the intersections are rotated back:
Xp = math.cos(w1) * xp - math.sin(w1) * yp
Yp = -math.sin(w1) * xp + math.cos(w1) * yp
Xm = math.cos(w1) * xm - math.sin(w1)...