Math question - plotting a line

Status
Not open for further replies.

Red Squirrel

No Lifer
May 24, 2003
71,319
14,088
126
www.anyf.ca
I can't seem to find much on google on how to do this.

Given two sets of coordinates, what are the calculation to plot each pixel of a line?

Ex: I want to make a line go from (5,5) to (100,100)

So when x is 6, then y is going to be equal to 6 as well, this is an easy example because it's a diagonal line. But basically I'm trying to find y given a certain value of x.

I need to do this programaticaly and I'm pretty sure I recall learning this stuff in math in school, and I think it's actually the first time where I can actually apply one of those things to the real world... but it's been too long so I don't remember any of it anyway. :awe:

I figured I'd find a lot of this stuff on google but all I'm finding is calculators that want to do it for me and spit out the result. I need the actual formula.
 

Red Squirrel

No Lifer
May 24, 2003
71,319
14,088
126
www.anyf.ca
I found that, but that's just for plotting at an angle isn't it? m modifies the angle and b offsets it or something like that? Could have sworn there was one for point to point.
 

Jeff7

Lifer
Jan 4, 2001
41,596
20
81
Straight line equations.


Rise/run gets you the slope, m.
Once you've got that and a set of x,y coordinates, solve for the y-intercept, b.

Now you've got the y=mx+b equation.

Plug in x;
Get y;
x++;
 

Red Squirrel

No Lifer
May 24, 2003
71,319
14,088
126
www.anyf.ca
Ohh ok think I got what I need, I'll have to play with that. Wow it's been so long since I played with any of that but it's kind of coming back. :p
 

Jeff7

Lifer
Jan 4, 2001
41,596
20
81
I've referenced that site various times as well, sometimes just for the stupidest little things.

It's astonishing how much information just goes away from memory when it isn't used. At least there are shreds of it left somewhere. "I knew this at one point in time. This can be done, and I don't think it was all that difficult."
 
Aug 12, 2004
106
0
76
to plot

(5,5) to (100,100)

you have all you need. Just fill in those points on a piece of graph paper, get a straight edge and draw a line through points.

mathematically, you see the

y=mx+b?

well with those two coordinates you have

(5,5) to (100,100)

(X1,Y1) and (X2,Y2)

M= (Y2-Y1)/(X2-X1)

once you find that out, use one of the sets of points, and the m value, and solve for b.

Then you will have the generic slope intercept form for a line
 

Belegost

Golden Member
Feb 20, 2001
1,807
19
81
I'm thinking that you're not particularly concerned with speed, in which case just taking the line equation, computing a y value for each x in floats, and rounding the y value to an integer pixel value is probably good enough.

However, if speed is a concern, then I recommend looking at Bresenham's Algorithm for an efficient implementation strictly in ints.
 

Elixer

Lifer
May 7, 2002
10,371
762
126
Have no fear, common core is here!
First, draw a square.
Then draw a line through the square from one corner to the opposite corner.
You just made 2 triangles!
Now erase everything except the line you made from one corner to another.
There is your line!

It is like magic!

Now, to translate that programmatically is really easy using logo...
 
Status
Not open for further replies.