void GameEngine::renderFogOfWar() { Point heroLoc = lib.res() / 2; int radius = 8; vector<Point> circPoints; //Find the circumference of the circle around the hero Point prevPoint(0, 0); for (double rad = 0; rad < pi / 4; rad += 0.02) { double xTrig = (double)radius * cos(rad); double yTrig = (double)radius * sin(rad); int x = heroLoc.x() + xTrig; int y = heroLoc.y() + yTrig; Point p(x, y); if (p != prevPoint) { circPoints.push_back(p); prevPoint = p; } } for (int octant = 0; octant < 8; octant++) { for (unsigned int i = 0; i < circPoints.size(); i++) { drawBressenhamLine(octant, circPoints[i]); } } }
void IntonationWidget::smoothPoints(QPainter& painter) { if (intonationPointList_.size() < 2U) return; for (unsigned int i = 0, end = intonationPointList_.size() - 1; i < end; ++i) { const auto& point1 = intonationPointList_[i]; const auto& point2 = intonationPointList_[i + 1]; double x1 = point1.absoluteTime(); double y1 = point1.semitone(); double m1 = point1.slope(); double x2 = point2.absoluteTime(); double y2 = point2.semitone(); double m2 = point2.slope(); double x12 = x1 * x1; double x13 = x12 * x1; double x22 = x2 * x2; double x23 = x22 * x2; double denominator = x2 - x1; denominator = denominator * denominator * denominator; double d = (-(y2 * x13) + 3.0 * y2 * x12 * x2 + m2 * x13 * x2 + m1 * x12 * x22 - m2 * x12 * x22 - 3.0 * x1 * y1 * x22 - m1 * x1 * x23 + y1 * x23) / denominator; double c = (-(m2 * x13) - 6.0 * y2 * x1 * x2 - 2.0 * m1 * x12 * x2 - m2 * x12 * x2 + 6.0 * x1 * y1 * x2 + m1 * x1 * x22 + 2.0 * m2 * x1 * x22 + m1 * x23) / denominator; double b = (3.0 * y2 * x1 + m1 * x12 + 2.0 * m2 * x12 - 3.0 * x1 * y1 + 3.0 * x2 * y2 + m1 * x1 * x2 - m2 * x1 * x2 - 3.0 * y1 * x2 - 2.0 * m1 * x22 - m2 * x22) / denominator; double a = (-2.0 * y2 - m1 * x1 - m2 * x1 + 2.0 * y1 + m1 * x2 + m2 * x2) / denominator; QPointF prevPoint(0.5 + timeToX(x1), 0.5 + valueToY(y1)); for (unsigned int j = static_cast<unsigned int>(x1); j <= static_cast<unsigned int>(x2); j += SMOOTH_POINTS_X_INCREMENT) { double x = j; double y = x * (x * (x * a + b) + c) + d; QPointF currPoint(0.5 + timeToX(x), 0.5 + valueToY(y)); painter.drawLine(prevPoint, currPoint); prevPoint = currPoint; } QPointF lastPoint(0.5 + timeToX(x2), 0.5 + valueToY(y2)); painter.drawLine(prevPoint, lastPoint); } }
void drawTexturePoly(cocos2d::CCTexture2D * tex, const cocos2d::CCPoint * poli, const unsigned int numberOfPoints, const bool preferHorizontal, const float texScale){ const float m_uPixelsWide(tex->getPixelsWide()*texScale); const float m_uPixelsHigh(tex->getPixelsHigh()*texScale); const float m_fMaxT(1.f); const float m_fMaxS(1.f); GLfloat coordinates[] = { 0.0f, m_fMaxT, m_fMaxS,m_fMaxT, 0.0f, 0.0f, m_fMaxS,0.0f }; CCPoint halfOffset{-m_uPixelsWide*.5f, -m_uPixelsHigh*.5f}; GLuint m_uName(tex->getName()); CCGLProgram *m_pShaderProgram(tex->getShaderProgram()); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords ); m_pShaderProgram->use(); m_pShaderProgram->setUniformsForBuiltins(); ccGLBindTexture2D( m_uName ); for (int pointI(1); pointI<numberOfPoints; ++pointI) { CCPoint currentPoint(ccpAdd(poli[pointI], halfOffset)); CCPoint prevPoint(ccpAdd(poli[pointI-1], halfOffset)); //CCPoint prevPoint(poli[pointI-1]); const float dist(preferHorizontal? fabsf(currentPoint.x-prevPoint.x) : fabsf(currentPoint.y-prevPoint.y)); const float step(preferHorizontal? m_uPixelsWide : m_uPixelsHigh); for (int i(0); i<dist; i+=step) { float progress(float(i)/dist); CCPoint point{ prevPoint.x+(currentPoint.x-prevPoint.x)*progress , prevPoint.y+(currentPoint.y-prevPoint.y)*progress }; GLfloat width = (GLfloat)m_uPixelsWide * m_fMaxS, height = (GLfloat)m_uPixelsHigh * m_fMaxT; GLfloat vertices[] = { point.x, point.y, width + point.x, point.y, point.x, height + point.y, width + point.x, height + point.y }; glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } } }
void BaseVH::filterContoursByEdgeAngle( std::vector< std::vector< cv::Point2f > > &contours , double angleThreshold ) { int numContours = contours.size(); double thresholdVal = - std::cos( ( angleThreshold / 180 ) * CV_PI ); std::vector< std::vector< cv::Point2f > > filteredContours; double averageContourLength = 0; int totalNumEdges = 0; for( int cc = 0; cc < numContours; cc++ ) { int numEdges = contours[ cc ].size(); std::vector< cv::Point2f > filteredContour; int id1 = -1 , id2 = -1 , id3 = -1; for( int ee = 0; ee < numEdges; ee++ ) { cv::Point2f &pt1 = contours[ cc ][ ( ee - 1 + numEdges ) % numEdges ]; cv::Point2f &pt2 = contours[ cc ][ ee ]; tr::Vector2f vec1( pt2.x - pt1.x , pt2.y - pt1.y ); averageContourLength += vec1.norm(); } totalNumEdges += numEdges; } if( totalNumEdges < 4 ) return; averageContourLength /= totalNumEdges; double lengthThreshold = 1e-4 * averageContourLength; for( int cc = 0; cc < numContours; cc++ ) { int numEdges = contours[ cc ].size(); std::vector< cv::Point2f > filteredContour; int id1 = -1 , id2 = -1 , id3 = -1; cv::Point2f prevPoint( 0 , 0 ); for( int ee = 0; ee < numEdges; ee++ ) { cv::Point2f &pt1 = contours[ cc ][ ( ee - 1 + numEdges ) % numEdges ]; cv::Point2f &pt2 = contours[ cc ][ ee ]; cv::Point2f &pt3 = contours[ cc ][ ( ee + 1 ) % numEdges ]; tr::Vector2f vec1( pt2.x - pt1.x , pt2.y - pt1.y ) , vec2( pt3.x - pt2.x , pt3.y - pt2.y ); vec1.normalize(); vec2.normalize(); double val = vec1.dot( vec2 ); if( val < thresholdVal ) { continue; } if( filteredContour.size() > 0 ) { tr::Vector2f vec( prevPoint.x - pt2.x , prevPoint.y - pt2.y ); if( vec.norm() < lengthThreshold ) { continue; } } prevPoint = pt2; filteredContour.push_back( pt2 ); } filteredContours.push_back( filteredContour ); } contours = filteredContours; }
// Note: with no antialiasing, the coordinates in QPointF are rounded to the nearest integer. void IntonationWidget::paintEvent(QPaintEvent*) { if (eventList_ == nullptr || eventList_->list().empty()) { return; } QPainter painter(this); painter.setFont(QFont("monospace")); if (modelUpdated_) { QFontMetrics fm = painter.fontMetrics(); textYOffset_ = 0.5 * fm.ascent(); leftMargin_ = MARGIN + TEXT_MARGIN + fm.width(yLabels[0]); modelUpdated_ = false; } totalWidth_ = std::ceil(leftMargin_ + graphWidth_ + 6.0 * MARGIN); if (totalWidth_ < MININUM_WIDTH) { totalWidth_ = MININUM_WIDTH; } totalHeight_ = std::ceil(2.0 * MARGIN + 3.0 * TRACK_HEIGHT + 15.0 * yStep_); if (totalHeight_ < MININUM_HEIGHT) { totalHeight_ = MININUM_HEIGHT; } setMinimumWidth(totalWidth_); setMinimumHeight(totalHeight_); double xStart = timeToX(0.0); double yStart = valueToY(MIN_VALUE); double xEnd = timeToX(maxTime_); double yEnd = valueToY(MAX_VALUE); // Y labels. for (unsigned int i = 0; i < yLabels.size(); ++i) { double y = MARGIN + (i + 3) * yStep_ + textYOffset_; painter.drawText(QPointF(MARGIN, y), yLabels[i]); } // Horizontal lines. painter.setPen(Qt::lightGray); for (int i = 1; i < 15; ++i) { double y = MARGIN + (i + 3) * yStep_; painter.drawLine(QPointF(xStart, y), QPointF(xEnd, y)); } painter.setPen(Qt::black); postureTimeList_.clear(); double yPosture = MARGIN + 3.0 * TRACK_HEIGHT - 0.5 * (TRACK_HEIGHT - 1.0) + textYOffset_; unsigned int postureIndex = 0; for (const TRMControlModel::Event_ptr& ev : eventList_->list()) { double x = timeToX(ev->time); if (ev->flag) { postureTimeList_.push_back(ev->time); const TRMControlModel::Posture* posture = eventList_->getPostureAtIndex(postureIndex++); if (posture) { painter.setPen(Qt::black); // Posture name. painter.drawText(QPointF(x, yPosture), posture->name().c_str()); } painter.setPen(Qt::lightGray); // Event vertical line. painter.drawLine(QPointF(x, yStart), QPointF(x, yEnd)); } } painter.setPen(Qt::black); // Frame. painter.drawRect(QRectF(QPointF(xStart, yStart), QPointF(xEnd, yEnd))); double yRuleText = MARGIN + TRACK_HEIGHT - 0.5 * (TRACK_HEIGHT - 1.0) + textYOffset_; double yRuleText2 = yRuleText + TRACK_HEIGHT; for (int i = 0; i < eventList_->numberOfRules(); ++i) { auto* ruleData = eventList_->getRuleAtIndex(i); if (ruleData) { unsigned int firstPosture = ruleData->firstPosture; unsigned int lastPosture = ruleData->lastPosture; int postureTime1, postureTime2; if (firstPosture < postureTimeList_.size()) { postureTime1 = postureTimeList_[firstPosture]; } else { postureTime1 = 0; // invalid } if (lastPosture < postureTimeList_.size()) { postureTime2 = postureTimeList_[lastPosture]; } else { postureTime2 = postureTime1 + ruleData->duration; } // Rule frame. painter.drawRect(QRectF( QPointF(timeToX(postureTime1), MARGIN), QPointF(timeToX(postureTime2), MARGIN + 2.0 * TRACK_HEIGHT))); // Rule number. painter.drawText(QPointF(timeToX(postureTime1) + TEXT_MARGIN, yRuleText), QString::number(ruleData->number)); // Rule duration. painter.drawText(QPointF(timeToX(postureTime1) + TEXT_MARGIN, yRuleText2), QString::number(ruleData->duration, 'f', 0)); } } QPen pen; QPen pen2; pen2.setWidth(2); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(QBrush(Qt::black)); // Lines between intonation points. QPointF prevPoint(0.5 + xStart, 0.5 + yStart); for (unsigned int i = 0, size = intonationPointList_.size(); i < size; ++i) { QPointF currPoint( 0.5 + timeToX(intonationPointList_[i].absoluteTime()), 0.5 + valueToY(intonationPointList_[i].semitone())); painter.setPen(pen2); painter.drawLine(prevPoint, currPoint); painter.setPen(pen); drawPointMarker(painter, 0.5 + currPoint.x(), 0.5 + currPoint.y()); prevPoint = currPoint; } painter.setBrush(QBrush()); painter.setPen(pen2); smoothPoints(painter); painter.setPen(pen); painter.setRenderHint(QPainter::Antialiasing, false); // Point selection. if (selectedPoint_ >= 0) { double x = timeToX(intonationPointList_[selectedPoint_].absoluteTime()); double y = valueToY(intonationPointList_[selectedPoint_].semitone()); painter.drawRect(QRectF( QPointF(x - SELECTION_SIZE, y - SELECTION_SIZE), QPointF(x + SELECTION_SIZE, y + SELECTION_SIZE))); } // Beat lines. for (unsigned int i = 0, size = intonationPointList_.size(); i < size; ++i) { double x = timeToX(intonationPointList_[i].beatTime()); painter.drawLine(QPointF(x, yStart), QPointF(x, yEnd)); } // Time scale. double yTick1 = yStart + TIME_DIVISION_MARK_SIZE; double yTick2 = yStart + 2.0 * TIME_DIVISION_MARK_SIZE; double yTimeText = yTick2 + 0.5 * TRACK_HEIGHT + textYOffset_; for (int time = 0, end = static_cast<int>(maxTime_); time <= end; time += 10) { double x = timeToX(time); if (time % 100 == 0) { painter.drawLine(QPointF(x, yStart), QPointF(x, yTick2)); painter.drawText(QPointF(x, yTimeText), QString::number(time)); } else { painter.drawLine(QPointF(x, yStart), QPointF(x, yTick1)); } } }