开发者

Straight Line Equation between two points

开发者 https://www.devze.com 2023-01-03 22:28 出处:网络
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 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 algorith开发者_运维百科m 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


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?


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


Look at Bresenham's line drawing 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消