Math: Difference between revisions

From wowdev
Jump to navigation Jump to search
Line 22: Line 22:


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
signed int C3Vector::MajorAxis(NTempest::C3Vector *this) {
signed int C3Vector::MajorAxis(C3Vector *this) {


   float fx = fabs(this->x);
   float fx = fabs(this->x);

Revision as of 02:01, 2 May 2017

C3Vector

C3Vector::Mag

Given a C3Vector, calculate and return the magnitude of the vector.

long double C3Vector::Mag(C3Vector *this) {

  float v2;

  v2 = this->x * this->x + this->y * this->y + this->z * this->z;

  return sqrt(v2);

}

C3Vector::MajorAxis

Given a C3Vector, return an integer representing which of the 3 axes of the vector is major.

signed int C3Vector::MajorAxis(C3Vector *this) {

  float fx = fabs(this->x);
  float fy = fabs(this->y);
  float fz = fabs(this->z);

  if (fx > fy && fx > fz) {

    return 0;

  } else if (fy > fz) {

    return 1;

  } else {

    return 2;

  }

}

C3Vector::MinorAxis

Given a C3Vector, return an integer representing which of the 3 axes of the vector is minor.

signed int C3Vector::MinorAxis(NTempest::C3Vector *this) {

  float fx = fabs(this->x);
  float fy = fabs(this->y);
  float fz = fabs(this->z);

  if (fz < fy && fz < fx) {

    return 2;

  } else if (fy < fx) {

    return 1;

  } else {

    return 0;

  }

}

CMath

CMath::split

Given an input of float xf, assign the integer part to int xi, and the fractional (radix) part to float xr.

void CMath::split(float xf, float *xr, int *xi) {

  if (xf <= 0.0f) {

    *xi = xf - 1;
    *xr = xf - *xi;

  } else {

    *xi = xf;
    *xr = xf - *xi;

  }

}

CMath::sinoid

Approximates sine for the given input float a1. This function is typically inlined.

double CMath::sinoid(float a1) {

  int xi;
  float xr;

  float v1 = (a1 * M_1_PI) - 0.5;

  CMath::split(v1, &xr, &xi);

  double result = 1.0 - xr * ((6.0 - 4.0 * xr) * xr);

  if (xi & 1) {
    result = -result;
  }
  
  return result;

}

CMath::cosoid

Approximates cosine for the given input float a1. This function is typically inlined.

double CMath::cosoid(float a1) {

  int xi;
  float xr;

  float v1 = a1 * M_1_PI;

  CMath::split(v1, &xr, &xi);

  double result = 1.0 - xr * ((6.0 - 4.0 * xr) * xr);

  if (xi & 1) {
    result = -result;
  }
  
  return result;

}