-3

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

  • Please do not post code and text output as an image - [reasoning](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Copy it as formatted text into the question. Also, please ensure the posted code is a complete [mre]. That is, include the test code with input values as well. – kaylum Oct 21 '21 at 23:53
  • 5
    This is similar to a previous (now deleted) question. The same comment applies: You can't just blindly copy the formula from the task description. `,` in a maths equation is not the same as `,` in C code. In the code you need to store each component of the cross products into the `result_` output variables. That is, you need 3 seperate variables. In C the comma operator will evaluate to the last expression only (`(x1*y2) - (y1*x2)`) which is clearly not what you need. – kaylum Oct 21 '21 at 23:56
  • Could you show an example of the result_ ? – student1847 Oct 22 '21 at 00:18
  • Your `printf` statement suggests that you have reused some code for a dot product rather than a cross product. The result of a cross product cannot be stored in a single variable `d` as you have attempted to do. – DavidHoadley Oct 22 '21 at 00:23

2 Answers2

3

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);
}
Riflender
  • 43
  • 5
  • Good answer. Aside: Recommend using `"%g"` to print _floating point_ than `"%f"`. More informative when values are small and less clutter when values are large. – chux - Reinstate Monica Oct 22 '21 at 05:45
2

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);
kaylum
  • 13,833
  • 2
  • 22
  • 31