This topic is now archived and is closed to further replies.


Line plotting

Recommended Posts

Right it coding somthing in basic language.. What I want to do is plot a line between 2 points and be able to plot each of the points along the line... What im doing is making sure that a bullet shot at me in 2d then travels right @ me if you know what I mean...

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
search for the bresenham line algorithm
there should be hundreds of docs on it.
chose the easiest explained one.

(my fav searching engine is google :-)

Share this post

Link to post
Share on other sites
Here''s a simple technique that isn''t as fast as Bresenham''s but takes me less time to type!

void drawLine(int x1, int y1, int x2, int y2)
int deltaX = x1 - x2;
int deltaY = y1 - y2;
float dist = sqrt(square(deltaX) + square(deltaY));

deltaX /= dist;
deltaY /= dist;

int x = x1;
int y = y1;
for(int d = 0; d < dist; d++)
plotPixel(x, y); //Replace with appropriate function

x += deltaX;
y += deltaY;

I''ll post a BASIC equivalent below (I''m a little rusty in BASIC, so forgive me ifthe syntax isn''t perfect):

Sub drawLine(x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer)

Dim deltaX as Integer
deltaX = x1 - x2

Dim deltaY as Integer
deltaY = y1 - y2

Dim dist as Single
dist = sqr(deltaX^2 + deltaY^2)

deltaX = deltaX / dist
deltaY = deltaY / dist

Dim x as Integer
x = x1
Dim y as Integer
y = y1

Dim d as Integer
For d = 1 To dist
plotPixel(x, y) ''Replace with PUT or whatever you use
x = x + deltaX
y = y + deltaY
Next d
End Sub

Hope that helps!

Share this post

Link to post
Share on other sites