segment a large integer

The question is (maybe) not really clear :slight_smile:

I have a number of rays - exactly 125. Some must swipe up to down (boolean true), others down to up (so false). I would like to segment these gameobjects in order to get this :

1 - 25 → true
26 - 50 → false
51 - 75 → true
76 - 100 → false
101 - 125 → true

I guess that there is something more efficient than the simple pseudo code below :

if (i => 0 && i <= 25) = true
if (i >= 26 && i <= 50) = false
if (i >= 51 && i <= 75) = true
if (i >= 76 && i <= 100) = false
if (i >= 101 && i <= 125) = true
...

How could you code this section? Maybe using a modulo? Thanks for your help ++

You probably mean “more elegant”? Does your code work as it is? If you don’t like it as it is, maybe try a switch statement?

Efficient as in faster runtime execution or fewer lines of code?

I would write it like so:

if (i < 25) = true
else if (i < 50) = false
else if (i < 75) = true
else if (i < 100) = false
else = true

Your solution is elegant - and it works as expected. However I am trying to find a good function checking this integer as an argument. According to my first explanation, you used the integer 125. But what about another like 150 or 777 ?

I show you what I am trying to implement :

private void AdjustRays()
{
    RaycastHit hit = new RaycastHit();
    int ratio;

    for (int i = 0; i < numOfRays; i++)
    {   // simple brush up to down
        if (feature == 1)
        {
            if (AdjustRayFromRaycast(rays[i].transform, horizontalScanAngle, scanAngle - Mathf.Sin(i), Mathf.PI * Random.Range(i / (float)numOfRays, i + 1 / (float)numOfRays), ref hit))
                CreateDotFromRaycast(hit);
        }
        // simple brush down to up
        if (feature == 2)
        {
            if (AdjustRayFromRaycast(rays[i].transform, horizontalScanAngle, -scanAngle + Mathf.Sin(i), Mathf.PI * Random.Range(i / (float)numOfRays, i + 1 / (float)numOfRays), ref hit))
                CreateDotFromRaycast(hit);
        }
        // butterfly brush
        if (feature == 3)
        {
            if (i % 2 == 0)
                ratio = 1;
            else
                ratio = -1;

            if (AdjustRayFromRaycast(rays[i].transform, horizontalScanAngle, (scanAngle * ratio) - Mathf.Sin(i), Mathf.PI * Random.Range(i / (float)numOfRays, i + 1 / (float)numOfRays), ref hit))
                CreateDotFromRaycast(hit);
        }
    }
}

The first part displays these rays up to down, the second - down to up, and the third creates something like a butterfly effect. Playing with numOfRays, I am trying to create other effects - maybe something divided by 5 to get three section swiping up to down, and two - down to up ++

This sounds like an abuse of integer numbers to encode meaning. There are enums for that very purpose:

public enum Swipe {
UpToDown, DownToUp, Butterfly,
}

if (feature == Swipe.UpToDown)
else if (feature == Swipe.DownToUp)
else if (feature == Swipe.Butterfly)

Note: do not ignore the “else”. While this works without else it requires the code to check all three variants and if the conditions get more complex you may accidentally execute several branches at the same time.

And move the call to AdjustRayFromRaycast after the conditions. You only have the third parameter depend on the type of swipe, so you calculate that and put it in a variable and afterwards call the AdjustRayFromRaycast once to simplify the code and make it more resilient and easier to change.

Looks the best way to do it. I need to learn more about those enums. :slight_smile:

Here’s an efficient solution that will work for all input values of i:
bool swipeUpDown = (i-1) / 25 % 2 == 0;

Thanks for your contribution but there are people who follow this thread and have no idea what that means. Could you, please, show the default method?

You are right. I changed my code for an enum. Thanks ++