1. ## Logic Problems

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");
{
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");
{
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(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.

2. I think, instead of looking at the y coordinates of a shared vertex, you should be looking at whether the vertex is concave or convex. Just a guess. One I have no idea how you would implement.

3. Aha.
Code:
switch_made = (next->y > here->y)? 1: -1;
Why are you doing that? Perhaps your lines are set up in such a way that lines which share vertices are contiguous, but the rest of your loop doesn't seem to imply that.

You know what I would do? I would save a pointer to here, a pointer to the previous line that intersected this point. Then you can do your comparisons from there.

BTW . . . what if more than two lines intersect a point?

[offtopic] Why Xfce? [/offtopic]

4. Why are you doing that? Perhaps your lines are set up in such a way that lines which share vertices are contiguous,
Yeah the lines are contiguous, but youre right the code doset show that.

BTW . . . what if more than two lines intersect a point?
Dunno yet, I might have a go at doing this some other way. Salem posted an alternative; might try that later. I'm getting fed up with this atm.

Why Xfce?
Oh, because I'm running eeeXUbuntu. I have Enlightenment installed too, but it dosent work so well with the small screen.