tags:

views:

90

answers:

4

Hi, I need to paint the line which links two points.

I am doing it, in Java. I receive two points as parameters and I have to calculate if the straight line between them, is inside the black figure.

I developed my own solution using the straight line equation, but my results are different than using the "professional" programs (such as GIMP or even MS Paint).

Here is a example of what I want:

alt text

But my algorithm does this:

alt text

*The green point is out of the figure and this is not possible.

Any ideas? Anyone know which code is been using for this, in "professional" apps?

Thanks! Daniel.

EDIT: Images

+2  A: 

"Professional" programs most likely use Bresenham's line algorithm.

Seth
Peter solved my doubt, but thanks for the info anyway!
dafero
+1  A: 

Look at Bresenham's line drawing algorithm

Lie Ryan
+1  A: 

Straignt lines are drawn using Bresenham's algorithm usually. I didn't get your point about green point being out of the figure - there's clipping to not waste time drawing outside the visible area.

Berkus
Peter solved my doubt, but thanks for the info anyway!
dafero
+3  A: 

It looks like you are truncating instead of rounding to the nearest pixel. Difficult to see on those small black ink splotches. Could you post the code?

Peter Tillemans
Indeed. Looks like your algorithm expects there to be 2 line segments instead of 3 segments. Is your dx/dy right? (Given that you round, not truncate).
Berkus
WOW! Peter you are a genius!!!I was using integers and that was the problem!Thank you!! :D
dafero