Click to See Complete Forum and Search --> : Mipoint Line Algorithm


Pipe
April 2nd, 1999, 04:09 PM
hello,
can someone help me get the case of a steep slope line for the
midpoint line algorithm. 1 < slope < infinity


here is how i computer a postive shallow slope.
0 < slope < 1

i just cant figure out a steep slope.

dx = x1 - x0;
dy = y1 - y0;
d = dy * 2 - dx;
incrE = dy * 2;
incrNE = (dy -dx) * 2;
...
else if( dx > abs(dy) ){ /* Shallows */
if( dy > 0 ){ /* Positive Shallow */

while( x < x1 ){
if( d <= 0 ){
d+=incrE;
x++;
}
else{
d+= incrNE;
x++;
y++;
}
SRGP_pointCoord( x, y );
}


thanks,
pipe

April 2nd, 1999, 06:30 PM
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

Remek Zajac
April 6th, 1999, 05:26 PM
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
zajac@ci.pwr.wroc.pl