Take the 2-minute tour ×

How do I draw a non-orthogonal line?

If we have an orthogonal line it's pretty easy:

Consider X1 = 100, Y1 = 80; And X2=100, Y2 = 185;

So we have something like this for this line.

for(nRow = Y1; nRow < Y2; nRow++)
   for(nCol = X1; nCol < X2; nCol++)
     nPixPos = nRow*nEffectiveWidth+nCol*3;

     Image[nPixPos] = 0 ; /// Image -> unsigned char * (BUFFER) || 0 -> Black COLOR
     Image[nPixPos+1] = 0 ;
     Image[nPixPos+2] = 0 ;

If I want to draw a non-orthogonal line such as:

X1 = 100 , Y1 = 80 and X2 = 115 , Y2 = 185

How would I go about constructing a loop to draw this line?

share|improve this question
If you are using some graphics software, you should either add a tag (if it exists) or explain in the question. C++ by doesn't know anything about drawing. –  juanchopanza May 10 '12 at 5:55
We'll need some more information than just the code you have there, what are you using to draw the lines? What is 'Image' and what does the value 0 represent in it? –  Jeames Bone May 10 '12 at 5:58
I don't know, I figured it was pretty obvious from context that he was looking for a general line drawing algorithm and didn't really understand how to ask the question... Also I'm going to guess that the 0 represents either color or some value that turns that pixel or tile off... –  OmnipotentEntity May 10 '12 at 6:00
I hope this questions helps you guys to understand..Thanks @OmnipotentEntity –  Pixel May 10 '12 at 6:11
add comment

1 Answer

up vote 3 down vote accepted

You have a few algorithms to choose from I suggest Bresenham's algorithm if you don't want anti-aliasing and Xiaolin-Wu's if you do.

share|improve this answer
add comment

Your Answer


