【发布时间】:2012-11-08 03:07:07
【问题描述】:
“组”是指一组像素,使得每个像素在同一组中至少有一个相邻像素,图中显示了组的示例。
我想找到与指定像素(例如,绿色像素)的直线距离最大的像素。并且连接两个像素的直线(红线)不能离开组。
我的解决方案是循环遍历度数并模拟从带有度数的绿色像素开始的线条的进度,并查看哪条线行进的距离最远。
longestDist = 0
bestDegree = -1
farthestX = -1
farthestY = -1
FOR EACH degree from 0 to 360
dx=longestDist * cos(degree);
dy=longestDist * sin(degree);
IF Point(x+dx , y+dy) does not belong to the group
Continue with next degree
//Because it must not be the longest line, so skip it
END IF
(farthestX , farthestY) = simulate(x,y,degree)
d = findDistance(x , y , farthestX , farthestY)
IF d > longestDist
longestDist = d
bestDegree = degree
END IF
END FOR
这显然不是最好的算法。因此,我在这里寻求帮助。
谢谢你,对不起我的英语不好。
【问题讨论】:
-
请注意,您可以丢弃所有内部像素的计算。
-
而且你不需要使用角度。你只需要使用勾股定理;)
-
@dfens: "内部像素" - 为什么?其中之一可能是解决方案。
-
@Karoly Horvath:我明白我们正在寻找两个最远的像素的问题?所以内部像素不是解决方案?
标签: algorithm pixel distance point