void LineBand::updatePath()
{
	//get the difference between the position and the current mouse position
	QPointF diff = mMouseNow - mPosition;
	
	//get the rectangle from that diff to the center
	QRect newr(0, 0, diff.x(), diff.y());
	newr = newr.normalized();
	
	//expand our size rectangle to contain newr
	mMaxGeom = mMaxGeom.unite(newr).normalized();
	
	//set the geometry to that rectangle, translated to our position
	setGeometry(mMaxGeom.translated(mPosition.x(), mPosition.y()).normalized());
	
	//translate the painter to be offset from the topleft of mMaxSize
	QPoint targetOffset = mMaxGeom.topLeft();
	QPoint remainingOffset = mCurPainterTranslation - targetOffset;	
	mIntersectionPath.translate(remainingOffset);
	mVisiblePath.translate(remainingOffset);
	mCurPainterTranslation = targetOffset;
	
	//update the path by drawing a line to diff
	mIntersectionPath.lineTo(diff - mCurPainterTranslation - QPointF(0.5, 0.5));
	mIntersectionPath.moveTo(diff - mCurPainterTranslation - QPointF(0.5, 0.5));
	mVisiblePath.lineTo(diff - mCurPainterTranslation - QPointF(0.5, 0.5));
	
	repaint();
}
int cg(MAT &A,VEC b,VEC &x,int maxIter, double tol)
{	
	//A_p: A * p
	VEC  A_p(A.dim());
    int node = std::sqrt(A.dim());
    //p: search direction; 
    VEC p(A.dim()), r(A.dim()), newr(A.dim()), newx(A.dim());//,ans(A.dim()),temp(A.dim());
    //MAT LU(A.dim()),a = A;
    //r2: rT * r
    double alpha, beta, r2, newr2, err;//,g;
    //g = (double)(node-1)/2000.0;
    int iter = 0;//
    /*
    LU = luFact(a);       //LU in-place decomposition
    temp=fwdSubs(LU,b);   //forward substitution
    ans= bckSubs(LU,temp);//backward substitution
    */

    
    //Initial condition for CGM
    p = r = b - (A * x);
    r2 = r * r;
   
    while(iter < maxIter)
    {
	A_p = A * p;
	alpha = r2 / (p * A_p);
	newx = x + (alpha * p);
	err = std::sqrt(r2/A.dim());
	if ( err < tol )
            break;

	newr = r - (alpha * A_p);
	newr2 = newr * newr;
	beta = newr2 / r2;
	p = newr + beta * p;


	//Re-initialization for next iteration
	x = newx;
	r = newr;
	r2 = newr2;
	//////////////////////////////////////
	iter++;		
    }
    //printf("cg:Vne: %lf; Vsw: %lf; Vse: %lf; R:%lf\n",newx[node-1],newx[(node-1)*node],newx[node*node-1], std::abs( 1/(g*(newx[0]-newx[1])+g*(newx[0]-newx[node] )) ) );
    //printf("LU:Vne: %lf; Vsw: %lf; Vse: %lf; R:%lf\n",ans[node-1],ans[(node-1)*node],ans[node*node-1], std::abs( 1/(g*(ans[0]-ans[1])+g*(ans[0]-ans[node] )) )  );
    //printf("Difference w.r.t. hw4: %E\n",linfnorm(ans - newx));
    return iter;
}
Example #3
0
vec3f PointLight::_shadowAttenuation(const vec3f& P, const ray& r) const
{
	double distance = (position - P).length();
	vec3f d = r.getDirection();
	vec3f result = getColor(P);
	vec3f curP = r.getPosition();
	isect isecP;
	ray newr(curP, d);
	while (scene->intersect(newr, isecP))
	{
		//prevent going beyond this light
		if ((distance -= isecP.t) < RAY_EPSILON) return result;
		//if not transparent return black
		if (isecP.getMaterial().kt.iszero()) return vec3f(0, 0, 0);
		//use current intersection point as new light source
		curP = r.at(isecP.t);
		newr = ray(curP, d);
		result = prod(result, isecP.getMaterial().kt);
	}
	return result;
}
void GridDisplay::paintEvent(QPaintEvent* /* event */) 
{

	QPainter painter;
	painter.begin(this);
	_cellSize = grid_->getCellSize(_winSize);

	//Find density
	int maxDensity = 0;
	int minDensity = 100;
	for (int i=0; i < grid_->getFiles().size(); i++) {
		if(grid_->getFilesAt(i).size() > maxDensity)
		{
			maxDensity = grid_->getFilesAt(i).size();
		}
		else if (grid_->getFilesAt(i).size() < minDensity) 
		{
			minDensity = grid_->getFilesAt(i).size();
		}
	}
	Colormap *map = Colormap::factory(Colormap::GreyScale);
	for (int i=0; i < grid_->getHeight(); i++) {
		for (int j=0; j < grid_->getWidth(); j++) {

			int k = j * grid_->getHeight() + i;

			QRect	 myr(i*_cellSize,j*_cellSize,_cellSize,_cellSize);
			QLine	 myl1(i*_cellSize,j*_cellSize, i*_cellSize, j*_cellSize + _cellSize);
			QLine	 myl2(i*_cellSize,j*_cellSize, i*_cellSize+_cellSize, j*_cellSize );

			if ( grid_->getFilesAt(k).size() == 0 ) 
			{
				QColor color;
				if(colourMapMode_)
				{
					color.setRgb(map->getRed(0), map->getGreen(0), map->getBlue(0));
				}
				else
				{
					color.setRgb(map->getRed(128), map->getGreen(0), map->getBlue(0));
				}
				painter.setBrush(color);
			}
			else 
			{
				if(colourMapMode_)
				{
					/*
					* index - genre - color
					* 0 - blues - Qt::blue
					* 1 - classical - Qt::darkRed
					* 2 - country - Qt::green
					* 3 - disco - PURPLE
					* 4 - hiphop - Qt::yellow
					* 5 - jazz - Qt::darkGreen
					* 6 - metal - BROWN
					* 7 - reggae - PINK
					* 8 - rock - ORANGE
					* 9 - pop - Qt::grey
					*/
					int * genreDensity = grid_->getDensity(k);
					double *squarePaintSize = new double[10];
					int startHeight = j*_cellSize;

					for(int m = 0; m < 10; m++)
					{
						squarePaintSize[m] = genreDensity[m] /  (1.0 * grid_->getFilesAt(k).size() );
					}

					// 10 is the number of genres
					for(int l = 0; l < 10; l++)
					{
						QColor * color;
						switch(l)
						{
						case 0:
							color = new QColor(Qt::blue);
							break;
						case 1:
							color = new QColor(Qt::darkRed);
							break;
						case 2:
							color = new QColor(Qt::green);
							break;
						case 3:
							color = new QColor(PURPLE);
							break;
						case 4:
							color = new QColor(Qt::yellow);
							break;
						case 5:
							color = new QColor(Qt::darkGreen);
							break;
						case 6:
							color = new QColor(BROWN);
							break;
						case 7:
							color = new QColor(PINK);
							break;
						case 8:
							color = new QColor(ORANGE);
							break;
						case 9:
							color = new QColor(Qt::gray);
							break;
						}
						if(grid_->getFilesAt(k).size() > 0)
						{
							painter.setBrush(*color);
							painter.setPen(Qt::NoPen);
							QRect rect(i*_cellSize, startHeight, _cellSize, squarePaintSize[l] * _cellSize);
							painter.drawRect(rect);
							startHeight += squarePaintSize[l] *  _cellSize;
						}
					}

				}
				else
				{
					int c = int(grid_->getFilesAt(k).size() / float(maxDensity) * (map->getDepth()-1));
					QColor color(map->getRed(c), map->getGreen(c), map->getBlue(c));
					painter.setBrush(color);
				}
			}

			if(colourMapMode_)
			{
				painter.setPen(Qt::white);
				if(grid_->getFilesAt(k).size() == 0)
				painter.drawRect(myr);
			}
			else
			{
				painter.setPen(Qt::NoPen);
				painter.drawRect(myr);
				painter.setPen(Qt::black);
			}
			painter.drawLine(myl1);
			painter.drawLine(myl2);

			painter.setBrush(Qt::red);
			QRect newr( grid_->getXPos() * _cellSize + _cellSize / 4,
				grid_->getYPos() * _cellSize + _cellSize / 4,
				_cellSize - _cellSize / 2,
				_cellSize-_cellSize / 2);
			painter.drawRect(newr);

			painter.setBrush(Qt::white);
			QRect newr1( grid_->getX1Pos() * _cellSize + _cellSize / 4,
				grid_->getY1Pos() * _cellSize + _cellSize / 4,
				_cellSize - _cellSize / 2,
				_cellSize-_cellSize / 2);
			painter.drawRect(newr1);
		}
	}

	// Draw an 'i' in all initilized squares
	for(int i = 0; i < squareHasInitialized.size(); i++)
	{
		if(squareHasInitialized[i])
		{
			int y = i / grid_->getHeight();
			int x = i % grid_->getHeight();
			painter.setBrush(Qt::green);
			QFont *font = new QFont();
			font->setPointSize(16);
			font->setStyleHint(QFont::OldEnglish);
			painter.setFont(*font);
			painter.drawText(x * _cellSize, y * _cellSize, _cellSize, _cellSize, Qt::AlignCenter, "i");
		}
	}

	delete map;
	painter.end();
}