Thread: Logic Problems

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

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

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM