所以我正在写一个Bresenham线算法的版本。我写了一个类,我尝试将其转换为java 8样式(这是我第一次尝试java 8)。我希望类的用户要么传入一个返回布尔值的lambda(是否继续),要么传入一个返回void的lambda(以完成算法的结尾)。
当我写两个单独的lambda接口时,我收到错误The method Line(int, int, int, int, BresenhamPlotter.BresenhamPlotFunctionAlwaysContinue) is ambiguous for the type BresenhamPlotter
这是我在课堂上的第一次尝试,但我在第一种方法中遇到了这个错误。
public class BresenhamPlotter {
/**
* The Interface BresenhamPlotFunction.
*/
public interface BresenhamPlotFunction {
/**
* Plot function, pass it into a plotting algorithm.
*
* @param x
* the x position
* @param y
* the y position
* @return True to continue, false to stop the algorithm early
*/
public boolean plotFunction(int x, int y);
}
public interface BresenhamPlotFunctionAlwaysContinue {
/**
* Plot function, pass it into a plotting algorithm.
*
* @param x
* the x position
* @param y
* the y position
*/
public void plotFunction(int x, int y);
}
/**
* Plots a line using the Bresenham line algorithm from (x0, y0) to (x1, y1) and plots each point using the plot function.
*
* @param x0
* the start x
* @param y0
* the start y
* @param x1
* the end x
* @param y1
* the end y
* @param plot
* the plot function
*/
public static void Line(final int x0, final int y0, final int x1, final int y1, final BresenhamPlotFunctionAlwaysContinue plot) {
Line(x0, y0, x1, y1, (x, y) -> {
plot.plotFunction(x, y);
return true;
});
}
/**
* Plots a line using the Bresenham line algorithm from (x0, y0) to (x1, y1) and plots each point using the plot function.
*
* @param x0
* the start x
* @param y0
* the start y
* @param x1
* the end x
* @param y1
* the end y
* @param plot
* the plot function
*/
public static void Line(int x0, int y0, final int x1, final int y1, final BresenhamPlotFunction plot) {
final int w = x1 - x0;
final int h = y1 - y0;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
if (w < 0) {
dx1 = -1;
} else if (w > 0) {
dx1 = 1;
}
if (h < 0) {
dy1 = -1;
} else if (h > 0) {
dy1 = 1;
}
if (w < 0) {
dx2 = -1;
} else if (w > 0) {
dx2 = 1;
}
int longest = Math.abs(w);
int shortest = Math.abs(h);
if (!(longest > shortest)) {
longest = Math.abs(h);
shortest = Math.abs(w);
if (h < 0) {
dy2 = -1;
} else if (h > 0) {
dy2 = 1;
}
dx2 = 0;
}
int numerator = longest >> 1;
for (int i = 0; i <= longest; i++) {
plot.plotFunction(x0, y0);
numerator += shortest;
if (!(numerator < longest)) {
numerator -= longest;
x0 += dx1;
y0 += dy1;
} else {
x0 += dx2;
y0 += dy2;
}
}
}
/**
* Plots a line using the Bresenham line algorithm from (x0, y0) to (x1, y1) and returns a list of the points.
*
* @param x0
* the start x
* @param y0
* the start y
* @param x1
* the end x
* @param y1
* the end y
* @return the list of plotted points
*/
public static List<Point> Line(int x0, int y0, final int x1, final int y1) {
final List<Point> points = new ArrayList<>();
final int w = x1 - x0;
final int h = y1 - y0;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
if (w < 0) {
dx1 = -1;
} else if (w > 0) {
dx1 = 1;
}
if (h < 0) {
dy1 = -1;
} else if (h > 0) {
dy1 = 1;
}
if (w < 0) {
dx2 = -1;
} else if (w > 0) {
dx2 = 1;
}
int longest = Math.abs(w);
int shortest = Math.abs(h);
if (!(longest > shortest)) {
longest = Math.abs(h);
shortest = Math.abs(w);
if (h < 0) {
dy2 = -1;
} else if (h > 0) {
dy2 = 1;
}
dx2 = 0;
}
int numerator = longest >> 1;
for (int i = 0; i <= longest; i++) {
points.add(new Point(x0, y0));
numerator += shortest;
if (!(numerator < longest)) {
numerator -= longest;
x0 += dx1;
y0 += dy1;
} else {
x0 += dx2;
y0 += dy2;
}
}
return points;
}
}
我将第一个方法更改为匿名类以避免错误
Line(x0, y0, x1, y1, new BresenhamPlotFunction() {
@Override
public boolean plotFunction(final int x, final int y) {
plot.plotFunction(x, y);
return true;
}
});
但是现在当我尝试调用该方法时,我会出现歧义错误
BresenhamPlotter.Line(3, 0, 3, 5, (x, y) -> doStuff(x,y));
如何在不使用匿名类或更改接口参数的情况下解决此问题?