锥形梯度在Qt(没有QConicalGradient)
Conical gradient in Qt (without QConicalGradient)
我必须在Qt c++中绘制锥形梯度,但我不能使用QConicalGradient。我有一个线性梯度,但我不知道如何做一个锥形梯度。我不想要完成的代码,但我要求一个简单的算法。
for(int y = 0; y < image.height(); y++){
QRgb *line = (QRgb *)image.scanLine(y);
for(int x = 0; x < image.width(); x++){
QPoint currentPoint(x, y);
QPoint relativeToCenter = currentPoint - centerPoint;
float angle = atan2(relativeToCenter.y(), relativeToCenter.x);
// I have a problem in this line because I don't know how to set a color:
float hue = map(-M_PI, angle, M_PI, 0, 255);
line[x] = (red << 16) + (grn << 8) + blue;
}
}
你能帮我吗?
下面是一些伪代码:
给定一些区域来绘制,并定义渐变的中心…
对于你在区域中绘制的每个点,计算到渐变中心的角度。
// QPoint currentPoint; // created/populated with a x, y value by two for loops
QPoint relativeToCenter = currentPoint - centerPoint;
angle = atan2(relativeToCenter.y(), relativeToCenter.x());
然后使用线性渐变或某种映射函数将该角度映射到颜色。
float hue = map(-PI, angle, PI, 0, 255); // convert angle in radians to value
// between 0 and 255
绘制该像素,并在您的区域的每个像素重复。
编辑:根据渐变的模式,您将需要创建不同的QColor
像素。例如,如果你有一个"彩虹"渐变,只是从一个色调到下一个,你可以使用线性映射函数,像这样:
float map(float x1, float x, float x2, float y1, float y2)
{
if(true){
if(x<x1)
x = x1;
if(x>x2)
x = x2;
}
return y1 + (y2-y1)/(x2-x1)*(x-x1);
}
然后使用输出值创建QColor
对象:
float hue = map(-PI, angle, PI, 0, 255); // convert angle in radians to value
// between 0 and 255
QColor c;
c.setHsl( (int) hue, 255, 255);
然后使用这个QColor
对象与您正在使用的QPainter
或QBrush
或QPen
。或者如果你把qRgb值放回
line[x] = c.rgb();
http://qt project.org/doc/qt - 4.8 -/- qcolor.html
希望对你有帮助。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 如果没有malloc,链表实现将失败
- 数组索引的值没有增加
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 变量没有改变?通过向量的函数调用
- 没有名称的C++模板参数
- 在线编译器中的分段C++没有打印消息
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 没有用于初始化C++中的变量模板的匹配构造函数
- 调用'begin(int [n])'没有匹配函数
- 使用C++程序合并排序没有得到正确的输出
- 为什么在这个代码结束循环中没有得到结束
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- 没有显式声明的int[]中的foreach