Tangency bug fix: instead of comparing the length of the perpendicular with the radius when determining tangency, confirm that the intersection between the segment and its perpendicular lies on the circle. The length comparison has no guarantee of working since the x component of the perpendicular's new endpoint is randomly chosen for any non-vertical or non-horizontal perpendicular.