Index: src/OFFloatVector.h ================================================================== --- src/OFFloatVector.h +++ src/OFFloatVector.h @@ -98,14 +98,26 @@ /** * \brief Returns an array of floats with the contents of the vector. * * Modifying the returned array directly is allowed and will change the vector. * - * \brief An array of floats with the contents of the vector + * \return An array of floats with the contents of the vector */ - (float*)cArray; +/** + * \brief Returns the magnitude or length of the vector. + * + * \return The magnitude or length of the vector + */ +- (float)magnitude; + +/** + * \brief Normalizes the vector. + */ +- (void)normalize; + /** * \brief Adds the specified vector to the receiver. * * \param vector The vector to add */ @@ -154,24 +166,21 @@ * \return The dot product of the receiver and the specified vector */ - (float)dotProductWithVector: (OFFloatVector*)vector; /** - * \brief Returns the magnitude or length of the vector. - * - * \return The magnitude or length of the vector - */ -- (float)magnitude; - -/** - * \brief Normalizes the vector. - */ -- (void)normalize; + * \brief Returns the cross product of the receiver and the specified vector. + * + * This currently only works for 3D vectors. + * + * \return The cross product of the receiver and the specified vector + */ +- (OFFloatVector*)crossProductWithVector: (OFFloatVector*)vector; /** * \brief Multiplies the receiver with the specified matrix on the left side and * the receiver on the right side. * * \param matrix The matrix to multiply the receiver with */ - (void)multiplyWithMatrix: (OFFloatMatrix*)matrix; @end Index: src/OFFloatVector.m ================================================================== --- src/OFFloatVector.m +++ src/OFFloatVector.m @@ -236,10 +236,41 @@ - (float*)cArray { return data; } + +- (float)magnitude +{ + float magnitude; + size_t i; + + magnitude = 0.0f; + + for (i = 0; i < dimension; i++) + magnitude += data[i] * data[i]; + + magnitude = sqrtf(magnitude); + + return magnitude; +} + +- (void)normalize +{ + float magnitude; + size_t i; + + magnitude = 0.0f; + + for (i = 0; i < dimension; i++) + magnitude += data[i] * data[i]; + + magnitude = sqrtf(magnitude); + + for (i = 0; i < dimension; i++) + data[i] /= magnitude; +} - (void)addVector: (OFFloatVector*)vector { size_t i; @@ -318,34 +349,32 @@ dotProduct += data[i] * vector->data[i]; return dotProduct; } -- (float)magnitude -{ - float magnitude; - size_t i; - - magnitude = 0.0f; - - for (i = 0; i < dimension; i++) - magnitude += data[i] * data[i]; - - magnitude = sqrtf(magnitude); - - return magnitude; -} - -- (void)normalize -{ - float magnitude; - size_t i; - - magnitude = [self magnitude]; - - for (i = 0; i < dimension; i++) - data[i] /= magnitude; +- (OFFloatVector*)crossProductWithVector: (OFFloatVector*)vector +{ + OFFloatVector *crossProduct; + + if (dimension != 3) + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; + + if (vector->dimension != dimension) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + crossProduct = [OFFloatVector vectorWithDimension: 3]; + + crossProduct->data[0] = + data[1] * vector->data[2] - data[2] * vector->data[1]; + crossProduct->data[1] = + data[2] * vector->data[0] - data[0] * vector->data[2]; + crossProduct->data[2] = + data[0] * vector->data[1] - data[1] * vector->data[0]; + + return crossProduct; } - (void)multiplyWithMatrix: (OFFloatMatrix*)matrix { float *newData;