Math: Difference between revisions

From wowdev
Jump to navigation Jump to search
(Initial commit)
 
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
== CMath::split ==
== C3Ray ==
 
=== C3Ray::FromStartEnd ===
 
TODO
 
== C3Vector ==
 
=== C3Vector::Mag ===
 
Given a <tt>C3Vector</tt>, calculate and return the magnitude of the vector.
 
<syntaxhighlight lang="cpp">
long double C3Vector::Mag(C3Vector *this) {
 
  float v2;
 
  v2 = this->x * this->x + this->y * this->y + this->z * this->z;
 
  return sqrt(v2);
 
}
</syntaxhighlight>
 
=== C3Vector::MajorAxis ===
 
Given a <tt>C3Vector</tt>, return an integer representing the offset of the vector's major axis.
 
<syntaxhighlight lang="cpp">
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;
 
  }
 
}
</syntaxhighlight>
 
=== C3Vector::MinorAxis ===
 
Given a <tt>C3Vector</tt>, return an integer representing the offset of the vector's minor axis.
 
<syntaxhighlight lang="cpp">
signed int C3Vector::MinorAxis(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;
 
  }
 
}
</syntaxhighlight>
 
=== C3Vector::normalize ===
 
<syntaxhighlight lang="cpp">
void __thiscall C3Vector::normalize(NTempest::C3Vector *this) {
 
  double lenSq = this->x * this->x + this->y * this->y + this->z * this->z;
 
  if (lenSq > 0.00000023841858) {
 
    double invLen = 1.0 / sqrt(lenSq);
 
    this->x = this->x * invLen;
    this->y = this->y * invLen;
    this->z = this->z * invLen;
 
  }
 
}
</syntaxhighlight>
 
== C44Matrix ==
 
=== C44Matrix::Translate ===
 
<syntaxhighlight lang="cpp">
void __thiscall C44Matrix::Translate(C44Matrix *this, C3Vector const& move) {
 
  this->columns[3].x = this->columns[2].x * move->z
                    + this->columns[1].x * move->y
                    + this->columns[0].x * move->x
                    + this->columns[3].x;
 
  this->columns[3].y = this->columns[2].y * move->z
                    + this->columns[1].y * move->y
                    + this->columns[0].y * move->x
                    + this->columns[3].y;
 
  this->columns[3].z = this->columns[2].z * move->z
                    + this->columns[1].z * move->y
                    + this->columns[0].z * move->x
                    + this->columns[3].z;
 
}
</syntaxhighlight>
 
== CAngle ==
 
=== CAngle::ClampTo2Pi ===
 
<syntaxhighlight lang="cpp">
double CAngle::ClampTo2Pi(float angle) {
 
  double v1 = angle;
  double v2 = (OO_TWO_PI_56 * angle) * TWO_PI_56;
  double result;
 
  if (angle >= 0.0) {
 
    result = v1 - v2;
 
  } else {
 
    result = v1 - (v2 - TWO_PI_56);
 
  }
 
  return result;
 
}
</syntaxhighlight>
 
== CMath ==
 
=== CMath::split ===


Given an input of <tt>float xf</tt>, assign the integer part to <tt>int xi</tt>, and the fractional (radix) part to <tt>float xr</tt>.
Given an input of <tt>float xf</tt>, assign the integer part to <tt>int xi</tt>, and the fractional (radix) part to <tt>float xr</tt>.
Line 6: Line 159:
void CMath::split(float xf, float *xr, int *xi) {
void CMath::split(float xf, float *xr, int *xi) {


   if (a1 <= 0.0f) {
   if (xf <= 0.0f) {


     *xi = xf - 1;
     *xi = xf - 1;
Line 13: Line 166:
   } else {
   } else {


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


Line 21: Line 174:
</syntaxhighlight>
</syntaxhighlight>


== CMath::sinoid ==
=== CMath::sinoid ===
 
Approximates sine for the given input <tt>float a1</tt>. This function is typically inlined.
 
<syntaxhighlight lang="cpp">
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 ==
}
</syntaxhighlight>
 
=== CMath::cosoid ===
 
Approximates cosine for the given input <tt>float a1</tt>. This function is typically inlined.
 
<syntaxhighlight lang="cpp">
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;
 
}
</syntaxhighlight>
 
== NTempest ==
 
=== NTempest::DistanceFromPolygon ===
 
TODO
 
=== NTempest::Intersect ===
 
TODO
 
== Other ==
 
=== HSVtoRGB ===
 
Given a <tt>C3Vector</tt> representing a color in the HSV (hue, saturation, value) color model, set a <tt>C3Vector</tt> to an equivalent color in the RGB (red, green, blue) color model.
 
<syntaxhighlight lang="cpp">
// TODO
</syntaxhighlight>
 
=== RGBtoHSV ===
 
Given a <tt>C3Vector</tt> representing a color in the RGB (red, green, blue) color model, set a <tt>C3Vector</tt> to an equivalent color in the HSV (hue, saturation, value) color model.
 
<syntaxhighlight lang="cpp">
// TODO
</syntaxhighlight>

Latest revision as of 05:17, 27 August 2017

C3Ray

C3Ray::FromStartEnd

TODO

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 the offset of the vector's major axis.

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 the offset of the vector's minor axis.

signed int C3Vector::MinorAxis(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;

  }

}

C3Vector::normalize

void __thiscall C3Vector::normalize(NTempest::C3Vector *this) {

  double lenSq = this->x * this->x + this->y * this->y + this->z * this->z;

  if (lenSq > 0.00000023841858) {

    double invLen = 1.0 / sqrt(lenSq);

    this->x = this->x * invLen;
    this->y = this->y * invLen;
    this->z = this->z * invLen;

  }

}

C44Matrix

C44Matrix::Translate

void __thiscall C44Matrix::Translate(C44Matrix *this, C3Vector const& move) {

  this->columns[3].x = this->columns[2].x * move->z
                     + this->columns[1].x * move->y
                     + this->columns[0].x * move->x
                     + this->columns[3].x;

  this->columns[3].y = this->columns[2].y * move->z
                     + this->columns[1].y * move->y
                     + this->columns[0].y * move->x
                     + this->columns[3].y;

  this->columns[3].z = this->columns[2].z * move->z
                     + this->columns[1].z * move->y
                     + this->columns[0].z * move->x
                     + this->columns[3].z;

}

CAngle

CAngle::ClampTo2Pi

double CAngle::ClampTo2Pi(float angle) {

  double v1 = angle;
  double v2 = (OO_TWO_PI_56 * angle) * TWO_PI_56;
  double result;

  if (angle >= 0.0) {

    result = v1 - v2;

  } else {

    result = v1 - (v2 - TWO_PI_56);

  }

  return result;

}

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;

}

NTempest

NTempest::DistanceFromPolygon

TODO

NTempest::Intersect

TODO

Other

HSVtoRGB

Given a C3Vector representing a color in the HSV (hue, saturation, value) color model, set a C3Vector to an equivalent color in the RGB (red, green, blue) color model.

// TODO

RGBtoHSV

Given a C3Vector representing a color in the RGB (red, green, blue) color model, set a C3Vector to an equivalent color in the HSV (hue, saturation, value) color model.

// TODO