# Lots of Macro Errors

Hello!

I am trying to implement Xiaolin Wu's anti-aliased line algorithm into one of my projects. However, upon trying to compile, it repeats the same 3 errors about a dozen or so times. I have no clue what they mean, and thus have failed to fix them.

Here is a frankenstein's monster of a sketch demonstrating the issue:

``````#define _swap_int16_t(a, b)  {a^=b;b^=a;a^=b;}
struct color{//not an issue
byte a=255;
union{
struct{
byte r : 5, g : 6, b : 5;
};
uint16_t col16=0;
};
color(byte r2,byte g2,byte b2,byte a2=255){
a=a2;
r=r2>>3,b=b2>>3,g=g2>>2;
}
color(uint16_t c, byte a2=255){
a=a2;
col16=c;
}
};
void pixel(int x, int y, color c,bool transform){}//just to demonstrate errors...
void lineAA(int x0,int y0,int x1,int y1,color c){
//Xiaolin Wu's Anti-Aliased line algorithm: https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
#define ipart(x)  ((int)x)
#define fpart(x)  (x-((int)x))
#define rfpart(x) (1-fpart(x))
#define plotAA(x,y,a)  {pixel(x,y,color(c.col16,(byte)(c.a*a)));}
bool steep = abs(y1 - y0) > abs(x1 - x0);

if(steep){
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if(x0 > x1){
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}

float dx = x1 - x0;
float dy = y1 - y0;
float gradient = (float)dy / (float)dx;
if(dx == 0.0)

// handle first endpoint
float xend = round(x0);
float yend = y0 + gradient * (xend - x0);
float xgap = rfpart(x0 + 0.5);
float xpxl1 = xend; // this will be used in the main loop
float ypxl1 = ipart(yend);
if(steep){
plotAA(ypxl1,   xpxl1, rfpart(yend) * xgap)
plotAA(ypxl1+1, xpxl1,  fpart(yend) * xgap)
}else{
plotAA(xpxl1, ypxl1  , rfpart(yend) * xgap)
plotAA(xpxl1, ypxl1+1,  fpart(yend) * xgap)
}
float intery = yend + gradient; // first y-intersection for the main loop

// handle second endpoint
xend = round(x1);
yend = y1 + gradient * (xend - x1);
xgap = fpart(x1 + 0.5);
float xpxl2 = xend; //this will be used in the main loop
float ypxl2 = ipart(yend);
if(steep){
plotAA(ypxl2  , xpxl2, rfpart(yend) * xgap);
plotAA(ypxl2+1, xpxl2,  fpart(yend) * xgap);
}else{
plotAA(xpxl2, ypxl2,  rfpart(yend) * xgap);
plotAA(xpxl2, ypxl2+1, fpart(yend) * xgap);
}

// main loop
if(steep){
for(float x=xpxl1 + 1;x<xpxl2 - 1;x++){
plotAA(ipart(intery)  , x, rfpart(intery));
plotAA(ipart(intery)+1, x,  fpart(intery));
}
}else{
for(float x=xpxl1 + 1;x<xpxl2 - 1;x++){
plotAA(x, ipart(intery),  rfpart(intery));
plotAA(x, ipart(intery)+1, fpart(intery));
}
}
}
void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}
``````

All of the errors are:

``````sketch_jul28d:24: error: expected unqualified-id before '(' token

#define fpart(x)  (x-((int)x))

^

C:\Users\FUZZYZ~1\AppData\Local\Temp\arduino_modified_sketch_885217\sketch_jul28d.ino:26:60: note: in definition of macro 'plotAA'

#define plotAA(x,y,a)  {pixel(x,y,color(c.col16,(byte)(c.a*a)));}

^

C:\Users\FUZZYZ~1\AppData\Local\Temp\arduino_modified_sketch_885217\sketch_jul28d.ino:83:40: note: in expansion of macro 'fpart'

plotAA(x, ipart(intery)+1, fpart(intery));

^
``````

Repeated many, many more times...

What is the issue? I have tried (and failed) to find any hint of reason as to why these errors occured, but the messages are not helpful

Thanks!

The defines should not be inside a function definition.

That's not the issue, but probably good practice. Thanks!

Fixed by replacing the macro plotAA with a function. Still have no clue why this does not work, but it's a work-around.

Fuzzyzilla:

``````  #define plotAA(x,y,a)  {pixel(x,y,color(c.col16,(byte)(c.a*a)));}
``````

---------------------------------------------------------^^^

Apart from missing the fourth argument to pixel(), you might also want to choose a different macro argument name than "a"...

Woops! This is actually implemented into my project with a default value of "true" for argument 4 (not that the function even does anything in this stripped down version!... I chose the name 'a' for alpha, but changing it fixes the issue(?!).

Thank you!