This one's got me really confused, but hopefully y'all will see something I can't for lack of experience and expertise. The following segment of code is part of a larger function that performs a Hough Transform on a series of coordinates to find lines described by the points. I'm looking for a line of a particular length, so this snippet is meant to find all of the coordinates that fall on the line and remove them from the list of coordinates, keeping track of the two endpoints. In the future I will probably be worrying about adjacency, but it's not an issue right now. The problem that I am running into is with the one printf statement that is in the code. If I take it out, the execution will never enter the if statement, even though I can confirm that it satisfies the condition. It doesn't matter what the text I'm printing is, it just has to be there and be at least one character long.

hits is a linked list that holds the coordinates being processed. Each element of the list has an x term and a y term, each with its own function to return it as a double. bestTheta and bestP are values describing the line found earlier in the function, which is at angle bestTheta in degrees from the positive x-axis and shortest distance bestP from the origin. Both variables are doubles. iterations simply keeps track of where in the linked list we are. (x_min, y_min) and (x_max, y_max) are the coordinates of the endpoints of the line. The odd lines that use the integer casting are used to round the values of the coordinates to the resolution of the Hough map. I think that's all you need to know, but let me know if more information would be helpful.

// Handle the case for a vertical line
if (bestTheta == 0)
{
	// Find all points on the line
	iterations = hits.count();
	index = 0;
	x_min = bestP;
	x_max = bestP;
	for (int hitIndex = 0; hitIndex<iterations; hitIndex++)
	{
		hit[0] = hits.getx(index);
		hit[1] = hits.gety(index);
		x = (static_cast<int> (hit[0]/distRes)) * distRes;
		y = (static_cast<int> (hit[1]/distRes)) * distRes;

		printf(" ");

		if (x == bestP)
		{
			if (y > y_max)
			{
				y_max = y;
			}
			if (y < y_min)
			{
				y_min = y;
			}
			hits.del(hit[0], hit[1]);
		}
		else
		{
			index++;
		}
	}
}

So again, when that printf(" "); statement is there, everything works as expected. But if it is taken out, nothing ever gets removed from the linked list. Anyone have a clue?

You haven't posted enough to figure thst out certainly.
My guess is that x and/or bestP are double/float etc and you are committing the cardinal sin of using == with floating point numbers.

I am guessing that x is very very close to bestP but that one is held in a register. The printf causes x to be dropped out of the register onto a memory location (i.e from 80 bit to 64 bit) and then == returns true. It should also work with any other complex command.

If this is the case, please read my post in http://www.daniweb.com/forums/thread160698.html

If not, please post a little more code including the definitions of the variables. Maybe make a short test case.

p.s. Please use preview post to get the [ code ] sections correct.

You are exactly right, and I apologize for not making it clear that x and bestP are both doubles - I thought that it would have been evident in context, but that was perhaps a mistaken assumption. I replaced the printout with an error calculation and changed the if statement as follows:

error = x-bestP;

if ((error<distRes) && (error>-distRes))

It works well now. Thanks for the fix, and I will use the preview to check my code tags henceforth.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.