-1

In Ken Perlin's improvement on his own Perlin Noise formula he has a certain grad function where he calculates the pseudorandom gradient

can anyone tell me what the syntax he's using actually means? I'm not familiar with it. This is the grad function he wrote:

static double grad(int hash, double x, double y, double z)
{
   int h = hash & 15;                      // CONVERT LO 4 BITS OF HASH CODE
   double u = h<8 ? x : y,                 // INTO 12 GRADIENT DIRECTIONS.
          v = h<4 ? y : h==12||h==14 ? x : z;
   return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
nachtwezen
  • 123
  • 9
  • Which specific lines are you talking about? Paste them here. He's using Java syntax, by the way. – nanofarad Nov 20 '13 at 22:39
  • I know it's java thank you, I just don't know the ?, & operator all that well and I am puzzled with what the v assignment en return really do. – nachtwezen Nov 20 '13 at 22:43
  • If you know it's Java, then you ought to be able to find out what those operators do faster than asking this question. – Ross Patterson Nov 20 '13 at 23:02

1 Answers1

2

There are some things you will have to do some research on:

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • thanks, I think I understand it now, never saw/used the ? operator. – nachtwezen Nov 20 '13 at 22:47
  • 1
    @SamGielis - `?` is also referred to as the [*ternary* operator](http://en.wikipedia.org/wiki/%3F:), in case you wanted to do more searching on it. – admdrew Nov 20 '13 at 22:50
  • @SamGielis: I had this discussion already before with Jon Skeet. He was right: The conditional operator is a ternary operator. Currently the conditional operator is also the only ternary operator in Java. Think of this: unary operator, binary operator, ternary operator. As soon as Java features a new ternary operator, all statements about ternary operators would be wrong because everybody meant referring to the conditional operator. It is like calling `+` (addition) the binary operator. – Martijn Courteaux Nov 21 '13 at 06:56