# The Ray Tracer Challenge: Page 37 4x4 cofactor?

**URL:** <https://forum.devtalk.com/t/the-ray-tracer-challenge-page-37-4x4-cofactor/41433>\
**Category:** PragProg Customers\
**Tags:** book-the-ray-tracer-challenge, question\
**Created:** [19 February 2022 09:29 UTC](https://forum.devtalk.com/t/the-ray-tracer-challenge-page-37-4x4-cofactor/41433 "2022-02-19T09:29:29Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![FPChris](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fpchris/32/3894_2.png) [@FPChris](https://forum.devtalk.com/u/FPChris)\
**Post date:** [19 February 2022 20:18 UTC](https://forum.devtalk.com/t/the-ray-tracer-challenge-page-37-4x4-cofactor/41433/2 "2022-02-19T20:18:26Z")

</div>

I finally resolved this. The book is really unclear. Terrible in fact.  
If anyone needs hep on this. Here is my solution below.  
This YT video was a great help [Finding the determinant of a 3x3 matrix method 2 | Matrices | Precalculus | Khan Academy - YouTube](https://www.youtube.com/watch?v=u00I3MCrspU)

//-----------------------------------------------------------------------------------  
function RCCalcCofactor4x4(A,row,col){

let B = RCGetSubmatrix4x3(A,row,col); //4x4 \> 3x3  
let s,a;  
s = RCGetSubmatrix3x2(B,0,0); //3x3 to 2x2  
a = B[0][0] \* RCCalcDeterminant2x2(s);  
s = RCGetSubmatrix3x2(B,0,1);  
a += -B[0][1] \* RCCalcDeterminant2x2(s); //Note: -B  
s = RCGetSubmatrix3x2(B,0,2);  
a += B[0][2] \* RCCalcDeterminant2x2(s);

if(isOdd(row + col)){ return -a; } //odd so negate  
return a; //even - leave alone. just return it

}  
//-----------------------------------------------------------------------------------  
function isOdd(num) { return num % 2;}  
//-----------------------------------------------------------------------------------  
function RCCalcDeterminant4x4(A){  
// Ch 3 - page 37  
/\*  
A[0][0] = -2; A[0][1] = -8; A[0][2] = 3; A[0][3] = 5;  
A[1][0] = -3; A[1][1] = 1; A[1][2] = 7; A[1][3] = 3;  
A[2][0] = 1; A[2][1] = 2; A[2][2] = -9; A[2][3] = 6;  
A[3][0] = -6; A[3][1] = 7; A[3][2] = 7; A[3][3] = -9;  
\*/

let d0 = RCCalcCofactor4x4(A,0,0); //690  
let d1 = RCCalcCofactor4x4(A,0,1); //447  
let d2 = RCCalcCofactor4x4(A,0,2); //210  
let d3 = RCCalcCofactor4x4(A,0,3); //51

let d = (A[0][0] \* d0) + (A[0][1] \* d1) + (A[0][2] \* d2) + (A[0][3] \* d3); //-4071

return d;

}

---

_[View the full topic](https://forum.devtalk.com/t/the-ray-tracer-challenge-page-37-4x4-cofactor/41433)._
