Wanting to compare multiple variables of the same type.

Okay. Right now I am writing this program from scratch that is supposed to do basic collision detection for class.

I have run into a problem that, as is, will end up with me doing a ridiculously long series of if statements just to check for the smallest and largest variables in a serious of x and y float variables I have declared.

The code would basically look like this.

1
2
3
4
if(x1 <= x2 && x1 <= x3 && x1 <= x4)
{
  xMin = x1;
}


-and it will be followed by a series of ifs checking each variable.

There would be a series of ifs checking for xMax, yMin and yMax after that.

What I need is a way to compare these in a much more compact, quick way.

I want to avoid using Arrays with this here. Any suggestions?
Last edited on
Really need to know if anyone can provide a suggestion that isn't too complicated.
If you can save you values in to an array, that would make it so much easier!

Then just loop the array checking for the lowest number.
xMin = min(x1, min(x2, min(x3, x4)));

Or in C++11
xMin = min({x1, x2, x3, x4});
Well. Here's the thing.

The current code I am trying to make is being made to detect and determine the intersection of two line segments.

Basically, what I want to do is this, in pseudocode.

1
2
3
4
5
6
7
Now that the user has input the four x and y values for the endpoints of both lines. 
Find the max and min values of both values for both lines. Then calculate the slope for both lines.

Once slope is calculated, begin checking if the xy values within the max/min range of both lines intersect.
if they do not, continue checking, if they do, what is the intersecting pair of values?

Once found, print out the intersecting values to the screen, then end the function.


For something like this. I think Arrays wouldn't be a very smart choice.

What I didn't include in there is that the values are being multiplied by the respective slopes of both lines when increasing the value

@Peter:

Wow... Wish I had thought of that possibility. I forgot about function overloading...
Last edited on
I think I may have found an alternative using the Conditional Operator, but I want to confirm if this would work out.

(x1 == x2) ? xMinA, xMaxA = x1 : xMinA = min (x1, x2), xMaxA = max(x1,x2);

or

(x1 == x2) ? xMinA = x1, xMax = x2 : xMinA= min (x1, x2), xMaxA = max(x1,x2);

If either of them work that is.

It may seem unnecessarily cumbersome, but it is supposed to ensure that, in the event that the x values of both end points are equal, they will both contain the same number. The best part is this makes the code more compact.

Yay, nay?
Topic archived. No new replies allowed.