($square/2) == floor($square/2) ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

($square/2) == floor($square/2) ???

Post by gth759k »

I'm converting a simple raytracer I made in php over to c++ and I can't figure out how to convert a certain part. The code below creates a "tile/checkered" pattern that can be put on a plane, I didn't make it up myself, I copied it from a javascript raytracer and just modified the function calls to make it work, so the javascript version of this code was very similar. Anyways, what is the == doing? In c++ it doesn't work at all and when I run it with just setColorRed((square/2)) it doesn't make the correct pattern of every other square being black or white. Any suggestions?

Code: Select all

$square = floor($intersection_position->getVectorX()) + floor($intersection_position->getVectorZ());

$winning_object_color->setColorRed(($square/2) == floor($square/2));
$winning_object_color->setColorGreen(($square/2) == floor($square/2));
$winning_object_color->setColorBlue(($square/2) == floor($square/2));
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: ($square/2) == floor($square/2) ???

Post by Christopher »

"($square/2) == floor($square/2)" is going to evaluate to true/false (or 1/0 perhaps in this case). It will be true if $square is even and false if it is odd. So to make the squares it is passing RGB 1,1,1 or 0,0,0.
(#10850)
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Re: ($square/2) == floor($square/2) ???

Post by gth759k »

Thanks so much! I got it working now!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: ($square/2) == floor($square/2) ???

Post by VladSun »

LOL, that's a bad code :)

Code: Select all

if (!($square % 2)) ...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply