Help with Streamlining Camera Rotation Direction !MATH!

This is really a MATH question.

So rotation of the camera, when clamped to 360 degrees, can be a value between -360 and 360

So if i take the absolute value of the rotation, it will be a number between 0 and 360

Then i see where it falls between increments of 45 degrees

So it can be between:
0-45
45-90
90-135

And so on

Depending on where it lands, i rotate the camera to look at the NEAREST 90 degree increment resulting in camera looking up/down/left/right

I have a VERY long if/else if chain handling this fine; but my brain has convinced me there is a faster math way that wont take up long lines of code

My brain says:
-you can use division to divide the absolute value of the rotationY of the camera by 45/90/something resulting in a decimal

-then remove the whole numbers until you are left with a decimal

-then round up or down to find 0 or 1 to determine the direction to rotate the camera towards.

-Then use the actual rotation value to determine how many degrees in the found direction the camera must move to reach it’s nearest 90 degree resting increment.

-Then apply negative or positive rotation to the camera depending on the original negativity/positivity of the rotation value

And all this using only a few lines of code!

My brain says its possible. But I can’t visualize it. Is my brain wrong?

Anyone have any ideas?

P.s. the existing code is ugly and long and I’m embarrassed by it. It’s just a long line of if/else if

P.p.s. I’m using craft and the camera dependancy. (Making my own camera)

Thank you for your time,
-Timmy_theBarbarian

@Timmy_theBarbarian Is this kind of what you’re after. You didn’t say how big your if/then statements were, but here’s an example. Move the slider to change the degrees from 0 to 360. I calc the newDeg with an if/then statement and move the red line to the new position. There’s probably a math calculation to do this, but I didn’t think the if/then was that large to figure out the math.

viewer.mode=STANDARD

function setup()
    fontSize(30)
    parameter.integer("deg",0,360)
    rad=WIDTH/2
end

function draw()
    background()
    
    if deg>315 or deg<=45 then
        newDeg=0
    elseif deg>=45 and deg<=135 then
        newDeg=90
    elseif deg>=135 and deg<=225 then
        newDeg=180
    elseif deg>=225 and deg<=315 then
        newDeg=270
    end
    
    
    stroke(255)
    strokeWidth(2)
    x=math.cos(math.rad(deg))*rad+WIDTH/2
    y=math.sin(math.rad(deg))*rad+HEIGHT/2
    line(WIDTH/2,HEIGHT/2,x,y)

    noFill()
    ellipse(WIDTH/2,HEIGHT/2,rad*2)
    fill(255)
    x=math.cos(math.rad(deg))*rad*.9+WIDTH/2
    y=math.sin(math.rad(deg))*rad*.9+HEIGHT/2
    text(deg,x,y)

    x=math.cos(math.rad(newDeg))*(rad*.8)+WIDTH/2
    y=math.sin(math.rad(newDeg))*(rad*.8)+HEIGHT/2
    strokeWidth(10)
    stroke(255,0,0)
    line(WIDTH/2,HEIGHT/2,x,y)
end

@dave1707 yep yep! That’s exactly how I’m doing it currently. Guess I’m being a bit picky with the number of lines i use. Since I can’t collapse my code YET(i have faith) I need to minimize the landscape

Trying to reduce the 9 lines of ifs to a single calculation. It’s more of a brain game than an actual need.

If I ever figure it out I will share an instruction post on how to use it.

Thanks for the response!
-Timmy_theBarbarian

@Timmy_theBarbarian Here’s the above code modified with one line replacing the 9 if/then statements.

function setup()
    fontSize(30)
    parameter.integer("degs",0,360)
    rad=WIDTH/2
end

function draw()
    background()

    newDeg=(((degs+45)%360)//90)*90
        
    stroke(255)
    strokeWidth(2)
    x=math.cos(math.rad(degs))*rad+WIDTH/2
    y=math.sin(math.rad(degs))*rad+HEIGHT/2
    line(WIDTH/2,HEIGHT/2,x,y)

    noFill()
    ellipse(WIDTH/2,HEIGHT/2,rad*2)
    fill(255)
    x=math.cos(math.rad(degs))*rad*.9+WIDTH/2
    y=math.sin(math.rad(degs))*rad*.9+HEIGHT/2
    text(degs,x,y)

    x=math.cos(math.rad(newDeg))*(rad*.8)+WIDTH/2
    y=math.sin(math.rad(newDeg))*(rad*.8)+HEIGHT/2
    strokeWidth(10)
    stroke(255,0,0)
    line(WIDTH/2,HEIGHT/2,x,y)
end

The man’s a genius! EXACTLY what I was looking for! Thank you sir!

-Timmy_theBarbarian