2013-03-11 29 views
1

我必须在Qt C++中绘制圆锥渐变,但我无法使用QConicalGradient。我确实有一个线性渐变,但我不知道如何制作一个圆锥渐变。我不想完成代码,但我要求一个简单的算法。Qt中的圆锥渐变(不含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; 
    } 
} 

你能帮助我吗?

+0

*为什么*你不能使用'QConicalGradient'? – ecatmur 2013-03-11 17:21:53

+0

因为我们必须实现您自己的绘图圆锥渐变版本 – 2013-03-11 17:56:20

回答

1

下面是一些伪代码:

鉴于一些地区油漆,并为您的梯度定义的中心......

对于您在区域画上的每个点,计算角度到你的渐变的中心。

// 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对象与你QPainterQBrushQPen您正在使用。或者,如果你是把一个qRgb值回:

line[x] = c.rgb(); 

http://qt-project.org/doc/qt-4.8/qcolor.html

希望有所帮助。

+0

我不知道我必须在这一行做什么:float hue = map(-PI,angle,PI,0,255); 我在下面粘贴了我的代码。 – 2013-03-12 19:54:14