Home > Net >  How to use the proper cross product?
How to use the proper cross product?

Time:10-22

So a cross product is v1 = <x1, y1, z1> and v2 = <x2, y2, z2> defined as v1v2 = <y1z2 - z1y2, z1x2 - x1z2, x1y2 - y1*x2> any a output of 32.000000. But I don't know if my code is wrong or wrong it the wrong way. But I'm not getting an weird text. Can anyone help me find the problem in the code?

void cross_product( float x1, float y1, float z1,
        float x2, float y2, float z2,
        float* result_x, float* result_y, float* result_z){
        float d = ((y1*z2)-(z1*y2), (z1*x2)-(x1*z2), (x1*y2) - (y1*x2));
        printf("v1 (dot) v2 = %f", d);

enter image description here

CodePudding user response:

As said by kaylum, you need to store the result for each vector in one of your variables return_ called by reference.

If you want to calculate de distance between your two points in float d, you have to make the square root of the sum of the powers of two of the distance between each coordinate.

At the end, your code should look like this:

void cross_product( float x1, float y1, float z1,
                    float x2, float y2, float z2,
                    float* result_x, float* result_y, float* result_z)
{
    *result_x = (y1 * z2) - (z1 * y2);
    *result_y = (z1 * x2) - (x1 * z2);
    *result_z = (x1 * y2) - (y1 * x2);

    float d = sqrtf(powf(x2 - x1, 2)   powf(y2 - y1, 2)   powf(z2 - z1, 2));

    printf("v1 (dot) v2 = %f", d);
}

CodePudding user response:

The comma operator evaluates each sub-expression and the final result is the last sub-expression. So in your case it means d only ends up with the result of (x1*y2) - (y1*x2).

For the cross product you need to treat each component result seperately. That is why the API has the output result variables. Store the result into those variables:

*result_x = (y1 * z2) - (z1 * y2);
*result_y = (z1 * x2) - (x1 * z2);
*result_z = (x1 * y2) - (y1 * x2);
  • Related