plot points in a line

Posted by: RobotCaleb

plot points in a line - 16/06/2003 10:15

whats a good formula to plot all the points between two points?
i need to check the color of points between two points in a straight line from point to point.
Posted by: CrackersMcCheese

Re: plot points in a line - 16/06/2003 10:35

You talking linear trends? y = mx + c and r values and all that? Excel will do this.

'x' and 'y' are the coordinates of the points that satisfy the function and so lie on the straight line graph.

'm' is the gradient of the straight line graph, and

'c' is the 'y intercept' of the straight line graph.


Sorry if this is waaaaay off what you mean!
Posted by: trs24

Re: plot points in a line - 16/06/2003 12:25

whats a good formula to plot all the points between two points?
Wouldn't plotting those points take... well... forever?
Posted by: mtempsch

Re: plot points in a line - 16/06/2003 12:45

Wouldn't plotting those points take... well... forever?

Heh, that was my first thought for a reply also...

i need to check the color of points between two points in a straight line from point to point.

But I'll assume all integer pairs creating a line between say (1;3) and (25;6). Sounds like he has a generated matrix with color values that he needs to check.

If we move from 1 to 25 in x-direction, somewhere the line will "step up" from 3 to 4, 4 to 5 and 5 to 6. I'd suspect that if there was some mathematical function resulting in those dots beeing draw, the exact point for the "steps" can vary depending on roundings. Ie, did the 3 in (1;3) become a three because it was actually just larger than 2.5, or is it 3 because it was just slightly less than 3.5? See pic below for the idea.



Became pretty poor as I scaled it, but the idea should come through. The red points are at the same x and y distance in both cases, but the "route" is slightly different.


/Michael
Posted by: RobotCaleb

Re: plot points in a line - 16/06/2003 16:43

k, heres what i got so far

i find the distance between the two points
i use that as my counter, roughly that should be how many pixels will be between these points.
then i find the rise over run using the same numbers i did in the distance formula. i convert this rise over run to two numbers. one being smaller than one and one being equal to one by dividing both numbers by the larger.
i then check the pixels at all the points in between by adding the new rise and run to the current/last drawn point.
if that makes sense

the problem i run into is the line drawn/checked ends up going further than the points checked, which kinda makes sense as the counter doesnt necessarily equal the number of drawn points.

i dont have the exact code handy to post here.
Posted by: mtempsch

Re: plot points in a line - 16/06/2003 21:42

For what you're doing it sounds like you want to find the distances in x and y direction, then loop over whichever is larger (say delta_x > delta_y) - this to limit rounding errors - and then step off in that direction, "stepping up" when you've gone delta_x/delta_y pixels (might be a -1 needed somewhere in these formulas - too early for creative thinking) .

But I believe this is susceptible to the problem in my last post, in case the pixels you're checking has been determined by some other function. If you're merely painting pixels in a line between 2 points, this should work OK...

/Michael
Posted by: gbeer

Re: plot points in a line - 16/06/2003 23:13

You'll have to translate to whatever the proper syntax for the coding you use. Its a start and possibly buggy.

This shouldn't care what order the points are given in or which direction the slope is.

glenn

start point = (x1,y1)
end point = (x2,y2)

xdir=(x1-x2)/abs(x1-x2)
ydir=(y1-y2)/abs(y1-y2)

rise=abs(y1-y2)
run=abs(x1-x2)
slope=rise/run
count=0

if run>=rise
do until count=x2
check pixel at (x1,y1)
count=count+xdir
x1=x1+xdir
y1=int(y1+slope*count) < buggy
end
else
do until count=y2
check pixel at (x1,y1)
count=count+xdir
y1=y1+ydir
x1=int(x1+(1/slope)*count) <buggy
end
endif

edit: yep it's buggy but its bed time. There is always tommorrow
Posted by: RobotCaleb

Re: plot points in a line - 17/06/2003 10:20

thanks, ill see what i can do with that.
whats the great error with my logic above?