Reputation: 1627
I was reading Hacker News and this article came up. It contains a raytracer that the code is written on the back of a business card. I decided it would be a good academic challenge to translate the c++ to python, but there's a few concepts I'm stuck on.
First, this function comes up: i T(v o,v d,f& t,v& n){...}
Which is translated to int Tracer(vector o, vector d, float& t, vector& n){...}
What does the float&
mean? I know that in other places &
is used as a ==
is that the case here? Can you do that in c++?
Second, I noticed these three lines:
for(i k=19;k--;) //For each columns of objects
for(i j=9;j--;) //For each line on that columns
if(G[j]&1<<k){
I know the <<
is a the bit shift, and I assume the &
is ==
. Are the for loops just like one for loop in an other?
Finally, this line: v p(13,13,13);
I am not quite sure what it does. Does it create a class labeled by p that extends v (vector) with the defaults of 13,13,13?
These are probably dumb questions, but I want to see if I can understand this and my searching didn't come up with anything. Thank you in advance!
Upvotes: 0
Views: 246
Reputation: 254461
What does the
float&
mean?
Here, &
means "reference", so the argument is passed by reference.
I know that in other places
&
is used as a==
is that the case here?
&
means various things in various contexts, but it never means ==
. In this case, it's not an operator either; it's part of a type specification, meaning that it's a reference type.
I know the << is a the bit shift, and I assume the
&
is==
No, it's a bitwise and operator. The result has its bits set where a bit is set in both operands. Here, with 1<<k
as one operand, the result is the kth bit of G[j]
; so this tests whether that bit is set.
Are the for loops just like one for loop in an other?
Yes. If you don't use braces around a for-loop's body, then the body is a single statement. So in this case, the body of the first loop is the second loop. To make this clear, I would recommend indenting the body of the loop, and using braces whether or not they are strictly necessary. But of course, I don't write (deliberately) obfuscated code.
Finally, this line:
v p(13,13,13);
v
is a class with a constructor taking three arguments. This declares an variable called p
, of type v
, initialised using that constructor; i.e. the three co-ordinates are initialised to 13
.
Upvotes: 3
Reputation: 300
When you seeVector& n
it is referencing the vector passed into the function. This means that you can change n
inside of this function without having to copy it to another Vector or without returning the Vector. This previous answer should be helpful to you.
Upvotes: 1