Re: [OT] a couple of C macro queries

new topic     » goto parent     » topic index » view thread      » older message » newer message
jimcbrown said...
petelomax said...

One for the C fans. Translating this bit of C code:

#define INVERT_Y(_y) (height-y) 
 
static int motion_cb(Ihandle *ih,int x,int y,char* status) 
{ 
  (void)status; 
 
  if (move) 
  { 
    double dif_x, dif_y; 
    int height = IupGetInt2(ih, "RASTERSIZE"); 
 
    dif_x = x - pos_x; 
    dif_y = y - pos_y; 
 
    pos_x = x; 
    pos_y = y; 
 
    gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,... 

Should I really just replace INVERT_Y(pos_y) with (height-y)?
Is the use of pos_y a complete red herring?

I think so. I can't find any reference to the underscore having a special meaning to the preprocessor, and _y is a valid identifier in C (and to the C preprocessor itself).

Make you wonder why they didn't simply just write "height-y" in that spot in the first place... it's less typing for one.

Thinking it over, I think the original C coder screwed up the macro, but it just so happened the resulting statement was equivalent to the intended one.

I suspect that this was the C code that was intended:

#define INVERT_Y(_y) (height-(_y)) 
 
static int motion_cb(Ihandle *ih,int x,int y,char* status) 
{ 
  (void)status; 
 
  if (move) 
  { 
    double dif_x, dif_y; 
    int height = IupGetInt2(ih, "RASTERSIZE"); 
 
    dif_x = x - pos_x; 
    dif_y = y - pos_y; 
 
    pos_x = x; 
    pos_y = y; 
 
    gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,... 

which, postprocessing, would have become this:

static int motion_cb(Ihandle *ih,int x,int y,char* status) 
{ 
  (void)status; 
 
  if (move) 
  { 
    double dif_x, dif_y; 
    int height = IupGetInt2(ih, "RASTERSIZE"); 
 
    dif_x = x - pos_x; 
    dif_y = y - pos_y; 
 
    pos_x = x; 
    pos_y = y; 
 
    gluUnProject(pos_x, height-(pos_y), 0.0,... 

It just so happens that pos_y and y have the same value (since pos_y was set to y right before the call), so the bad C macro ended up coming up with the desired result by accident.

I guess pos_y is a module level or file level variable that is initialize elsewhere, as I don't see it declared or initialized in this snippet.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu