Vi starter med problemet, nemmelig matematik i c#
//Kode mangler...
///Decide what has relevence
public class sortAccordingToRelevance
{
///purpose: determine whether a number is a power of 2
///input: the number beeing checked
///output: true if it's a power of 2, else false
bool powOfTwo( int num )
{
return !(num & (num - 1));
}
///Some various data types which can bee used to
///store points
///1. an array of floats. The advantage lies in speed, ease,
/// and low memory cost
float 3dPoint[3];
///2. a stucture holding 3 floats. The advantage lies in the
/// ability to overload operators and define functions
struct 3dPoint
{
float x, y, z;
///The overloaded addition operator
3dPoint operator+ (const 3dPoint &P2) const
{
3dPoint = {
this->x + P2.x,
this->y + P2.y,
this->z + P2.z };
return temp;
}
};
///purpose: calculate the slope of a line given 2 points
///input: P1 - an aray of 2 floats representing point 1
/// P2 - an array of 2 floats representing point 2
///output: the slope between our 2 points
float slopeBetweenPoints( float *P1, float *P2 )
{
return( P2[1] - P1[1] / P1[0] - P1[0] );
}
///purpose: to determine the slope of the
///line perpendicular to ourselves
///input: slope - the slope of our line
///output: the slope of the line perpendicular to us
float perpSlope(float slope)
{
return -1 / slope;
}
///purpose: to determine whether two lines are perpendicular
///input: slope1 - the slope of the first line
/// slope2 - the slope of the second line
///output: true if the lines are perpendicular, else false
bool arePerp(float slope1, float slope2)
{
if( slope1 * slope2 == -1)
return true;
return false;
}
///purpose: find the point of intersection between two lines
///input: L1Point - a 2D point along our first line
/// L1Slope - the slope of our first line
/// L2Point - a 2d point along our second line
/// L2Slope - the slope of our second line
///output: our array of floats holding our point
float *lineIntersect(float *L1Point, float L1Slope, float *L2Point, float L2Slope)
{
///A temp array for holding our awnser
float temp[2] = { 0, 0 };
///Slope for our x value of the solution
temp[0] = (L1Slope * L1Point[0] - L2Slope * L2Point + L2Point[1] - L1Point[1]) / (L1Slope - L2Slope);
///Use our new found value to solve for our y value
temp[1] = L1Slope(temp[0] - L1Point[0]) + L1Point[1]);
return temp;
}
///purpose: to calculate the distance between two points
///input: P1 - an array of two floats representing point 1
/// P2 - an array of two floats representing point 2
///output: the distance between two points
float distance2D(float *P1, float *P2)
{
///Calculate our distance and return it
return (float)sqrt(pow(P2[0] - P1[0], 2) + pow(P2[1] - P1[1], 2);
}
///purpose: calculate the midpoint of a line segment
///input: P1 - an array of two floats representing point 1
/// P2 - an array of two floats representing point 2
///output: the midpoint between the twopoints
float *find2MidPoint(float *P1, float *P2)
{
///Allocate enough memory to our pointer
float *temp = new float[2];
///Calculate our midpoint
temp[0] = (P1[0] + P2[0]) / 2.0f;
temp[1] = (P1[1] + P2[1]) / 2.0f;
///Return our awnser
return temp;
}
///purpose: calculate the midpoint of a line segment in 3d
///input: P1 - an aray of three floats representing point 1
/// P2 - an array of three floats representing point 2
///output: the midpoint between the two points
float *find3DMidPoint(float *P1, float *P2)
{
///Allocate enough memory to our pointer
float *temp = new float[3];
///Calcutate our midpoint
temp[0] = (P1[0] + P2[0]) / 2.0f;
temp[1] = (P1[1] + P2[1]) / 2.0f;
temp[2] = (P1[2] + P2[2]) / 2.0f;
///Return our answer
return temp;
}
struct circle
{
float center[2];
float radius;
};
struct sphere
{
float center[3];
float radius;
};
///purpose: to detect a collision between two spheres
///input: S1 - our first sphere, passed by reference
/// S2 - our second sphere, passed by reference
///output: true if there is a collision, else false
bool ColBetweenSpheres(sphere &S1, sphere &S2)
{
return( pow(S2.center[0] - S1.center[0], 2) +
pow(S2.center[1] - S1.center[1], 2) +
pow(S2.center[2] - S1.center[2], 2) <
pow(S1.radius + S2.radius, 2));
}
////////////////////////////////////////////////////////////
///checkBallCollision : Check for a collision with
///another ball
///
///In : ball2 - the ball to check collision with
///
///Return : True if there was a collision with another ball
////////////////////////////////////////////////////////////
bool Ball::checkBallCollision(ball &ball2)
{
///The radius of the balls
int radius1 = diameter / 2;
int radius2 = ball2.diameter / 2;
///The center point of the first ball
POINT center1;
center1.x = radius1 + bounds.left;
center1.y = radius1 + bounds.top;
///The center point of the second ball
POINT center2;
center2.x = radius2 + ball2.bounds.left;
center2.y = radius2 + ball2.bounds.top;
///The distance between the two balls' center
double distance = sqrt(SQUARE(center2.x - center1.x) +
SQUARE(center2.y - center1.y));
///See if thay have collided
if ( distance <= radius1 + radius2)
return true;
return false;
}
///This will hold our value
float sin_table[360];
///Fill in our table
for (int i = 0 ; i < 360 ; ++i)
{
///Remember our DegToRad const from before, PI / 180
sin_table[i] = sin(i * DegToRad);
}
///Calculate the sine of any angle
float value = sin_table(abs((int)angle) % 360);
#pragma intrinsic(sin, cos, tan);
///purpose: to calculate the angle between 2 objects in
///2d space
///input: P1 - the location of the first object
/// P2 - the location of the second object
///output: the angle between the object in degrees
float clacAngle2D(float *P1, float *P2)
{
///Calculate our angle
float ang = (float)atan((P2[1] - P1[1]) / (P2[0] - P1[0])) *
RadToDeg;
///In the event that the angle is in the first quadrant
if(P2[1] < P1[1] && P2[0] > P1[0])
return ang;
///In the event of second or third quadrant
else if ( (P2[1] < P[1] -66 P2[0] < P1[0]) ||
(P2[1] < P1[1] && P2[0] < P1[0]))
return ang + 180;
///If none of the above, it must be in the fourth
else
return ang + 360;
}
///An array of three floats is one way to store a vector,
///i j and k
float 3Dvector[3] = { 0, 0, 0 };
///A user-defined data type is another
struct 3Dvector
{
float x, y, z;
};
///A structure for holding a vector in component form
struct 2Dvector_comp
{ float x, y;
};
///A structure for holding a vector in
///magnitude/direction form
{
float mag, dir;
};
///purpose: to convert a vector from magnitude/direction
///to component form
///input: vec- a vector in magnitude/direction form
///output: our converted vector
2Dvector_comp_PolarToCompConversion(2Dvector_polar vec)
{
///A temporary variable which will hold our answer
2Dvector_comp temp;
///Fill in our values
temp.x = mag * cos(dir * PI / 180);
temp.y = mag * sin(dir * PI / 180);
///Return our answer
return temp;
}
///purpose: to convert a vector from component
///to magnitude/direction form
///input: vec- a vector in component form
///output: our converted vector
2Dvector_polar_CompToPolarConvertion(2Dvector_comp vec)
{
///A temporary variable which will hold our answer
2Dvector_polar temp = { 0, 0 };
///Calculate our magnitude using the Pythagorean theorom
temp.mag = sqrtf(vec.x * vec.x + vec.y * vec.y);
///Error check to prevent a divide-by-zero issue in
///our next section
if(temp.mag == 0)
return temp;
///Caculate our angle. We are using
///asin() which will return an angle
///in either the 1st or the 4th quadrant
if(vec.x < 0)
temp.dir += 180;
///Adjust our angle in the event that
///it lies in the 4th quadrant
else if(vec.x > 0 && vec.y < 0)
temp.dir += 360;
///Return our new vector
return temp;
}
bool areMatricesEqual(Matrix3X3 a, Matrix3X3 b)
{
int errorFlag = 0;
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 3; j++)
{
if((a.index[i] [j]) != (b.index[i] [j]))
errorFlag = 1; }
}
}
///Test for error in equality
if ( errorFlag = 1 )
return false;
else
return true;
}
Matrix3X3 addMatrices(Matrix3X3 a, Matrix3X3 b)
{
Matrix3X3 temp;
for ( int i; i < 3; i++)
{
for ( int j = 0; j < 3; j++)
{
temp.index[i] [j] = (a.index[i] [j] + b.index[i] [j]);
}
}
return temp;
}
//Kode mangler
Det er c++ kode, men burte den ikke virke i c#???