Question

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 http://img411.imageshack.us/img411/788/img1q.png

But my algorithm does this:

alt text http://img267.imageshack.us/img267/1908/img2d.png

*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

Was it helpful?

Solution

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?

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top