创建在二维数组的线性梯度梯度、数组、线性

2023-09-11 22:56:47 作者:春风不识路

我已经让我们说500 * 500的值的2D位图般的阵列。我试图创建阵列上的线性渐变,所以生成位图看起来像这个(灰度):

的输入将是阵列填补,两个点(像起止点在Photoshop / GIMP渐变工具)和值的这将要使用的范围。

我目前最好的结果是这样的:

...这是隔靴搔痒什么,我想实现。它看起来更像是一个径向渐变。

什么是创造这样一个梯度的最简单方法?我会在C ++来实现它,但我想一些通用的算法。

解决方案

在你的榜样形象,它看起来就像你有一个径向渐变。这是我的impromtu的数学解释你需要的步骤。很抱歉的数学,其他的答案都在执行方面更好。

定义的线性函数(如Y = X + 1)与域(即X)从要开始的颜色你想结束与色彩之中。你可以认为这在一定范围内的职责范围内OX0到OxFFFFFF(24位色)。如果你要处理的事情如亮度,你就必须做一些技巧的范围(即y值)。 接下来,您需要映射整个你有矩阵向量,因为这定义了方向的颜色会改变。此外,通过您的线性函数定义的颜色值将沿向量中的每个点分配。矢量的起点和终点也定义域的最小值和最大值的1你可以把矢量作为一行渐变的。 对于矩阵中的每个单元,颜色可以分配从其中来自细胞的垂线相交的向量的向量的值。见下文其中c是细胞和的位置的图。是交叉点。如果pretend,在颜色。是红色的,那么这就是你将分配给小区什么。

             |
             C
             |
             |
    VECT:____.______________
             |
             |

物联网学习教程 二维数组的引用

I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to create a linear gradient on the array, so the resulting bitmap would look something like this (in grayscale):

The input would be the array to fill, two points (like the starting and ending point for the Gradient tool in Photoshop/GIMP) and the range of values which would be used.

My current best result is this:

...which is nowhere near what I would like to achieve. It looks more like a radial gradient.

What is the simplest way to create such a gradient? I'm going to implement it in C++, but I would like some general algorithm.

解决方案

In your example image, it looks like you have a radial gradient. Here's my impromtu math explanation for the steps you'll need. Sorry for the math, the other answers are better in terms of implementation.

Define a linear function (like y = x + 1) with the domain (i.e. x) being from the colour you want to start with to the colour your want to end with. You can think of this in terms of a range the within Ox0 to OxFFFFFF (for 24 bit colour). If you want to handle things like brightness, you'll have to do some tricks with the range (i.e. the y value). Next you need to map a vector across the matrix you have, as this defines the direction that the colours will change in. Also, the colour values defined by your linear function will be assigned at each point along the vector. The start and end point of the vector also define the min and max of the domain in 1. You can think of the vector as one line of your gradient. For each cell in the matrix, colours can be assigned a value from the vector where a perpendicular line from the cell intersects the vector. See the diagram below where c is the position of the cell and . is the the point of intersection. If you pretend that the colour at . is Red, then that's what you'll assign to the cell.

             |
             c
             |
             |
    Vect:____.______________
             |
             |