1

我在这里有这个算法:

pc = # the point you are coloring now
p0 = # start point
p1 = # end point
v = p1 - p0
d = Length(v)
v = Normalize(v) # or Scale(v, 1/d)

v0 = pc - p0

t = Dot(v0, v)
t = Clamp(t/d, 0, 1)

color = (start_color * t) + (end_color * (1 - t))

生成点对点线性渐变。它对我来说效果很好。我想知道是否有类似的算法来生成径向渐变。类似地,我的意思是求解点 P 的颜色,而不是求解某个颜色的 P(其中 P 是您正在绘制的坐标)。

谢谢

4

3 回答 3

3
//loop through vector
//x and y px position
int x = i%w;
int y = i/w;
float d = distance(center,int2(x,y));
//if within the grad circle
if(d < radius)
{
  //somehow set v[i] alpha to this:
  float a = d/r;
}
于 2011-04-27T03:34:42.343 回答
1

在 atan2(dy,dx) 上线性上升,其中 dx 是 x 中心,dy 是 y 中心。

cx # center x
cy # center y

r1 # ring is defined by two radius
r2 #  r1 < r2

c1 # start color
c2 # stop color

ang # start angle 

px # currect point x,y
py 
if( px^2 + py^2 <= r2^2 AND px^2 + py^2 >= r1^2  )  # lies in ring?
    t= atan2(py-cy,px-cx)+ang
    t= t+ pi # atan2 is from -pi to pi
    if (t > 2* pi) # it might over 2pi becuse of +ang
       t=t-2*pi
    t=t/(2*pi) # normalise t from 0 to 1
    color = (c1 * t) + (c2 * (1 - t))

这个算法的问题是 ang 实际上是错误的,应该由 pi 旋转并在 0 和 2pi 之间标准化。

于 2010-06-09T23:01:15.317 回答
0

根据评论,您仍然可以将您想要的内容视为线性渐变 - 即您有一条从圆心到圆外的线,并且沿着该线有一个线性渐变。因此,计算实际上与您已有的相同。

编辑:好的,显然我误解了你想要的。要计算围绕半径运行的渐变,您仍然基本上将其线性化 - 计算出该半径处的周长 (2*Pi*R),然后沿该长度的线进行线性插值。

于 2010-06-09T20:47:12.557 回答