1

I want to calculate the cross product of x and vector y without numpy or any imports.

x = Vector(1,2,1,0)  
y = Vector(0,1,2,1)

but my result is always wrong. what am i missing here?

def crossproduct(x, y):
        final = Vector()
        final.v[0] = y.v[1] * v.v[2] - x.v[2] * y.v[1]
        final.v[1] = y.v[2] * v.v[3] - x.v[3] * y.v[2]
        final.v[2] = y.v[3] * v.v[0] - x.v[0] * y.v[3] 
        final.v[3] = y.v[0] * v.v[1] - x.v[1] * y.v[0]
        return final
Spektre
  • 49,595
  • 11
  • 110
  • 380
Yefkin
  • 13
  • 2
  • Please share x y and the expected results – Leo Arad May 09 '20 at 12:01
  • 1
    Strange, there's no binary cross product in 4d. – Stéphane Laurent May 09 '20 at 12:45
  • Yep exactly 4D cross product has 3 operands not 2 !!! so its either 3D corss product with vectors in homogenuous coordinates (but then the w would be `w=0` ) or 4D operation but not cross product ... Its possible to obtain perpendicular vector to 2 vectors in 4D but there are infinite number of them ... You should clarify what exactly is your case... also title is 4D and tags are 3D ? – Spektre May 10 '20 at 09:22
  • 1
    your right it was at the end a 3D cross product with vectors and i had to finally set my w=0 is it possible to change to title? – Yefkin May 12 '20 at 23:10
  • @Yefkin Yes you can edit the whole stuff (just click on the edit on the left below your question) I already eddited the stuff ... and voted for reopen – Spektre May 13 '20 at 07:50
  • thanks a lot Spektre. Have to learn a lot on this page :) – Yefkin May 13 '20 at 12:27

1 Answers1

1

Solving cross on a Vec4 (homogenuous) is the same as solving it as a Vec3 (Cartesian), since you're in 3 dimensions, no matter how you use the w component.

Check your cross formula, the one I usually use looks like this :

crossX = vector1.Y * vector2.Z - vector2.Y * vector1.Z
crossY = -(vector1.X * vector2.Z - vector2.X * vector1.Z)
crossZ = vector1.X * vector2.Y - vector2.X * vector1.Y
crossW = 0.0
Spektre
  • 49,595
  • 11
  • 110
  • 380
G.Vernier
  • 369
  • 2
  • 14
  • Thanks a lot. That was the missing clue! – Yefkin May 09 '20 at 12:42
  • https://www.jstor.org/stable/2323537?seq=1 This paper demonstrates the non-existence of cross-product in euclidian spaces with dimensions other than 3 and 7 (to keep orthogonality property). This question was tagged '3D' so the use of Vec4 is surely to store additional component for weighted values or else. – G.Vernier May 10 '20 at 18:26
  • @G.Vernier question is confusing in more ways that is why its closed as OP author did not clarify. The only thing that hints 3D is the tag but title,text,data and equation hints 4D operation. Did not read the paper as the site need registration (and I am not willing to do so) however you can compute ND cross product the same way you can do 2D,3D,4D,5D,... using [determinant approach](https://stackoverflow.com/a/57953322/2521214). For example [here is implemented in form of C++ template for ND](https://stackoverflow.com/a/55439139/2521214) – Spektre May 11 '20 at 07:02
  • @G.Vernier btw there are other methods to get perpendicular vectors in ND like these [How to generate a randomly oriented, high dimension circle in python?](https://stackoverflow.com/q/42338910/2521214) how ever OP does not mention orthogonality at all .... – Spektre May 11 '20 at 07:04
  • @G.Vernier Yep but that does not make it correct ... imagine others would look for 4D cross product in future and find this QA ... without major changes in Question is your answer not valid (for the question's current state). I know its not your fault the OP author is silent for now so at least the down votes indicates a problem for future readers to stay alert. Anyway I am still not convinced this solves OPs problem even if (s)he claims it does as more things there hints 4D instead of 3D and the "correctness" might be just false positive. – Spektre May 12 '20 at 21:16