Math to match series: 0, 90, 180, -90, 0, 90, 180, -90,

Question!

Some devices can't use rotation 270, so I want to convert it to -90

How can I make this series use -90, 0, 90, 180 instead?

var val = 0;
    val = (val + 90) % 360; // 90
    val = (val + 90) % 360; // 180
    val = (val + 90) % 360; // 270
    val = (val + 90) % 360; // 0
By : neaumusic


Answers

Here's another little trick to fool the processor:

function toRad(deg){
  return deg * Math.PI / 180;
}

function toDeg(rad){
  return Math.round(rad * 180 / Math.PI);
}

function f(deg){
  var rad = toRad(deg);

  return toDeg(
           Math.round(Math.sin(rad))
           ? Math.asin(Math.sin(rad))
           : Math.acos(Math.cos(rad))
         );
}

var val = 0;

val = (val + 90) % 360; // 90 
console.log(val,f(val));

val = (val + 90) % 360; // 180 
console.log(val,f(val));

val = (val + 90) % 360; // 270 
console.log(val,f(val));

val = (val + 90) % 360; // 0
console.log(val,f(val));



Here is an example of shifting the modulo series 0..90..180..270 by subtracting 90 at the end (generator function, but can be applied to anything else really, just shows the pattern)

function *g() {
  let x = 0;
  while(true) {
    x = (x + 90) % 360;
    yield x - 90;
  }
}
let gen = g();
for (let i = 0; i < 10; i++)
  console.log(gen.next().value);

By : ASDFGerte


You can add (almost) 180 before the modulus, and subtract it later.

Not exactly 180 because then you would get -180 instead of 180.

var val = 0;
for(var i=0; i<8; ++i) {
  console.log(val = (val + 90 + 179) % 360 - 179);
}

By : Oriol


This video can help you solving your question :)
By: admin