// DkPongPort -------------------------------------------------------------------- DkPongPort::DkPongPort(QWidget *parent, Qt::WindowFlags flags) : QGraphicsView(parent) { //setAttribute(Qt::WA_TranslucentBackground, true); fieldColor = QColor(0,0,0); playerColor = QColor(255, 255, 255); unit = 10; playerSpeed = unit; minBallSpeed = 0.5*unit; maxBallSpeed = 4*unit; ballDir = DkVector(unit*0.15, unit*0.15); player1Speed = 0; player2Speed = 0; player1Pos = INT_MAX; player2Pos = INT_MAX; ball = QRect(QPoint(), QSize(unit, unit)); player1 = QRect(QPoint(), QSize(unit, 2*unit)); player2 = QRect(QPoint(), QSize(unit, 5*unit)); initGame(); eventLoop = new QTimer(this); eventLoop->setInterval(10); eventLoop->start(); connect(eventLoop, SIGNAL(timeout()), this, SLOT(gameLoop())); //show(); }
void DkRotatingRect::getTransform(QTransform& tForm, QPointF& size) const { if (rect.size() < 4) return; // default upper left corner is 0 DkVector xV = DkVector(rect[3] - rect[0]).round(); DkVector yV = DkVector(rect[1] - rect[0]).round(); QPointF ul = QPointF(qRound(rect[0].x()), qRound(rect[0].y())); size = QPointF(xV.norm(), yV.norm()); qDebug() << xV.toQPointF(); qDebug() << "size: " << size; double angle = xV.angle(); angle = DkMath::normAngleRad(angle, -CV_PI, CV_PI); if (std::abs(angle) > DBL_EPSILON) qDebug() << "angle is > eps..."; // switch width/height for /\ and \/ quadrants if (std::abs(angle) > CV_PI*0.25 && std::abs(angle) < CV_PI*0.75) { float x = (float)size.x(); size.setX(size.y()); size.setY(x); } // invariance -> user does not want to make a difference between an upside down rect if (angle > CV_PI*0.25 && angle < CV_PI*0.75) { angle -= CV_PI*0.5; ul = rect[1]; } else if (angle > -CV_PI*0.75 && angle < -CV_PI*0.25) { angle += CV_PI*0.5; ul = rect[3]; } else if (angle >= CV_PI*0.75 || angle <= -CV_PI*0.75) { angle += CV_PI; ul = rect[2]; } tForm.rotateRadians(-angle); tForm.translate(qRound(-ul.x()), qRound(-ul.y())); // round guarantees that pixels are not interpolated }
void DkPongPort::initGame() { ballDir = DkVector(unit*0.5, unit*0.5); ball.moveCenter(QPoint(width()*0.5, height()*0.5)); player1.moveCenter(QPoint(unit, height()*0.5)); player2.moveCenter(QPoint(width()-unit*1.5, height()*0.5)); }
void DkPongPort::initGame() { ballDir = DkVector(unit*0.5f, unit*0.5f); ball.moveCenter(QPoint(qRound(width()*0.5f), qRound(height()*0.5f))); player1.moveCenter(QPoint(unit, qRound(height()*0.5f))); player2.moveCenter(QPoint(qRound(width()-unit*1.5f), qRound(height()*0.5f))); }
std::ostream& DkRotatingRect::put(std::ostream& s) { s << "DkRotatingRect: "; for (int idx = 0; idx < rect.size(); idx++) { DkVector vec = DkVector(rect[idx]); s << vec << ", "; } return s; }
void DkCropToolBar::on_horValBox_valueChanged(double) { DkVector diag = DkVector((float)horValBox->value(), (float)verValBox->value()); emit aspectRatio(diag); QString rs = QString::number(horValBox->value()) + ":" + QString::number(verValBox->value()); int idx = ratioBox->findText(rs); if (idx != -1) ratioBox->setCurrentIndex(idx); else if (horValBox->value() == 0 && verValBox->value() == 0) ratioBox->setCurrentIndex(0); else ratioBox->setCurrentIndex(1); }
void DkRotatingRect::updateCorner(int cIdx, QPointF nC, DkVector oldDiag) { // index does not exist if (cIdx < 0 || cIdx >= rect.size()*2) return; if (rect[(cIdx+1) % 4] == rect[(cIdx+3) % 4]) { QPointF oC = rect[(cIdx+2) % 4]; // opposite corner rect[cIdx] = nC; rect[(cIdx+1) % 4] = QPointF(nC.x(), oC.y()); rect[(cIdx+3) % 4] = QPointF(oC.x(), nC.y()); } // these indices indicate the control points on edges else if (cIdx >= 4 && cIdx < 8) { DkVector c0 = rect[cIdx % 4]; DkVector n = (rect[(cIdx+1) % 4] - c0).normalVec(); n.normalize(); // compute the offset vector DkVector oV = n * n.scalarProduct(nC-c0); rect[cIdx % 4] = (rect[cIdx % 4] + oV).toQPointF(); rect[(cIdx+1) % 4] = (rect[(cIdx+1) % 4] + oV).toQPointF(); } else { // we have to update the n-1 and n+1 corner DkVector cN = nC; DkVector c0 = rect[cIdx]; DkVector c1 = rect[(cIdx+1) % 4]; DkVector c2 = rect[(cIdx+2) % 4]; DkVector c3 = rect[(cIdx+3) % 4]; if (!oldDiag.isEmpty()) { DkVector dN = oldDiag.normalVec(); dN.normalize(); float d = dN*(cN-c2); cN += (dN*-d); } // new diagonal float diagLength = (c2-cN).norm(); float diagAngle = (float)(c2-cN).angle(); // compute the idx-1 corner float c1Angle = (float)(c1-c0).angle(); float newLength = cos(c1Angle - diagAngle)*diagLength; DkVector nc1 = DkVector((newLength), 0); nc1.rotate(-c1Angle); // compute the idx-3 corner float c3Angle = (float)(c3-c0).angle(); newLength = cos(c3Angle - diagAngle)*diagLength; DkVector nc3 = DkVector((newLength), 0); nc3.rotate(-c3Angle); rect[(cIdx+1) % 4] = (nc1+cN).toQPointF(); rect[(cIdx+3) % 4] = (nc3+cN).toQPointF(); rect[cIdx] = cN.toQPointF(); } }