-2

I not found any solution for my problem so i ask, how ? and : operators works when i have multiple statemants?

What i want to do, i have pixel on the middle pixel[pos] and pixels around, its look like:

0 0 0
0 x 0
0 0 0

x is a center pixel.

Im checking, if i have any white (zero) pixel around it. If is anyone, i marked pixel as two. If not, so pattern looks like:

1 1 1
1 x 1
1 1 1

1 is an black pixel, i set it to one.

Now, the code:

if(pixels[positionOfPixel] == one && x > 0 && x < width 
                                  && y > 0 && y < height)
{
    pixels[positionOfPixel] = pixels[positionOfPixel - 1]          == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel + 1]          == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset]     == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel + offset]     == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset + 1] == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel + offset - 1] == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset - 1] == zero ? two :
    pixels[positionOfPixel] = pixels[positionOfPixel - offset + 1] == zero ? two : zero;
}

My question is, why every one pixel is marked as two ? Why it didnt recognize pixel, where every pixel arond is one (like in second pattern)?

Thanks for any advices!

  • what's your really question? – D-Shih Aug 26 '18 at 19:03
  • There is only the [`?:` operator; not two](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator).. – user2864740 Aug 26 '18 at 19:05
  • `a?b:c` is 1 operator with 3 operands. – H H Aug 26 '18 at 19:05
  • 2
    And the guideline is to use it sparingly and not to nest it. You are nesting it 8 times, with embedded assignments for extra unreadability. This is not a good way to code. – H H Aug 26 '18 at 19:07
  • 5
    Is ternary the right choice here? You've obfuscated the code from even yourself. I don't think your implementation would suffer much with traditional `if` statements – Parrish Husband Aug 26 '18 at 19:07
  • I cant find solution on Microsoft Documentation, how to write operator like mine. From code above, i always get `positive` result. `pixels[position...]` is pixel value. If it have any zeros around, i marked it as `two`. If it dont have any zeros around (so it should be `negative` , on the "right" side of operator), i marked it as `one`. But its alweys get marked as `two`. Myq uestion is, how to write it properly, that statemant with multiple operators? –  Aug 26 '18 at 19:09
  • I just want to check every operator in sequence –  Aug 26 '18 at 19:10
  • Yes, i i only change value of `pixel[positionOfPixel]`, the rest of pixels like `pixel[positionOfPixel + stride]` etc are unchanged –  Aug 26 '18 at 19:14
  • @ParrishHusband but i think its a good to know exactly how more complicated ternary work. And traditional if statements will looks pretty bad here, because i will need to add a couple of it –  Aug 26 '18 at 19:16
  • @metadon789 complexity doesn't change how the operator functions: `condition ? trueResult : falseResult`. Chaining additional checks on the false result doesn't fundamentally change how it works. I've rarely seen a ternary nested over two where the code benefited as a result. – Parrish Husband Aug 26 '18 at 19:20
  • @ParrishHusband to be honest, statemenets need to be checking in sequence, something like `if -> else if -> else if` etc. I cant write it (every line) in `positive : negative` because it wont work –  Aug 26 '18 at 19:25
  • Okey, sorry, i found a solution. The main problem was an `parallel.for` that i used with that `if` statement from my original post. Thats was the main issue –  Aug 26 '18 at 19:30

1 Answers1

1

I'm not a C# specialist, but there is common rule how ? : operator can be used.

x = (boolean condition) ? reult_if_true : result_if_false;

E.g.

drink = isThisPersonAGirl ? wine : beer;

If you want to use many conditions with ? : operator, you should do it that way:

x = (boolean condition 1) ? result_if_true : (boolean condition 2) ? result_if_bool_2_is_true : result_if false;

E.g.

drink = isThisPersonAChild ? lemonade : isThisPersonAGitl ? wine : beer

In your code snippet, it's hard to understand what's going on because you use = operator too often. In most languages, you could initalize several variables like this:

a = b = c = 0, so a, b, c will be = 0;

So I think your mistake is using = operator too often so maybe only this condition does matter, while others are simply skipped:

pixels[positionOfPixel - offset + 1] == zero ? two : zero;

Sorry of it doesn't help, since I'm really not a C# coder)

shaurun
  • 70
  • 6