我正在尝试沿一条线插值颜色,以便给定两个点及其各自的 RGB 值,我可以绘制一条具有平滑颜色渐变的线。使用布雷森纳姆的线条算法,我现在可以绘制线条,但不确定如何开始在两个端点之间插值颜色。以下是 drawLine() 函数的一部分,适用于斜率小于 1 的所有直线。
int x_start = p1.x, x_end = p2.x, y_start =p1.y, y_end = p2.y;
int dx = Math.abs(x_end-x_start), dy = Math.abs(y_end-y_start);
int x = x_start, y = y_start;
int step_x = x_start < x_end ? 1:-1;
int step_y = y_start < y_end ? 1:-1;
int rStart = (int)(255.0f * p1.c.r), rEnd = (int)(255.0f * p2.c.r);
int gStart = (int)(255.0f * p1.c.g), gEnd = (int)(255.0f * p2.c.g);
int bStart = (int)(255.0f * p1.c.b), bEnd = (int)(255.0f * p2.c.b);
int xCount = 0;
//for slope < 1
int p = 2*dy-dx;
int twoDy = 2*dy, twoDyMinusDx = 2*(dy-dx);
int xCount = 0;
// draw the first point
Point2D start = new Point2D(x, y, new ColorType(p1.c.r, p1.c.g, p1.c.b));
drawPoint(buff, start);
float pColor = xCount / Math.abs((x_end - x_start));
System.out.println(x_end + " " + x_start);
while(x != x_end){
x+= step_x;
xCount++;
if(p<0){
p+= twoDy;
}
else{
y += step_y;
p += twoDyMinusDx;
}
Point2D draw_line = new Point2D(x, y, new ColorType(p1.c.r*(1-pColor)+p2.c.r*pColor,p1.c.g*(1-pColor)+p2.c.g*pColor,p1.c.b*(1-pColor)+p2.c.b*pColor));
System.out.println(pColor);
drawPoint(buff,draw_line );
}
所以我的想法是,就像画线一样,我也需要某种决策参数 p 来确定何时更改 RGB 值。我正在考虑类似的事情x 增量,查看每个 rgb 值并决定是否要操纵它们.
我初始化了 rStart 和 rEnd(g 和 b 等),但不知道从哪里开始。任何形式的帮助或建议将不胜感激!
编辑:感谢@Compass 的好建议!现在,我在尝试实施该策略时遇到了另一个错误,我几乎可以肯定这是一个简单的错误。我只是现在看不到它。由于某种原因我的pColor总是返回0,我不知道为什么。我运行了一些打印语句以确保 xCount 确实在增加,所以我不确定还有什么可能使该变量始终为 0。