# Processing is my Whetstone

**URL:** <https://discourse.processing.org/t/processing-is-my-whetstone/3885>\
**Category:** Coding Questions\
**Created:** [September 27, 2018, 4:26am UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885 "2018-09-27T04:26:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [September 27, 2018, 4:26am UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/1 "2018-09-27T04:26:35Z")

</div>

I’m not the sharpest tool in the shed, but I try to exercise what I got and Processing has been my whetstone.

I recently tried out some vector math, cross product, dot product, etc. with good results after some typos slowed me up. This forum proved helpful again, thanks. What I want to do now is apply something I saw after searching “Inverse of a cross product”. The majority of answers were NO, except for One. A professor from the Polytechnica of Equador gave an answer that was very interesting and explained how it is possible using four-vectors. Here’s his answer:

[Diego Saa’s Answer](https://www.quora.com/Whats-the-inverse-of-the-cross-product)

What I want to do is solve this problem and use Processing to show it. I figure I should start by implementing his answer and proving his numbers, but I cant decipher the notation. It looks simple but…

I already have some functions for dot product, cross product and I think Processing has these too.

Suppose C = A X B. If I already have B and C, I want to derive A. Apparently, this is the holy grail of vector algebra and Diego Saa makes it sound simple.

I still don’t know where to start. The first question I have about the notation is he says A = [a,a] = {0,3,4,7}  
with a = (3.4.7). So, that means the other a = 0?

He then says, B = {0,-2,2,-3} , so that would be B = [b,b} ?

Then he shows how to multiply,

A∗B=[a,a]∗[b,b]=[ab+a⋅b,ab−ba+a×b]  
If the unbold a and unbold b are zeros, then wouldn’t that be the dot product of a and b is the first element and the cross product of a and b is the other 3 elements?

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [September 27, 2018, 11:56am UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/2 "2018-09-27T11:56:39Z")

</div>

Well, Processing is oriented towards visual solutions and this is not much to look at being just numbers.

I duplicated his numbers though. One thing that makes me wonder is there a way to round a number. I used (int) and it truncated one of the numbers that was essentially correct and it looks wrong by 1 now.

Here’s the butchery:

```auto
double A[] = {0,3,4,7}; //new double[4];
double B[] = {0,-2,2,-3}; //new double[4];
double C[] = new double[4]; //{-19,-26,-5,14}; //new double[4];

double a[] = {3,4,7};
double b[] = {-2,2,-3};
double c[] = {-26,-5,14};

double D[] = new double[4];

// void setup() {
// size(127, 75, OPENGL);
     
// }
   
   void draw() {
  

    
   C = Mult(A,B,a,b);
   
   println((int)C[0],(int)C[1],(int)C[2],(int)C[3]);

   
println(Mod(a));

D = InvConj(A,a);
double d[] = new double[3];

d[0] = D[1];
d[1] = D[2];
d[2] = D[3];

B = Mult(D,C,d,c);

println((int)B[0],(int)B[1],(int)B[2],(int)B[3]);
   

    
    
     
     
   }
   
   double[] InvConj(double A[], double a[]){
     
     double res[] = new double[4];
 res[0] = 1/(Mod(a) * -1) * 0;
 res[1] = (1/Mod(a) * -1) * a[0];
 res[2] = (1/Mod(a) * -1) * a[1];
 res[3] = (1/Mod(a) * -1) * a[2];
 
// println(res[0],res[1],res[2],res[3]);
 
 
     return res;
   }
   
   double[] Mult(double A[], double B[], double a[], double b[]){
     
    double ab[] = new double[3];

  double acrossb[] = new double[3];

    double ba[] = new double[3];
    double AB[] = new double[4];
   
    ab = aVector(A[0],b);
    ba = aVector(B[0],a);
    
   double adotb = Dot(a,b);
    acrossb = Cross(a,b);
    
    double I = Dot(a,b);
    double J[] = Subtract(ab,ba);
    double K[] = Add(J, acrossb);
    AB[0] = I;
    AB[1] = K[0];
    AB[2] = K[1];
    AB[3] = K[2];
    
    return AB;
     
     
   }
   
   double Mod(double x[]) {

    return((x[0] * x[0] + x[1] * x[1] + x[2] * x[2]));
   
   }
   
  double[] Add(double x[], double y[]) {
  double z[] = new double[3];
  for (int i = 0; i < 3; i++) {
       z[i] = x[i] + y[i];
       
      } 
    return z;
  }
  

      
  double[] Subtract(double x[], double y[]) {
  double z[] = new double[3];
  for (int i = 0; i < 3; i++) {
       z[i] = x[i] - y[i];
      } 
      return z;
  }
     
 double[] aVector(double a, double x[]) {
   double z[] = new double[3];
   z[0] = a * x[0];
   z[1] = a * x[1];
   z[2] = a * x[2];
  // z[3] = a * x[3];
   
   return (z);
   
 }
 

 
  double Dot(double x[], double y[])
 {
   return (x[0]*y[0]+x[1]*y[1]+x[2]*y[2]);
   
 }
 
 double[] Cross(double x[], double y[]){
  
   double z[] = new double[3];
   z[0] = (x[1]*y[2]) - (x[2]*y[1]);
   z[1] = (x[2]*y[0]) - (x[0]*y[2]);
   z[2] = (x[0]*y[1]) - (x[1]*y[0]);
   
   return (z);
   
 }

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 27, 2018, 9:57pm UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/3 "2018-09-27T21:57:16Z")

</div>

@HackinHarry – a tip: if you want to do a sketch that prints some computations only once, then you can put your code in setup rather than draw – or you can call `noLoop()` in setup or draw so that the calculations won’t run over and over.

Am I right that you are looking for a geometrically intuitive idea of an inverse cross product, as with visualizing arrows floating in a 3D space? I am assuming by inverse you do not mean the moment the example we see in this video of the red arrow changing direction.

[![](https://img.youtube.com/vi/h0NJK4mEIJU/maxresdefault.jpg "Cross Product and Dot Product: Visual explanation") ](https://www.youtube.com/watch?v=h0NJK4mEIJU)

> [@HackinHarry](#):
>
> Suppose C = A X B. If I already have B and C, I want to derive A. Apparently, this is the holy grail of vector algebra and Diego Saa makes it sound simple.

This is NOT an area of expertise for me, but I am skeptical that there is a major missing method in the field that is solved in a Quara post. There might be some misunderstanding that resolved partly revolves around disciplinary uses of the word “inverse”? For your second question, I think you want something like this?

#### Finding a Missing Component of a Cross Product Given One Vector and Unknown Vector

[![](https://i.ytimg.com/vi/KDdDp8CjQvU/hqdefault.jpg "Finding a Missing Component of a Cross Product Given One Vector and Unknown Vector") ](https://www.youtube.com/watch?v=KDdDp8CjQvU)

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [September 27, 2018, 11:16pm UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/4 "2018-09-27T23:16:02Z")

</div>

Yes, sir, thank you. I put noLoop() in there. I’m just a hair less sloppy than I was a few months ago when I dove into making a star globe and you gave me a link to “spherical trigonometry”.

What I was trying to solve is given C = A X B, B and C known, find A. The second video answers that. All that four-vector stuff worked, but is like using a sledge hammer when a feather duster will do.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 28, 2018, 12:01am UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/5 "2018-09-28T00:01:10Z")

</div>

If you are interested in getting deeper into playing with quaternions then you might want to play with PeasyCam and check out the PeasyCam source code. I believe it uses quaternions for its internal representation of orientation.

> <https://github.com/jdf/peasycam/blob/master/src/peasy/org/apache/commons/math/geometry/Rotation.java#L26-L34>

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [October 1, 2018, 5:55pm UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/6 "2018-10-01T17:55:25Z")

</div>

Yup, I’ve been playing with the source code for Peasy, actually the underlying Apache\math source. I found that has the functions I was using, so that wheel shall not be rewritten by me. I tested some and see there is some difference that I’m trying to resolve. I get complements and reverse signs, etc., so something is slightly awry.

Anyway, here’s something vaguely related that I tried today. On one of my favorite links, [Navigational Algorithms](https://sites.google.com/site/navigationalalgorithms/links) , there is a further link to : [http://www.yrvind.com/](http://www.yrvind.com/) Yrvind is a Swedish solo sailor who invented a fixed angle sextant he calls the [Bris Sextant](http://www.yrvind.com/bris-mini-sextant/). I made one rather than pay $200. After getting the welder’s shade no.12 cut by a local glass company, I cut the microscope slides without much trouble and then glued it all together with epoxy. I taped it to the camera of my tablet and took some shots to see what the heck I made.

Here they are:

 ![Sun11311001](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e58d396e99c6693890f0ff8477b0f343b25e6c84.jpeg)

About an hour later, I got something very different.

 ![Sun20181001_122832](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f59da51a09e3fe186ddd9edacf00e3e713de4e4a.jpeg)

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [October 4, 2018, 1:28am UTC](https://discourse.processing.org/t/processing-is-my-whetstone/3885/7 "2018-10-04T01:28:06Z")

</div>

It’s getting cleaner and simpler by using the Vector3D class from apache\math that’s used by peasycam.

```auto
double Z;
double alt;

/* double GHA = Math.toRadians(162.3366651);
 double dec = Math.toRadians(45.99798965);
 double Be = Math.toRadians(35.4000);
 double Le = Math.toRadians(26.452837); */

double GHA = Math.toRadians(166.829);
double dec = Math.toRadians(19.335);
double Be = Math.toRadians(42.98208);
double Le = Math.toRadians(89.56648); 

void setup() {
  size(127, 75, OPENGL);
}

void draw() {
  // AltitudeAzimuth( GHA, dec, Be, Le);

  Vector3D NP = new Vector3D(0, 0, 1);
  Vector3D U2 = new Vector3D();

  Vector3D GP = new Vector3D(GHA, dec);
  Vector3D AP = new Vector3D(Le, Be);

  Vector3D U1 = new Vector3D(); //(u1[0],u1[1],u1[2]);

  U1 = (U1.crossProduct(AP, NP));
  println("U1: ", U1.getX(), U1.getY(), U1.getZ());

  U1 = U1.normalize();
  println("U1: ", U1.getX(), U1.getY(), U1.getZ());

  U2 = (U2.crossProduct(U1, AP));
  // U2 = U2.negate();
  println("U2: ", U2.getX(), U2.getY(), U2.getZ());

  Vector3D U3 = new Vector3D();
  U3 = (U3.crossProduct(AP, GP));
  U3 = U3.normalize();

  println("U3: ", U3.getX(), U3.getY(), U3.getZ());

  Vector3D AZ = new Vector3D( U3.crossProduct(U3, AP).getX(), 
    U3.crossProduct(U3, AP).getY(), U3.crossProduct(U3, AP).getZ()) ;
  double Z = Math.toDegrees(AZ.angle(U2, AZ));

  if (U1.dotProduct(U1, AZ) < 0.0) {
    println(Math.toDegrees(U1.dotProduct(U1, AZ)));
    Z = 360.0 - Z;
  }

  println(Z);

  // alt = Math.toDegrees(U3.dotProduct(AP,GP));
  // println(alt);
  alt = 90.0 - Math.toDegrees(U1.angle(AP, GP));
  println(alt);

  noLoop();
}

```
