I have been stuck with this for about three hours now and its getting very annoying. Basically I can draw these polygons using a scanline raycasting method. it basically keeps track of whether its inside a polygon or not and draws when inside. When it hits a line the on/off draw state is flipped. This works fine, but where two points meet the state would be flipped twice (s it lies on two lines). Wnen both lines connected to the vertex have their second points both above or below the current vertex this is what I want, but when one line ends above and the other below the state should only be flipped once or I end up getting inverse coloured lines. As shown in this pic.

I wrote some code to determine whether the other end points on the line are above or below and flip accorfingly; the problem is that it seems to have little or no visible effect:

Code:

for(int z=1; z<p.size; z++) //For each line on polygon
{
/* if the point is on a vertex it would normally be switched twice
* points on vertices should be switched twice if:
* On both occurences the second vertex is above below the first
*/
if(x == here->x && y == here->y) //if on vertex
{
printf("a\n");
if(! switch_made)
{
printf("A\n");
switch_made = (next->y > here->y)? 1: -1;
in_poly ^= 1;
}
else if(next->y > here->y) //if this second vertex is lower
{
printf("x\n");
if(switch_made > 0) in_poly ^= 1; //if original second vertex was lower
break;
}
else if(next->y < here->y) //if this second vertex was higher
{
printf("y\n");
if(switch_made < 0) in_poly ^= 1; //if original second vertex was higher
break;
}
}
else if(x == next->x && y == next->y)
{
printf("b\n");
if(! switch_made)
{
switch_made = (here->y > next->y)? 1: -1;
in_poly ^= 1;
}
else if(here->y > next->x) //if second vertex is lower
{
if(switch_made > 0) in_poly ^= 1; //if original second vertex was lower
break;
}
else if(here->y < next->y) // if second vertex is higher
{
if(switch_made < 0) in_poly ^= 1; //If original second vertex was higher
break;
}
printf("B\n");
}// Point is not on a vertex
else if(PointIsWithinLine(here->x, here->y, next->x, next->y, x, y))
{
in_poly ^= 1;
}
here = next;
next = next->next;
}
//IF no switch was made check last point to first
if(! switch_made)
{
if(PointIsWithinLine(here->x, here->y, p.pt->x, p.pt->y, x, y))
in_poly ^= 1;
}

Can anyone tell whats wrong with my logic here?

Cheers.