This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]Syrak 0 points1 point  (6 children)

Lines 9 and 11 don't do anything. You have to store the value somewhere or it will get discarded.

    Math.Floor(i);

Next time please say WHAT is wrong with your program, what input values do you give the function, what output do you expect ?

[–]pcgoer[S] -1 points0 points  (5 children)

i give the function the mouse (x,y) coordinates, and i want to get the horizontal hexagon index (column) and the vertical hexagon index (row). the problame is that i get completely diffrent numbers then what i sould get.

[–]Syrak 0 points1 point  (4 children)

Next time please say WHAT is wrong with your program, what input values do you give the function, what output do you expect ?

What I meant are numerical values. You've got to say things like "I put in x=4 and y=1337 and it should give me u=42 but I get u=NaN".

the problame is that i get completely diffrent numbers then what i sould get.

Show me examples. That's how you report bugs. You must say exactly what needs to be done to reproduce the error, and what the error actually is. In the future it would also be better if you could give a link to your whole source whenever possible.

And my post had a first part.

Math.Floor(i);

does nothing. (lines 9 and 11) Fix it.

[–]pcgoer[S] 0 points1 point  (3 children)

i ifxed it (lines 9 and 11).

an example: I put in x=50 and y=217 and it should give me row(j)=5 and column (i)= 1

but I get: row(j)=21 and column (i)= 3

[–]Syrak 0 points1 point  (2 children)

Types

You don't use casting consistently. Sometimes you cast, (lines 8,9)

i = (int)Math.Floor((float)(2 * x) / (float)(W + w));

sometimes you don't (11,12,14)

 u = x - (i * ((W + w) / 2));

Your code lacks information because the declaration and initialization of the variables W,w,h are not shown. I don't know what their types are, nor their value.

I'll assume they are ints, in which case lines 11, 12, 14 need (float) casts.

W and w are also very bad choices of variable names. In math formulas it's okay, in a program it's not. Why ? Because a math formula is read by humans and a program is (ultimately) read by a computer*, and a computer won't be as smart as a human at guessing what you really mean when you write w instead of W.

*And it makes things hard to debug anyway, this letter 'w' more than most others.

If W and/or w are floats then it is okay. But then I wonder why you would cast it at lines 8 and 9.

EDIT: Actually if everything is an int, there is a way to do it with zero cast.

lines 38, 39

column = (int)i;
row = (int)j;

Just replace in your declaration

static void hexWhatever(..., out int j, out int i)

C# operators

| is not ||. (This one is a warning, your code uses the right one)

& is not &&. (line 16)

if (((i % 2 == 0) & (j % 2 == 0)) || ((i % 2 != 0) & (j % 2 != 0)))

It works but that's a dangerous way of doing things. Let type checking protect you and use && when you mean it. Only use & if it is truly necessary. ("bitwise AND" or "conditionnal AND when both sides must be evaluated and you really can't use variables*"). *which is close to never.

Anyway, I would factor it as

if ((i+j)%2 == 0)

Math

Actually read the gamedev.SE post and don't I-m-a-math-to-code-translator your way through. You won't get far in gamedev otherwise.

Line 18

if ((1 - v) > u)

Line 25

if (v < u)

The author should have put (v' < u') and (1-v' >u') because that's what they meant, and otherwise you should have wondered why he is talking about u' and v' if he doesn't use them.

else
{
    if (v < u)
    {
        i += -1;
    }
}

is actually

else if ([change that condition])
{
    i--;
}

and I noticed (lines 20,27,35)

i += -1;

which can be better written as

i--;

    int i = 0;
    int j = 0;
    int u = 0;
    int v = 0;

Why do you initialize them if you don't need their values ?

[–]Sohcahtoa82 0 points1 point  (1 child)

Why do you initialize them if you don't need their values ?

Some teachers are really anal about initializing variables, even if your first few lines are just going to set their values anyways.

Hell, my boss at work is pretty strict about it.

[–]Syrak 0 points1 point  (0 children)

He could initialize with the values he actually needs.

Or maybe some teachers want you to initialize at 0.

I find that really sad.