Using visible line segments to compute a visibility polygon Given a simple polygon $P$ and a point $p$ within the polygon, I want to compute the region in $P$ consisting of all points $q$ visible from point $p$. A point $q$ is visible from point $p$, if the line segment $\overline{qp}$ is completely in the polygon $P$.

As for now, let's ignore the case where a line segment lies completely on the ray.

Now I have a collection of line segments (edges of the polygon) which are visible from point $p$. The problem is that not every line segment in the collection is fully visible which is why I can't simply connect them into a polygon. I first need to compute which part of each line segment is visible and connect those. This last step is what I struggle with.

Here are two examples:



In the left example edge $\overline{AD}$ is partially seen from $p$, in the right example it's $\overline{GH}$ (rest of the edges are completely visible).

For the left example, how can I use the fact that $\overline{EB}, \overline{BC}, \overline{AD}$ and $\overline{DE}$ are visible from point $p$ in order to compute the visibility polygon?

I struggle with finding an algorithm to do this which doesn't fail for every second case I apply it to. Any suggestions?
First, assume we already have a list $L$ with all segments in $P$ that are at least partially visible from $p$, ordered by the smallest angle $\theta$ such that a segment is visible from $p$ at angle $\theta$. (This can be done with a (angular) sweep line algorithm in $O(n\log n)$) 

Now, observe that if a line from $p$ to some point on the segment $s_i$ in $L$ intersects some segment of $P$, it must intersect either $s_{i-1}$ or $s_{i+1}$ (be careful to wrap around the ends of the list), i.e. one of the neighbours of $s_i$.

Therefore, we can construct the visibility polygon as follows: iterate over all segments $s_i$ in $L$ and compute the visible sub-segment of $s_i$ using the segments $s_{i-1},s_{i+1}$. (call it $s_i^*$) Add it to your visibility polygon and then add the lines connecting the endpoints of $s_i^*$ with $s_{i-1}^*$ and $s_{i+1}^*$ to the visibility polygon (if they aren't already connected directly).