const int OCPiano::PosToPitch(QPoint Pos) { Pos.setX(Pos.x()-5); int Octave=IntDiv(IntDiv(Pos.x()+WhiteKeyWidth,WhiteKeyWidth),7); int Key=IntDiv((Pos.x()+WhiteKeyWidth)-((Octave*7)*WhiteKeyWidth),WhiteKeyWidth); int Pitch=0; switch (Key) { case 1: Pitch=2; break; case 2: Pitch=4; break; case 3: Pitch=5; break; case 4: Pitch=7; break; case 5: Pitch=9; break; case 6: Pitch=11; break; } if (Pos.y() < BlackKeyHeight) { int xx=(Pos.x()+WhiteKeyWidth)-(((Octave*7)+Key)*WhiteKeyWidth); switch (Pitch % 12) { case 0: case 2: case 5: case 7: case 9: if (xx>BlackKeyOffset) Pitch++; break; } switch (Pitch % 12) { case 2: case 4: case 7: case 9: case 11: if (xx<(WhiteKeyWidth-BlackKeyOffset)+BlackKeyWidth) Pitch--; break; } } return Pitch+(Octave*12); }
bool IntegerSqrt(Integer &x, Integer &exv) { #ifdef __DEBUG_MODE_ON_ if (IntSmaller(x, IntZero)) throw Debugger::DebugMessage("In IntegerSqrt(Integer&, Integer&),\nGot number below zero.\n"); #endif Integer l = IntZero, r = x; Integer mid; while (IntBigger(IntMinus(r, l), IntOne)) { mid = IntDiv(IntPlus(l, r), IntTwo); if (IntBigger(IntMult(mid, mid), x)) r = mid; else l = mid; } if (IntEqual(IntMult(l, l), x)) { exv = l; return true; } if (IntEqual(IntMult(r, r), x)) { exv = r; return true; } return false; }
const int OCPiano::PitchToPos(const int Pitch) { int X=(IntDiv(Pitch, 12)*7)-1; switch (Pitch % 12) { case 2: case 3: X+=1; break; case 4: X+=2; break; case 5: case 6: X+=3; break; case 7: case 8: X+=4; break; case 9: case 10: X+=5; break; case 11: X+=6; break; } X*=WhiteKeyWidth; if (IsBlackKey(Pitch)) X+=BlackKeyOffset; return X+5; }
// a div b ComplexRational _IntDiv(const ComplexRational &x, const ComplexRational &y) { #ifdef __DEBUG_MODE_ON_ if (!_IsInteger(x) || !_IsInteger(y)) throw Debugger::DebugMessage("In _IntDiv(C,C),\nA parameter is not a integer.\n"); #endif return ComplexRational(IntDiv(x.real, y.real), RatZero); }
void RatStd(Rational &num) { Integer tmp; if (IntSmaller(num.denom, IntZero)) { //num.numer.nonnegative = !num.numer.nonnegative; //num.denom.nonnegative = !num.denom.nonnegative; num.numer = IntMult(num.numer, IntMinusOne); num.denom = IntMult(num.denom, IntMinusOne); } tmp = IntGcd(IntAbs(num.numer), IntAbs(num.denom)); if (!IntEqual(tmp, IntOne)) { num.numer = IntDiv(num.numer, tmp); num.denom = IntDiv(num.denom, tmp); } //return num; }
Integer IntMod(const Integer &a, const Integer &b) { #ifdef __DEBUG_MODE_ON_ if (b.size == 1 && b.num[0] == 0) throw Debugger::DebugMessage("In builtin function IntMod,\nDivided By zero without checking,\ntry to modify it in 'Integer.cpp'"); #endif //Integer exv; //exv = ; return IntMinus(a, IntMult(b, IntDiv(a, b))); }
Rational IntDiv(const Rational &x, const Rational &y) { //x = RatStd(x); y = RatStd(y); //debugger Rational t1(x), t2(y); RatStd(t1); RatStd(t2); #ifdef __DEBUG_MODE_ON_ if (!IntEqual(t1.denom, IntOne) || !IntEqual(t2.denom, IntOne)) { throw Debugger::DebugMessage( "In function IntDiv(Rat), Rational number x,y are not integer." ); } #endif return Rational(IntDiv(t1.numer, t2.numer), IntOne); }
//static_cast Integer RationalTruncate(Rational &x) { return (IntMult(IntDiv(x.numer, x.denom), x.denom)); }