/// <summary> /// This takes in two rays (a point and a direction) and finds the point of intersection between /// the two, if there is an intersection. The method uses Cramer's Rule to solve the system of /// linear equations to find the unknown (x, y) value. The operation doesn't use Unity's Ray2d /// class because the direction property normalizes the vector which throws off the calculation /// due to floating point error. /// </summary> /// <param name="intersection">The point where the two rays intersect</param> /// <param name="position1">The origin position for the first ray.</param> /// <param name="direction1">The direction of the first ray.</param> /// <param name="position2">The origin position for the second ray.</param> /// <param name="direction2">The direction of the second ray.</param> /// <returns>If the two rays intersect</returns> public static bool RayRayIntersection(out Vector2 intersection, Vector2 position1, Vector2 direction1, Vector2 position2, Vector2 direction2) { const int VECTOR_SIZE = 2; var coefficient = new Matrix2x2(); var answers = new Vector2(); intersection = new Vector2(); coefficient[0, 0] = direction1.x; coefficient[0, 1] = -direction2.x; coefficient[1, 0] = direction1.y; coefficient[1, 1] = -direction2.y; answers[0] = position2.x - position1.x; answers[1] = position2.y - position1.y; float det = coefficient.determinant; if (det == 0) { return(false); } for (int col = 0; col < VECTOR_SIZE; col++) { Vector2 originalColumn = coefficient.GetCol(col); coefficient.SetCol(col, answers); intersection[col] = coefficient.determinant / det; coefficient.SetCol(col, originalColumn); } return(true); }
/// <summary> /// This does an element wise comparison on both matrices /// to determine if they are equal to each other. /// </summary> /// <param name="obj">The matrix to compare.</param> /// <returns>If the two matricies have the same elements.</returns> public override bool Equals(object obj) { if (!(obj is Matrix2x2)) { return(false); } Matrix2x2 mat = (Matrix2x2)obj; return(mat == this); }
/// <summary> /// This will add two 2x2 matricies together. /// </summary> /// <param name="mat1">2x2 Matrix</param> /// <param name="mat2">2x2 Matrix</param> /// <returns>The summed matrix.</returns> public static Matrix2x2 operator +(Matrix2x2 mat1, Matrix2x2 mat2) { var matrix = new Matrix2x2(); for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { matrix[row, col] = mat1[row, col] + mat2[row, col]; } } return(matrix); }
/// <summary> /// This will multiply the matrix by a scalar which is an element-wise /// multiplication. /// </summary> /// <param name="matrix">The 2x2 matrix.</param> /// <param name="scalar">The scalar to multiply each element by.</param> /// <returns>The scaled matrix.</returns> public static Matrix2x2 operator *(Matrix2x2 matrix, float scalar) { var scaledMatrix = new Matrix2x2(); for (int row = 0; row < MATRIX_SIZE; row++) { for (int col = 0; col < MATRIX_SIZE; col++) { scaledMatrix[row, col] = matrix[row, col] * scalar; } } return(scaledMatrix); }