wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Line drawing algorithms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Line drawing algorithms" Watch "Line drawing algorithms" New topic

Line drawing algorithms

John Brookes

Joined: Feb 26, 2004
Posts: 29
Does anyone know the algorithm for drawing polygonal lines (ie: from (x1,y1) to (x2,y2) for any value of the x/y variables)? I can't use the Java libraries to do it because, to be honest, I'm doing it in C and I haven't got enough program memory to store the maths libraries. I haven't been able to find any algortihms on the net and, while I'm close to figuring it out myself, I'm getting stuck on working out the gradient loops.

So far i've got this:

y = (int)y2 - (int)y1; // finds length of y
x = x2 - x1;// finds length of x
m = (y/x); // Grad of line
c = y-mx; // intersection w/ y-axis

x = x*-1; // Make the length positive for use in loop

y = y*-1; // Make the length positive for use in loop

for(i=x1; i<=x2; i++)

for(i=y1; i<=y2; i++)


and a load of ideas in my head that dont work s

Thanks in advance.
Consider Paul's rocket mass heater.
subject: Line drawing algorithms
Similar Threads
Drawing a Diamond Shape (like Rectangle, Circle)
drawRect() issues
for loop working 2 - 4 times
New College Project
Using mouse clicks to draw on a JFrame, problem probably with coordinates