QPointF ImageMarker::getLimitPos(QPointF newPos) { qreal loLen; qreal hiLen; if (!m_limit) { return QPointF(); } QVector2D ownVector = QVector2D(newPos); QVector2D limVector = QVector2D(m_limit->pos()); if (m_outerLimit) { loLen = ownVector.length(); hiLen = limVector.length(); } else { loLen = limVector.length(); hiLen = ownVector.length(); } if (loLen <= hiLen) { return QPointF(); } return (ownVector.normalized() * (m_outerLimit ? hiLen : loLen)).toPointF(); }
void LiftDragPlot::mouseMoveEvent( QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint pos = event->pos(); QVector2D diff = QVector2D(pos - mBeginPos); if (diff.length() > selectionTolerance()) { mDragging = true; } if (mDragging) { const double cd = xAxis->pixelToCoord(pos.x()); const double cl = yAxis->pixelToCoord(pos.y()); const double c = cd / 2; const double a = c / cl / cl; mMainWindow->setMinDrag(c); mMainWindow->setMaxLD(1 / sqrt(4 * a * c)); } } else if (QCPCurve *graph = qobject_cast<QCPCurve *>(plottable(0))) { const QCPCurveDataMap *data = graph->data(); double resultTime; double resultDistance = std::numeric_limits<double>::max(); for (QCPCurveDataMap::const_iterator it = data->constBegin(); it != data->constEnd(); ++it) { QVector2D pt = QVector2D(xAxis->coordToPixel(it.value().key), yAxis->coordToPixel(it.value().value)); double dist = pt.distanceToPoint(QVector2D(event->pos())); if (dist < resultDistance) { resultTime = it.value().t; resultDistance = dist; } } if (resultDistance < selectionTolerance()) { setMark(resultTime); } else { mMainWindow->clearMark(); QToolTip::hideText(); } } }
bool B9SupportStructure::IsVertical() { QVector2D disp = QVector2D(topPivot) - QVector2D(bottomPivot); if(disp.length() < 0.1) return true; return false; }
double Torus::F(const QVector3D &pp) { QVector3D p = pp - position; QVector2D p2 = QVector2D(p.x(), p.z()); QVector2D q = QVector2D(p2.length() - t.x(), p.y()); return q.length() - t.y(); }
/** * Computes the intersection between two line segments on the XY plane * Segments must intersect within their extents for the intersection to be valid * z coordinate is ignored **/ bool Polygon3D::segmentSegmentIntersectXY(QVector2D &a, QVector2D &b, QVector2D &c, QVector2D &d, float *tab, float *tcd, bool segmentOnly, QVector2D &intPoint) { QVector2D u = b - a; QVector2D v = d - c; if( u.lengthSquared() < MTC_FLOAT_TOL || v.lengthSquared() < MTC_FLOAT_TOL ) { return false; } float numer = v.x()*(c.y()-a.y()) + v.y()*(a.x()-c.x()); float denom = u.y()*v.x() - u.x()*v.y(); if (denom == 0.0f) { // they are parallel *tab = 0.0f; *tcd = 0.0f; return false; } float t0 = numer / denom; QVector2D ipt = a + t0*u; QVector2D tmp = ipt - c; float t1; if (QVector2D::dotProduct(tmp, v) > 0.0f){ t1 = tmp.length() / v.length(); } else { t1 = -1.0f * tmp.length() / v.length(); } //Check if intersection is within segments if( !( (t0 >= MTC_FLOAT_TOL) && (t0 <= 1.0f-MTC_FLOAT_TOL) && (t1 >= MTC_FLOAT_TOL) && (t1 <= 1.0f-MTC_FLOAT_TOL) ) ){ return false; } *tab = t0; *tcd = t1; QVector2D dirVec = b-a; intPoint = a+(*tab)*dirVec; return true; }
void CFruchtermanReingold::calculate_coordinates() { CalculateForces(); for(int i = 0; i < vgc_nodes_num; ++i) { if(!vgc_graph->vertice_exists(i)) continue; QVector2D delta = vgc_vertices[i].v_force * NODE_MASS * TIME_DELTA * TIME_DELTA; vgc_vertices[i].v_coordinates += delta.toPoint() * std::min(delta.length(), (double)vgc_temperature) / delta.length(); } Cool(); }
float Window::projection_on_curve(const QVector2D& projected) { //This is the distance where the curves changed in terms of the window size const float radius = 0.8f; float z = 0.0f; if (projected.lengthSquared() <= (0.5f * radius * radius)) { //Inside the sphere z = sqrt(radius * radius - projected.lengthSquared()); } else { //Outside of the sphere using hyperbolic sheet z = (0.5f * radius * radius) / projected.length(); } return z; }
QVector2D Boids::obstacleForce(std::vector<Boids*> obstacles) const { QVector2D dist; QVector2D meanPos; QVector2D force; if(obstacles.size() == 0) return QVector2D(0,0); for(unsigned int i(0);i<obstacles.size();i++) { dist = this->getPos() - obstacles[i]->getPos(); //differentiating big or small obstacle if(obstacles[i]->isItAnObstacle() == SMALL) { if(dist.length() < OBST_SMALL_RAD + OBST_DIST) meanPos += dist; } else { if(dist.length() < OBST_BIG_RAD + OBST_DIST) meanPos += dist; } } //multiplying by obstacle force coeffcient meanPos = meanPos*OBSTACLE_FORCE; //Make the force tengent to the obstacle force = QVector2D(meanPos.y(),-meanPos.x()); return force; }
void GLWidget::mouseReleaseEvent(QMouseEvent *e) { // Mouse release position - mouse press position QVector2D diff = QVector2D(e->pos()) - mousePressPosition; // Rotation axis is perpendicular to the mouse position difference // vector QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized(); // Accelerate angular speed relative to the length of the mouse sweep qreal acc = diff.length() / 100.0; // Calculate new rotation axis as weighted sum rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized(); // Increase angular speed angularSpeed += acc; }
void JaguarView::mouseReleaseEvent(QMouseEvent *event) { QPoint pos; QVector2D v; switch(m_mode) { case MODE_RULER: pos = ((QPointF) mapToScene(event->pos())).toPoint(); m_scaleLineItem->setLine(ptMouseDown.x(), ptMouseDown.y(), pos.x(), pos.y()); v = QVector2D(pos - ptMouseDown); scene()->removeItem(m_scaleLineItem); emit rulerDone(v.length()); break; default: break; } return QGraphicsView::mouseReleaseEvent(event); }
void OpenGLWidget::mouseReleaseEvent(QMouseEvent *e) { QApplication::restoreOverrideCursor(); if (e->button() == Qt::RightButton) { // Mouse release position - mouse press position QVector2D diff = QVector2D(e->localPos()) - m_lastMousePosition; // Rotation axis is perpendicular to the mouse position difference vector QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized(); // Accelerate angular speed relative to the length of the mouse sweep qreal acc = diff.length() / 100.0; // Calculate new rotation axis as weighted sum m_rotationAxis = (m_rotationAxis * m_angularSpeed + n * acc).normalized(); // Increase angular speed m_angularSpeed += acc; } e->accept(); }
void MeshViewer::mouseMoveEvent(QMouseEvent *e) { switch (interactionState) { case Camera: { if (e->buttons() & Qt::LeftButton) { QVector2D diff = QVector2D(e->pos()) - mouseState.prev_pos; if ((e->modifiers() & Qt::ShiftModifier)) { //press "shift" key viewerState.translation += QVector3D(diff.x() / 100.0, -diff.y() / 100.0, 0.0); } else if (e->modifiers() & Qt::ControlModifier) //press "crl" key { viewerState.translation += QVector3D(0.0, 0.0, diff.x() / 100.0 - diff.y() / 100.0); } else{ // Rotation axis is perpendicular to the mouse position difference // vector QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized(); // Accelerate angular speed relative to the length of the mouse sweep qreal acc = diff.length() / 4.0; // Calculate new rotation axis as weighted sum viewerState.rotationAxis = (viewerState.rotationAxis * viewerState.angularChange + n * acc).normalized(); // Change rotation angle viewerState.angularChange = acc; viewerState.rotation = QQuaternion::fromAxisAndAngle(viewerState.rotationAxis, viewerState.angularChange) * viewerState.rotation; } viewerState.updateModelView(); mouseState.prev_pos = QVector2D(e->pos()); } updateGL(); break; } case Camera_Translation: if (e->buttons() & Qt::LeftButton) { QVector2D diff = QVector2D(e->pos()) - mouseState.prev_pos; viewerState.translation += QVector3D(diff.x() / 100.0, -diff.y() / 100.0, 0.0); viewerState.updateModelView(); mouseState.prev_pos = QVector2D(e->pos()); } updateGL(); break; case Camera_Zoom: if (e->buttons() & Qt::LeftButton) { QVector2D diff = QVector2D(e->pos()) - mouseState.prev_pos; viewerState.translation += QVector3D(0.0, 0.0, diff.x() / 100.0 - diff.y() / 100.0); viewerState.updateModelView(); mouseState.prev_pos = QVector2D(e->pos()); } updateGL(); break; //selection box case SelectFace: case SelectEdge: case SelectVertex: { if (mouseState.isPressed) { isSelecting = true; sbox.corner_win[2] = e->x(); sbox.corner_win[3] = viewerState.viewport.h - e->y(); /* cout<<"e->x(win[2])"<<endl<<e->x()<<endl; cout<<"e->y(win[3])"<<endl<<e->y()<<endl; viewerState.print(); */ computeGlobalSelectionBox(); } else { isSelecting = false; mouseState.isPressed = false;//later added } // cout<<"moving mousestate"<<mouseState.isPressed<<endl; updateGL(); break; } } }
/** * オリジナルの座標系での頂点座標v1とv2を通る直線を、縮尺された座標系のhtSpace上に描画する。 */ void HoughTransform::line(const QVector2D& p1, const QVector2D& p2, float sigma) { QVector2D v1 = (p1 - bbox.minPt) * scale; QVector2D v2 = (p2 - bbox.minPt) * scale; QVector2D vl, vr; if (v1.x() < v2.x()) { vl = v1; vr = v2; } else { vl = v2; vr = v1; } QVector2D vu, vd; if (v1.y() < v2.y()) { vd = v1; vu = v2; } else { vd = v2; vu = v1; } QVector2D dir = v2 - v1; float len = dir.length(); sigma *= scale; float sigma2 = SQR(sigma); if (fabs(dir.x()) > fabs(dir.y())) { for (int x = 0; x < htSpace.cols; x++) { int y = dir.y() * ((float)x - v1.x()) / dir.x() + v1.y() + 0.5f; if (y < 0 || y >= htSpace.rows) continue; float h = 0; if (x >= vl.x() && x <= vr.x()) { h = len; } else if (x < vl.x()) { h = len * expf(-(SQR(x - vl.x()) + SQR(y - vl.y())) * 0.5f / sigma2); } else { h = len * expf(-(SQR(x - vr.x()) + SQR(y - vr.y())) * 0.5f / sigma2); } htSpace.at<float>(y, x) += h; } } else { for (int y = 0; y < htSpace.rows; y++) { int x = dir.x() * ((float)y - v1.y()) / dir.y() + v1.x() + 0.5f; if (x < 0 || x >= htSpace.cols) continue; float h = 0; if (y >= vd.y() && y <= vu.y()) { h = len; } else if (y < vd.y()) { h = len * expf(-(SQR(x - vd.x()) + SQR(y - vd.y())) * 0.5f / sigma2); } else { h = len * expf(-(SQR(x - vu.x()) + SQR(y - vu.y())) * 0.5f / sigma2); } htSpace.at<float>(y, x) += h; } } }
/** * カーテシアン座標系から、極座標系へ変換する。 */ void Util::cartesian2polar(const QVector2D &pt, float &radius, float &theta) { radius = pt.length(); theta = atan2f(pt.y(), pt.x()); }
void ball::advance(int phase) { if(!phase) return; QPointF next_point=mapToParent(xComponent,yComponent); qreal next_x = next_point.x(); qreal next_y = next_point.y(); qreal new_x = next_x; qreal new_y = next_y; qreal width = t->getWidth(); qreal height = t->getHeight(); qreal penWidth = t->getPenWidth(); qreal widthBound = width-radius-penWidth/2; qreal heightBound = height-radius-penWidth/2; // Checking if the ball hit the boundary of the table while(XOffBoundary(t->getWidth(), new_x) || YOffBoundary(t->getHeight(), new_y)) { qreal speed = qSqrt(qPow(xComponent,2)+qPow(yComponent,2)); if(speed > this->explosionSpeed) { this->explode(); delete this; return; } chomp->play(":/sounds/spring.wav"); //right boundary if(next_x >= widthBound){ new_x = 2*widthBound - next_x; xComponent= -xComponent; } //left boundary if(next_x <= (radius+penWidth/2)){ new_x = 2*(radius+penWidth/2)-next_x; xComponent= -xComponent; } //bottom boundary if(next_y >= heightBound){ new_y = 2*heightBound - next_y; yComponent= -yComponent; } //top boundary if(next_y <= (radius+penWidth/2)){ new_y = 2*(radius+penWidth/2)-next_y; yComponent= -yComponent; } qreal cFriction = t->getCushionFriction(); CushionEnergyLoss(cFriction); } //Now, check if the ball is colliding with an item other than the table //If we are colliding with something QList<QGraphicsItem *> colliders = collidingItems(); if(colliders.size() != 0) { // if the collision speed is higher than the threshold set in the config file // explode! qreal speed = qSqrt(qPow(xComponent,2)+qPow(yComponent,2)); if(speed > this->explosionSpeed) { this->explode(); delete this; return; } // otherwise check if the ball is colliding with some objects on the table else { qreal x = pos().x(); qreal y = pos().y(); QVector2D ballvec = QVector2D(x, y); QVector2D ballvelocity = QVector2D(this->getXVel(), this->getYVel()); //For every item that we collide with for(QList<QGraphicsItem *>::iterator iter = colliders.begin(); iter != colliders.end(); ++iter) { ball * other = dynamic_cast<ball *> (*iter); Block *block = dynamic_cast<Block *>(*iter); residue *r = dynamic_cast<residue *> (*iter); blackHole *bHole = dynamic_cast<blackHole *>(*iter); if(r) // if roll over residue { qreal tFriction = t->getTableFriction(); FrictionMovement(tFriction); } //If we didn't collide with a ball, see if we collided with a block. Collisions must be handled differently since blocks are fixed objects, and balls can move. if (block) { chomp->play(":/sounds/spring.wav"); //if we're colliding vertically, switch the ball's y component if( (x + radius > block->getXPos() && x + radius < block->getXPos() + block->getWidth()) && (y < block->getYPos() || y > block->getYPos() + block->getHeight()) ) yComponent = -yComponent; else //otherwise we're colliding horizontally, and reverse the y component xComponent = -xComponent; } else if(bHole) // if approaching black hole { // if the ball has rotated for quite a while, just delete the ball. if(this->angle >= M_PI) { delete this; return; } if(playBlackHoleSound) { chomp->play(":/sounds/whip.wav"); this->playBlackHoleSound = false; } qreal r = bHole->getRadius(); qreal b_x = bHole->getXPos(), b_y = bHole->getYPos(); // The ball moves in circular motion setPos(b_x + cos(this->angle) * r, b_y + sin(this->angle) * r); this->angle += bHole->returnPower(); return; } else if(other) //otherwise, we're colliding with a ball { chomp->play(":/sounds/clang.wav"); qreal otherX = other->pos().x(); qreal otherY = other->pos().y(); //Make vectors representing our velocity and the item's velocity QVector2D othervec = QVector2D(otherX, otherY); QVector2D collision = ballvec - othervec; QVector2D othervelocity = QVector2D(other->getXVel(), other->getYVel()); qreal distance = collision.length(); //if the ball is inside another object due to going too fast.. if (distance <= this->getRadius()) { qDebug()<< "inside another object!" << other; collision = QVector2D(1,0); // make sure not to divide by 0 distance = 1 ; //hack to make sure ball doesn't get stuck inside things } //calculate the components of the velocity vectors paralell to the collision collision = collision / distance; // normalise the collision vector qreal ballCollisionVelocityInitial = QVector2D::dotProduct(ballvelocity, collision); qreal otherCollisionVelocityInitial = QVector2D::dotProduct(othervelocity, collision); //assume the objects collide elastically, so the new velocities are given by... qreal ballCollisionVelocityFinal = otherCollisionVelocityInitial; qreal otherCollisionVelocityFinal = ballCollisionVelocityInitial; QVector2D newVelVec = collision *(ballCollisionVelocityFinal - ballCollisionVelocityInitial); this->setXVel(this->getXVel() + newVelVec.x()); this->setYVel(this->getYVel() + newVelVec.y()); QVector2D newOtherVelVec = collision * (otherCollisionVelocityFinal - otherCollisionVelocityInitial); other->setXVel(other->getXVel() + newOtherVelVec.x()); other->setYVel(other->getYVel() + newOtherVelVec.y()); } } } } setPos(this->pos().x() + this->getXVel(), this->pos().y()+ this->getYVel()); //friction slow down qreal tFriction = t->getTableFriction(); FrictionMovement(tFriction); //get the current speed of the ball qreal speed = qSqrt(qPow(xComponent,2)+qPow(yComponent,2)); if(speed<0.1) { xComponent = 0; yComponent = 0; } }