I'm trying to make an algorithm which draws lines, using the DDA (Digital Differential Analyzer), that also use the Wu's algorithm as anti-aliasing.
The problem is that the output doesn't look quite good. In particular:
- the color that i choose, it changes (i know why but i want to know if it has to be like this)
- the color of the pixel more bright is to bright
How can i choose the color that i want for the line? Considering that it's affected by the algorithm ?
Here's the code:
void dda(int x0, int y0, int x1, int y1, int z, float red, float green, float blue) {
float dy = y1-y0;
float dx = x1-x0;
float m = dy/dx;
if (m<=1) {
int x;
float y;
y = y0;
for (x=x0; x<x1; x++) {
pixel(x, round(y), z, frame, rfpart(red), rfpart(green), rfpart(blue));
pixel(x, round(y)+1, z, frame, fpart(red), fpart(green), fpart(blue));
y = y+m;
}
}
}
int round(float d) {
return floor(d + 0.5);
}
float fpart(float x) {
if (x < 0)
return 1 - (x - floor(x));
return x - floor(x);
}
float rfpart(float x) {
return 1 - fpart(x);
}
your code works only for first octant
so I hope you are testing only there
you have forgot to mix the background color and line color
so add transparency or read the background pixel directly and mix colors on your own. The
a,a0coefficients would be the alpha for transparency color mixing. In that case you should not change ther,g,bvalues but the alpha only instead. Also if you know the background color you could ignore the reading of pixels but the result will be of a bit while crossing something already rendered.I change your code to be compatible with mine coding in C++:
changed to fixed point (8-bit fractional part)
f,dfchanged the
roundtofloor(my pixels are already shifted by half)added color mixing with background color
pnt(x,y,col);draws a pixelx,ywith colorcolcol=pnt(x,y);reads the pixel from screen/image intocolcolis 32 bit color (0x00RRGGBB) that union is there just for easyr,g,baccess