Skip to content

Instantly share code, notes, and snippets.

@Glavak
Created April 19, 2018 07:18
Show Gist options
  • Save Glavak/4af09678c64e3fc873f0f678de43096a to your computer and use it in GitHub Desktop.
Save Glavak/4af09678c64e3fc873f0f678de43096a to your computer and use it in GitHub Desktop.
void IsoLinesWidget::paintIsoLines(QPainter & painter)
{
QSizeF squareSize((float)imageRect.width() / widthGridCount, (float)imageRect.height() / heightGridCount);
QVector<float> topLine(widthGridCount + 1);
QVector<float> bottomLine(widthGridCount + 1);
for (int x = 0; x < widthGridCount + 1; ++x)
{
QPointF coords = QPointF(squareSize.width()*x, 0);
bottomLine[x] = function(imageToFunction(coords));
}
for (int squareY = 0; squareY < heightGridCount; ++squareY)
{
topLine.swap(bottomLine);
for (int x = 0; x < widthGridCount + 1; ++x)
{
QPointF coords = QPointF(squareSize.width()*x, squareSize.height()*(squareY+1));
bottomLine[x] = function(imageToFunction(coords));
}
for (int squareX = 0; squareX < widthGridCount; ++squareX)
{
float f1 = topLine[squareX];
float f2 = topLine[squareX+1];
float f3 = bottomLine[squareX];
float f4 = bottomLine[squareX+1];
float f5 = function(imageToFunction(QPointF(squareSize.width()*(squareX+0.5f), squareSize.height()*(squareY+0.5f))));
painter.setPen(qRgb(93, 138, 138));
for (int levelNum = 0; levelNum < userNumbers.count(); ++levelNum)
{
paintIsoLine(f1,f2,f3,f4,f5, userNumbers[levelNum], QPoint(squareX, squareY), squareSize, painter);
}
painter.setPen(linesColor);
for (int levelNum = 0; levelNum < legendNumbers.count(); ++levelNum)
{
paintIsoLine(f1,f2,f3,f4,f5, legendNumbers[levelNum], QPoint(squareX, squareY), squareSize, painter);
}
if (hoverLineDrawing)
{
painter.setPen(Qt::blue);
paintIsoLine(f1,f2,f3,f4,f5, hoverLineValue, QPoint(squareX, squareY), squareSize, painter);
}
}
}
}
void IsoLinesWidget::paintIsoLine(float f1, float f2, float f3, float f4, float f5, float level, QPoint squareCoords, QSizeF squareSize, QPainter &painter)
{
QVector<QPointF> hits;
float topHit = getHit(f1, f2, level);
float rightHit = getHit(f2, f4, level);
float bottomHit = getHit(f3, f4, level);
float leftHit = getHit(f1, f3, level);
if (topHit >= 0) hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+topHit), squareSize.height()*squareCoords.y()));
if (rightHit >= 0) hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+1), squareSize.height()*(squareCoords.y()+rightHit)));
if (bottomHit >= 0) hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+bottomHit), squareSize.height()*(squareCoords.y()+1)));
if (leftHit >= 0) hits.push_back(QPointF(squareSize.width()*squareCoords.x(), squareSize.height()*(squareCoords.y()+leftHit)));
if (hits.isEmpty()) return;
if (showLines)
{
if (hits.count() == 2) painter.drawLine(imageRect.topLeft()+hits[0], imageRect.topLeft()+hits[1]);
if (hits.count() == 3)
{
float epsilon = 0.01f;
paintIsoLine(f1,f2,f3,f4,f5, hoverLineValue+epsilon, squareCoords, squareSize, painter);
}
if (hits.count() == 4)
{
float TLhit = getHit(f1, f5, level);
float TRhit = getHit(f2, f5, level);
float BLhit = getHit(f3, f5, level);
float BRhit = getHit(f4, f5, level);
if (TLhit > -1)
{
hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+TLhit/2), squareSize.height()*(squareCoords.y()+TLhit/2)));
painter.drawLine(imageRect.topLeft()+hits[0], imageRect.topLeft()+hits[hits.count()-1]);
painter.drawLine(imageRect.topLeft()+hits[3], imageRect.topLeft()+hits[hits.count()-1]);
}
if (TRhit > -1)
{
hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+1-TRhit/2), squareSize.height()*(squareCoords.y()+TRhit/2)));
painter.drawLine(imageRect.topLeft()+hits[0], imageRect.topLeft()+hits[hits.count()-1]);
painter.drawLine(imageRect.topLeft()+hits[1], imageRect.topLeft()+hits[hits.count()-1]);
}
if (BLhit > -1)
{
hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+BLhit/2), squareSize.height()*(squareCoords.y()+1-BLhit/2)));
painter.drawLine(imageRect.topLeft()+hits[2], imageRect.topLeft()+hits[hits.count()-1]);
painter.drawLine(imageRect.topLeft()+hits[3], imageRect.topLeft()+hits[hits.count()-1]);
}
if (BRhit > -1)
{
hits.push_back(QPointF(squareSize.width()*(squareCoords.x()+1-BRhit/2), squareSize.height()*(squareCoords.y()+1-BRhit/2)));
painter.drawLine(imageRect.topLeft()+hits[2], imageRect.topLeft()+hits[hits.count()-1]);
painter.drawLine(imageRect.topLeft()+hits[1], imageRect.topLeft()+hits[hits.count()-1]);
}
}
}
if (showEntryPoints)
{
painter.setBrush(QBrush(Qt::red, Qt::SolidPattern));
foreach (QPointF hit, hits) painter.drawEllipse(imageRect.topLeft()+hit, 3, 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment