Re: Mipoint Line Algorithm
I am not exactly sure what you are looking for. Are you looking for the coordinates of the midpoint of a line? would'nt that be;
x = (X1+X2)/2;
y = (Y1+Y2)/2;
or
x = X1 + (X2-X1)/2;
y = Y1 + (Y2-Y1)/2;
I may have missed your problem, I have been accused of being dense, at times.
Gary Kirkham
Re: Mipoint Line Algorithm
Think I got'Ya problem.
The midpoint algorythm you're using is in its simplest mode, that is:
line goes bottom-letf upper-rigth (shallow slope) (As in the book).
The simplest thing to do is to transform coord-system u're using (i.e.: reflect agains y=x, x=0,y=0 or 0).
I'll try to illustrate it in the least elegant way - re-work it out to a neat code:
if ( abs(dx)<abs(dy) && dx>0 && dy>0)
//steep slope
{
//exchange x with y and y with x
//note: this is the reflection of
// coordinatates against:
// f(x) = x;
WritePixel(y,x,Color);
}
else
if ( abs(dx)>abs(dy) && dx>0 && dy>0)
//shallow slope
{
...
WritePixel( x, y, Color );
}
else
if ( abs(dx)>abs(dy) && dx<0 && dy>0)
//shallow slope - right left
{
x = -x;
...
WritePixel( -x, y, Color );
}
... //there are all the combinations to add here
Well, there may be some major mistakes in the above solution, cause i have punched it down without ensuring, but i can
give my head on the general idea.
So again:
transform entry coords x0,x1,y0,y1 so that:
dx=x1-x0>0 AND dy=y1-y0>0 AND dx>dy hold.
and transform the computed intermediate points back before drawing.
good luck
Remek Zajac
[email protected]