- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经用 Python 编写了 Bresenham 算法的实现(在 Wikipedia article 之后),它可以正常工作,但某些角度的线除外。所有应在 45 到 90 度之间或 135 到 270 度之间延伸的线将改为沿线 y = x 延伸。
这是我的代码:
def bresenham(origin, dest):
# debug code
print origin
print dest
# end debug code
x0 = origin[0]; y0 = origin[1]
x1 = dest[0]; y1 = dest[1]
steep = abs(y1 - y0) > abs(x1 - x0)
backward = x0 > x1
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if backward:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx = x1 - x0
dy = abs(y1 - y0)
error = dx / 2
y = y0
if y0 < y1: ystep = 1
else: ystep = -1
result = []
#if x0 > x1: xstep = -1
#else: xstep = 1
# debug code
print "x0 = %d" % (x0)
print "x1 = %d" % (x1)
print "y0 = %d" % (y0)
print "y1 = %d" % (y1)
for x in range(x0, x1):
if steep: result.append((y,x))
else: result.append((x,y))
error -= dy
if error < 0:
y += ystep
error += dx
# ensure the line extends from the starting point to the destination
# and not vice-versa
if backward: result.reverse()
print result
return result
有人看到我搞砸了吗?
编辑:
我在函数中添加了一些打印代码。
(0,0) 在显示屏的左上角。
我的测试框架非常简单。它是一个独立的函数,所以我只传递两点给它:
origin = (416, 384)
dest = (440, 347)
bresenham(origin, dest)
(416, 384)
(440, 347)
x0 = 384
x1 = 347
y0 = 416
y1 = 440
[]
最佳答案
我不知道您为什么要使用 xstep 变量。您真的不需要使用您正在使用的算法。
@Gabe: xstep is needed because without it, if x0 > x1, then the for loop will terminate immediately, as the default step for a Python for loop is 1.
你不需要 xstep 变量的原因是,如果它向后移动,坐标已经被切换(在开头的 if backward:
条件中)所以终点现在是起点,反之亦然,这样我们现在仍然从左到右。
你只需要这个:
result = []
for x in range(x0, x1):
if steep: result.append((y, x))
else: result.append((x, y))
error -= dy
if error < 0:
y += ystep
error += dx
return result
如果你想要坐标列表从开始到结束的顺序,那么你可以在最后做检查:
if backward: return result.reverse()
else: return result
编辑:问题是 backward
bool 值在它需要被求值之前被求值。如果 steep
条件执行,那么值会改变,但到那时你的 backward
条件是不同的。要解决这个问题,不要使用 backward
bool 值,而是将其设为显式表达式:
if x0 > x1:
# swapping here
再一次,因为你稍后会使用 bool 值,你可以在条件之前定义它:
backward = x0 > x1
if backward:
关于python - 对于某些角度的线,我对 Bresenham 算法的实现失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3713821/
我正在尝试为 2D 方 block 游戏编写一个闪电引擎。与大多数游戏一样,我在 map 上有一个 map [][]和一个光照贴图[][]。没什么特别的。从一个光点,我在灯光范围内画一个圆圈,并尝试将
我已经实现了 Bresenham 圆算法。但这段代码只输出一个白色窗口。不绘制圆边界的单个像素。 该函数绘制 8 个对称点。 void plot_point(int x, int y, int xc,
我正在尝试使用 Bresenham 算法在 C 语言中画一条线。我在 Windows 7 的 dosbox 中使用 turbo C++ 来实现此代码。编译时我没有收到任何错误,但是当我运行代码时程序在
我有以下代码 bresenham's algorithm表示适用于 Scala Java 代码。 def bresenham(x0: Int, y0: Int, x1: Int, y1: Int) =
我正在寻找这种行为 http://www.brainjunkie.com/web/js-wordsearch/但有了这个盒子: div { display: inline-block; wid
代码在第一次调用 positionGenerator(1046,1623,750,1290); 时运行良好,但在第二次调用 positionGenerator(1046,1623,375,810);
我试图用星星(*)填充矩阵来绘制布雷森汉姆线,但是当我打印出这个东西时,矩阵只填充了一颗星星,我不知道出了什么问题。语言是Java public class PtLine extends Vector
我正在尝试使用 Bresenham's Line Drawing Algorithm在 20x20 的瓷砖网格上画一条线。 所以基本上,当变量 deltaerror(来自 wiki)大于 1(当 y
我创建了一个函数,它接受一个 2D std::vector, vector 中的 2 个点,并在 vector 中“绘制”一条线。但是,它并不涵盖所有情况(八分圆)。一条线是指在一条直线上相互连接的点
Bresenham 的浮点算法中有些东西让我很烦。 算法如下: void line(x0, x1, y0, y1) { int deltax = x1 - x0; int deltay = y
我编写了这段代码。这段代码处理 dx 和 dy 大于 0 的情况。但是当其中一个小于 0 时该怎么办。算法说 m 应该作为绝对值,x 和 y 应该递减。但是如何在第二个 for 循环中递减 i(x)?
https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/ 我正在查看 Bresenham 的算法,我试图用它来制作 MS 绘
我正在使用中点圆算法(也称为 Bresenham 算法)来绘制同心圆。每个圆的半径与下一个圆的半径之差始终为 1,因此最终结果应该是一个完整的圆形区域。 但是,如附图所示,一些像素留空。 我正在使用
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有以下绘制圆的代码: #include #include #include #include void main() { int xc, yc, x, y, p[100], r, k;
我有一个练习,我想用 Bressenham 算法画一条线。问题是它对向下和向右的线路非常有效,但是当线路向上或向后时,它就不再工作了......有人可以帮助我吗? void draw_line(
我在 HTML Canvas 上画线,并使用不太精确的二维数组(表示 10x10 像素的 block ),我在其中使用 Bresenham 算法“画”线以存储线 ID,因此我可以使用该数组查看选择了哪
嗨,我正在尝试编写一些基本代码来实现 Bresenham 算法,但我一直在尝试使用 round和/ 。我的代码是: bresenhamAlgorithm :: Coord -> Coord -> In
我编写了一个小程序,它根据用户输入 x1,y1 和 x2,y2 绘制一条线。由于某种原因,线条只能在水平时起作用,但是一旦线条应该是垂直的,它只会向我显示黑色像素而不是线条。我已经检查了所有内容,并且
我有一个像素 电话 在网格上。我想知道该像素是否会出现在 bresenham 上之间生成的线P0 和 P1 没有实际生成线。这有可能提出这样的决定因素吗? 最佳答案 我正在写这个作为答案,即使它只是一
我是一名优秀的程序员,十分优秀!