生成径向梯度的算法

发布于 2024-09-05 04:38:23 字数 389 浏览 3 评论 0原文

我这里有这个算法:

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 是您正在绘制的坐标)。

谢谢

I have this algorithm here:

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))

to generate point to point linear gradients. It works very well for me. I was wondering if there was a similar algorithm to generate radial gradients. By similar, I mean one that solves for color at point P rather than solve for P at a certain color (where P is the coordinate you are painting).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉鱼一梦 2024-09-12 04:38:23
//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;
}
//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;
}
乖乖哒 2024-09-12 04:38:23

在 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 之间标准化。

Linerise over atan2(dy,dx) where dx is x-center, and dy is y-center.

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))

Problem whit this algorhitm is that ang is actualy wrong and should be rotated by pi and normalized between 0 and 2pi.

梦晓ヶ微光ヅ倾城 2024-09-12 04:38:23

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

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

Based on the comment, what you want can still be viewed as a linear gradient -- i.e. you have a line from the center to the outside of the circle, and you have a linear gradient along that line. As such, the calculation is virtually identical to what you already had.

Edit: Okay, apparently I misunderstood what you want. To figure a gradient running around a radius, you still basically linearize it -- figure out the circumference at that radius (2*Pi*R), and then do a linear interpolation along a line of that length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文