两点之间的直线方程
我需要画出连接两点的线。
我正在用 Java 做这件事。我收到两个点作为参数,我必须计算它们之间的直线是否位于黑色图形内。
我使用直线方程开发了自己的解决方案,但我的结果与使用“专业”程序(例如 GIMP 甚至 MS Paint)不同。
这是我想要的示例:
alt text http://img411.imageshack.us /img411/788/img1q.png
但我的算法是这样做的:
替代文本http://img267.imageshack.us/img267/1908/img2d.png
*绿点不在图中,这是不可能的。
有什么想法吗?有人知道在“专业”应用程序中使用了哪些代码吗?
谢谢! 丹尼尔.
编辑:图像
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在截断而不是四舍五入到最近的像素。很难看清那些小的黑色墨水斑点。你能发布代码吗?
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?
“专业”程序最有可能使用Bresenham 直线算法。
"Professional" programs most likely use Bresenham's line algorithm.
看看 Bresenham 画线算法
Look at Bresenham's line drawing algorithm
通常使用 Bresenham 算法绘制直线。我不明白你关于绿点不在图中的观点 - 有剪裁,以免浪费时间在可见区域之外绘制。
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.