mercredi 30 mars 2016

c++ if statements always return true

I have a bunch of maths manipulations that have thresholds, yet no matter what I change, the if statements always return true. No compile errors, can't get the debugger to work. This is a function, the X Y and Z arrays are all correct (I printed them to check earlier), the maths is right at least for the blade distance check, yet that always returns true. I ran the same code (rewritten obviously) through matlab and that returns true or false depending on my data, so clearly its something with the way I've written this that's wrong. Also is there any way to slimline this?

bool Device::_SafeD(char _Type, float _Data[20][3]) {
bool S;
double x[20], y[20], z[20];
for (int i=0; i<20; i++){
    x[i] = _Data[i][0];
    y[i] = _Data[i][1];
    z[i] = _Data[i][2];

}

// Check angles for needle
if (_Type == 'n'){
    for (int i=0; i<20; i++) {
        float dot, moda, modb, c, angle;
        dot = ((x[i]*x[i+1]) + (y[i]*y[i+1]) + (z[i]*z[i+1]));
        moda = sqrt(pow(x[i],2)+pow(y[i],2)+pow(z[i],2));
        modb = sqrt(pow(x[i+1],2)+(y[i+1],2)+(z[i+1],2));
        c = dot/(moda*modb);
        angle = acos(c);

        if (angle > 45){
          S = 0;}
        else {
          S = 1;
        }
      }

}

// Check distance for blade
if (_Type == 'b'){
      for (int i=0; i<19; i++) {

          float distance = (x[i+1]-x[i]) + (y[i+1]-y[i]) + (z[i+1]-z[i]);
          cout << "distance " << distance << endl;


        if (distance > 5.0) {
          S  = 0;}

        else {
          S =  1;
        }
      }
}
if (S==0){
    return 0;
}
if(S==1){
    return 1;
}

}

Cheers

Aucun commentaire:

Enregistrer un commentaire