Example #1
0
void GuiModel::processFoundToken()
{
    tokensFound++;
    int itt = 0;
    QPointF distance;
    distance.setX(500);
    distance.setY(500);
    QString nearest = "ERROR";

    int dist = 5000;

    for (itt = 0 ; itt < 4 ; itt++){

        if(initList.at(itt).second.x() >= 0){

            distance = initList.at(itt).second - leadRoverPosition;

            if(distance.manhattanLength() < dist){
                nearest = initList.at(itt).first;
                dist = distance.manhattanLength();
            }

        }

    }

    leadRunningTokenError += dist;
    leadTokenErrorDivisor++;

    runStats();

    emit updateLeadRoverTokenStats(nearest, leadRoverPosition);

}
// returns distance to nearest end of line
float ShaderConnectionUI::distanceFromLineEnds(const QPointF& point) const
{
	// currently this is only designed to be used with fully-connected (at both ends)
	// connections - no temporary ones
	QPointF srcPoint;
	QPointF dstPoint;

	// these checks should be redundant as it should be done before this function gets called...

	if (m_pSrcNode && m_srcNodePortIndex != -1)
	{
		srcPoint = m_pSrcNode->getOutputPortPosition(m_srcNodePortIndex);
	}

	if (m_pDstNode && m_dstNodePortIndex != -1)
	{
		dstPoint = m_pDstNode->getInputPortPosition(m_dstNodePortIndex);
	}

	QPointF v = srcPoint - point;
	QPointF u = dstPoint - srcPoint;

	// TODO: strictly-speaking, this isn't correct, and should be the real length, but...
	float length = u.manhattanLength();

	float det = (-v.x() * u.x()) + (-v.y() * u.y());
	if (det < 0.0f || det > length)
	{
		u = dstPoint - point;
		return sqrtf(std::min(v.manhattanLength(), u.manhattanLength()));
	}

	det = u.x() * v.y() - u.y() * v.x();
	return sqrtf((det * det) / length);
}
bool SCgCommandSelectedObjectMove::mergeWith (const QUndoCommand* command)
{
    if (command->id() != id() || childCount() || command->childCount())
        return false;

    const SCgCommandSelectedObjectMove* c = static_cast<const SCgCommandSelectedObjectMove*>(command);
    if(mUndoInfo.keys() != c->mUndoInfo.keys())
        return false;

    qreal maxManhattanLength = 0;

    SCgScene::ObjectUndoInfo::ConstIterator const_it = c->mUndoInfo.begin();
    while(const_it != c->mUndoInfo.end())
    {
        QPointF offset = const_it.value().second.second - const_it.value().first.second;
        if(offset.manhattanLength() > maxManhattanLength)
            maxManhattanLength = offset.manhattanLength();
        ++const_it;
    }

    if(maxManhattanLength > 35)
        return false;

    SCgScene::ObjectUndoInfo::iterator it = mUndoInfo.begin();
    while(it != mUndoInfo.end())
    {
        it.value().second = c->mUndoInfo.value(it.key()).second;
        ++it;
    }
    return true;
}
bool ShaderConnectionUI::didHit(const QPointF& pos, SelectionInfo& selInfo, float& closestDistance) const
{
	if (boundingRect().contains(pos))
	{
		float distance = distanceFromLineEnds(pos);

		// see if we're close to the line now
		if (distance < closestDistance)
		{
			closestDistance = distance;

			// nasty, but...
			selInfo.pConnection = const_cast<ShaderConnectionUI*>(this);

			// this is duplicate work done already in distanceFromLineEnds(), but...

			QPointF srcDelta = m_pSrcNode->getOutputPortPosition(m_srcNodePortIndex) - pos;
			QPointF dstDelta = m_pDstNode->getInputPortPosition(m_dstNodePortIndex) - pos;

			float srcDistance = srcDelta.manhattanLength();
			float dstDistance = dstDelta.manhattanLength();

			if (srcDistance < dstDistance)
			{
				selInfo.wasSource = true;
			}

			return true;
		}
	}

	return false;
}
Example #5
0
void QSplineEdit::selectControlHandle(int x, int y)
{
	m_selected = HNone;
	
	QPointF pmouse(x, y);
	
	QPointF tostart = toDrawSpace(0, m_startValue) - pmouse;
	if(tostart.manhattanLength() < 9.0) {
		m_selected = HStart;
		return;
	}
	
	QPointF toend = toDrawSpace(1, m_endValue) - pmouse;
	if(toend.manhattanLength() < 9.0) {
		m_selected = HEnd;
		return;
	}
	
	QPointF toone = toDrawSpace(m_startCvx, m_startCvy) - pmouse;
	if(toone.manhattanLength() < 9.0) {
		m_selected = HControlLeft;
		return;
	}
	
	QPointF totwo = toDrawSpace(m_endCvx, m_endCvy) - pmouse;
	if(totwo.manhattanLength() < 9.0) {
		m_selected = HControlRight;
		return;
	}
}
Example #6
0
double CubicSegment::minimumDistance(const QPointF &pickPoint, double &tReturnValue) const
{
    double actualMinimumDistance = 10000000.;
    for (double t = 0.0; t <= 1.0; t += 0.1) {
        QPointF samplePoint = sample(t);
        QPointF distanceVector = pickPoint - samplePoint;
        if (distanceVector.manhattanLength() < actualMinimumDistance) {
            actualMinimumDistance = distanceVector.manhattanLength();
            tReturnValue = t;
        }
    }

    return actualMinimumDistance;
}
QPointF AudioBarSpectrumItem::normalFromTangent(const QPointF& tangent) {
    // returns a normalized normal vector given a tangent vector
    if (tangent.manhattanLength() == 0) return QPointF(0, 0);
    QPointF n(tangent.y(), tangent.x() * (-1));
    n /= QVector2D(n).length();
    return n;
}
Example #8
0
void Universe::createIsles(const qreal inUniverseWidth, const qreal inUniverseHeight, const uint inNumIsles)
{
    srand(time(NULL));

    const uint maxWidth = (uint) inUniverseWidth;
    const uint maxHeight = (uint) inUniverseHeight;

    qreal x, y;
    bool tooClose = true;  // pos is too close to another isle
    for(uint i = 0; i < inNumIsles; i++)
    {
        do
        {
            x = rand() % maxWidth;      // random pos
            y = rand() % maxHeight;
            tooClose = false;
            for(Isle *isla : m_isles)
            {
                QPointF p = isla->pos() - QPointF(x, y);
                if(p.manhattanLength() < 50.0f)     // |p.x| + |p.y| < 50 is too close
                {
                    tooClose = true;
                    break;
                }
            }
        } while(tooClose);

        Isle *isle = new Isle(m_lastInsertedId++, Player::PLAYER_UNSETTLED, QPointF(x, y), Player::colorForOwner(Player::PLAYER_UNSETTLED));
        m_isles.append(isle);
    }
}
void KarbonCalligraphicShape::addCap(int index1, int index2, int pointIndex,
                                     bool inverted)
{
    QPointF p1 = m_points[index1]->point();
    QPointF p2 = m_points[index2]->point();

    // TODO: review why spikes can appear with a lower limit
    QPointF delta = p2 - p1;
    if (delta.manhattanLength() < 1.0)
        return;

    QPointF direction = QLineF(QPointF(0, 0), delta).unitVector().p2();
    qreal width = m_points[index2]->width();
    QPointF p = p2 + direction * m_caps * width;

    KoPathPoint * newPoint = new KoPathPoint(this, p);

    qreal angle = m_points[index2]->angle();
    if (inverted)
        angle += M_PI;

    qreal dx = std::cos(angle) * width;
    qreal dy = std::sin(angle) * width;
    newPoint->setControlPoint1(QPointF(p.x() - dx / 2, p.y() - dy / 2));
    newPoint->setControlPoint2(QPointF(p.x() + dx / 2, p.y() + dy / 2));

    insertPoint(newPoint, KoPathPointIndex(0, pointIndex));
}
Example #10
0
void EditorLineItem::endEdition()
{
    // check validity
    QPointF dist = m_line[BEGIN] - m_line[END];
    if (dist.manhattanLength() < 3)
        deleteLater();
}
QPointF NodeConnectionLines::normalFromTangent(const QPointF& tangent) {
    // returns a normalized normal vector given a tangent vector
    if (tangent.manhattanLength() == 0) return QPointF(0, 0);
    QPointF n(tangent.y(), tangent.x() * (-1));
    n /= QVector2D(n).length();
    return n;
}
Example #12
0
bool QKineticScrollerPrivate::moveWhilePressed(QKineticScroller::Input, const QPointF &position, qint64 timestamp)
{
    Q_Q(QKineticScroller);

    QPointF deltaPixel = position - pressPosition;

    bool moveStarted = ((deltaPixel.manhattanLength() / pixelPerMeter) > dragStartDistance);

    if (moveStarted) {
        qreal deltaXtoY = qAbs(pressPosition.x() - position.x()) - qAbs(pressPosition.y() - position.y());
        deltaXtoY /= pixelPerMeter;

        QPointF maxPos = q->maximumContentPosition();
        bool canScrollX = (maxPos.x() > 0);
        bool canScrollY = (maxPos.y() > 0);

        if (hOvershootPolicy == QKineticScroller::OvershootAlwaysOn)
            canScrollX = true;
        if (vOvershootPolicy == QKineticScroller::OvershootAlwaysOn)
            canScrollY = true;

        if (deltaXtoY < 0) {
            if (!canScrollY && (!canScrollX || (-deltaXtoY >= dragStartDirectionErrorMargin)))
                moveStarted = false;
        } else {
            if (!canScrollX && (!canScrollY || (deltaXtoY >= dragStartDirectionErrorMargin)))
                moveStarted = false;
        }
    }

    if (moveStarted) {
        if (cancelPress)
            q->cancelPress(pressPosition);
        setState(QKineticScroller::StateDragging);

        // subtract the dragStartDistance
        deltaPixel = deltaPixel - deltaPixel * (dragStartDistance / deltaPixel.manhattanLength());

        if (!deltaPixel.isNull()) {
            // handleDrag updates lastPosition, lastTimestamp and velocity
            handleDrag(pressPosition + deltaPixel, timestamp);
        }
    }
    return moveStarted;
}
Example #13
0
void GuiModel::processObjectPos(RpiMsg &inmsg){

    uint32_t temp = 0;
    float x;
    float y;
    float angle;
    float length;
    float width;
    double error;
    QPointF position;
    QPointF position_converted;
    QPointF distance;
    QString nearest = "ERROR";
    int dist = 5000;
    distance.setX(500);
    distance.setY(500);

    QStringList myOptions;
    myOptions << "T1" << "T2" << "T3" << "T4" << "O1" << "O2" << "O3" << "O4";

    temp = ( (inmsg.msg[1]) << 8 | (inmsg.msg[2])  );
        x = float((float)temp/10);
    temp = ( (inmsg.msg[3]) << 8 | (inmsg.msg[4])  );
        y = float((float)temp/10);
    temp = ( (inmsg.msg[5]) << 8 | (inmsg.msg[6]) );
        angle = ((float)temp);
    temp = ( (inmsg.msg[7]) << 8 | (inmsg.msg[8]) );
        length = float((float)temp/10);
    temp = ( (inmsg.msg[9]) << 8 | (inmsg.msg[10]) );
        width = float((float)temp/10);

    position.setX(x);
    position.setY(y);

    position_converted.setX(x*10);
    position_converted.setY(y*10);


    switch(inmsg.msg[0]){

        case LEAD:
            //qDebug() << "Leader Information Identified";
            emit updateLeadRover(position, angle);
            leadTracker.push_front(position);
            leadRoverPosition.setX(position_converted.x());
            leadRoverPosition.setY(position_converted.y());
            leadRoverAngle = angle;
        break;

        case FOLLOW:
            //qDebug() << "Follower Information Identified";
            followTracker.push_front(position);
        break;

        case OBSTACLE:
                //qDebug() << "Obs Information Identified";

                for (itt = 4 ; itt < 8 ; itt++){

                    if(initList.at(itt).second.x() > 0){

                        distance = initList.at(itt).second - position_converted;

                        if(distance.manhattanLength() < dist){
                            nearest = initList.at(itt).first;
                            dist = distance.manhattanLength();
                        }
                    }
                }

                if (nearest == "ERROR"){
                    // NO OBSTACLES IN THE INIT LIST
                    qDebug() << "ERROR: POSSIBLY NO OBSTACLES IN GUIMODEL INITLIST";
                }

                //qDebug() << "NEAREST OBS" << myOptions.indexOf(nearest);

                switch(myOptions.indexOf(nearest)){

                case O1:
                    if(initList.at(4).second.x() >= 0){
                        //qDebug() << "O1";
                        emit updateObject("O1", position, 0);
                        error = (initList.at(4).second - position_converted).manhattanLength();
                        sensorRunningError += error;
                        o1RunningError += error;
                        o1errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }
                    break;
                case O2:
                    if(initList.at(5).second.x() >=0){
                        //qDebug() << "O2";
                        emit updateObject("O2", position, 0);
                        error= (initList.at(5).second - position_converted).manhattanLength();
                        sensorRunningError += error;
                        o2RunningError += error;
                        o2errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }
                    break;
                case O3:
                    if(initList.at(6).second.x() >=0){
                        //qDebug() << "O3";
                        emit updateObject("O3", position, 0);
                        error= (initList.at(6).second - position_converted).manhattanLength();
                        sensorRunningError += error;
                        o3RunningError += error;
                        o3errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }
                    break;
                case O4:
                    if(initList.at(7).second.x() >=0){
                        //qDebug() << "O4";
                        emit updateObject("O4", position, 0);
                        error= (initList.at(7).second - position_converted).manhattanLength();
                        sensorRunningError += error;
                        o4RunningError += error;
                        o4errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                        break;
                    }
                default:
                    qDebug() << "Something wrong deciding which obs this is";
                    break;

                }

                break;

        case TOKEN:

                //qDebug() << "Token Information Identified";

                for (itt = 0 ; itt < 4 ; itt++){

                    if(initList.at(itt).second.x() > 0){
                        distance = initList.at(itt).second - position_converted;

                         //qDebug() << "TOKEN POSITION TEST" << initList.at(itt) << distance;

                        if(distance.manhattanLength() < dist){
                            nearest = initList.at(itt).first;
                            dist = distance.manhattanLength();
                        }
                    }
                }

                //qDebug() << "NEAREST TOKEN" << myOptions.indexOf(nearest);

                if (nearest == "ERROR"){
                    // NO TOKENS IN THE INIT LIST
                }

                switch(myOptions.indexOf(nearest)){

                case T1:

                    if(initList.at(0).second.x() >= 0){
                        emit updateObject("T1", position, 0);
                        error= (initList.at(0).second - position*10).manhattanLength();
                        sensorRunningError += error;
                        t1RunningError += error;
                        t1errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }

                    break;
                case T2:

                    if(initList.at(1).second.x() >= 0){
                        emit updateObject("T2", position, 0);
                        error= (initList.at(1).second - position*10).manhattanLength();
                        sensorRunningError += error;
                        t2RunningError += error;
                        t2errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }

                    break;
                case T3:

                    if(initList.at(2).second.x() >=0){
                        emit updateObject("T3", position, 0);
                        error= (initList.at(2).second - position*10).manhattanLength();
                        sensorRunningError += error;
                        t3RunningError += error;
                        t3errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }
                    break;
                case T4:
                    if(initList.at(3).second.x() >= 0){
                        emit updateObject("T4", position, 0);
                        error= (initList.at(3).second - position*10).manhattanLength();
                        sensorRunningError += error;
                        t4RunningError += error;
                        t4errorCt++;
                        if(error >= sensorMaxError){
                            sensorMaxError = error;
                        }
                        sensorDivisor++;
                    }
                    break;
                default:
                    qDebug() << "Something wrong deciding which token this is";
                    break;

                }


                break;


        case ROVER_MOVE:
            startLeadRoverMove(inmsg);
            break;


        default:
        break;
    }

    runStats();

}
Example #14
0
void KisToolFreehandHelper::paintBezierSegment(KisPaintInformation pi1, KisPaintInformation pi2,
        QPointF tangent1, QPointF tangent2)
{
    if (tangent1.isNull() || tangent2.isNull()) return;

    const qreal maxSanePoint = 1e6;

    QPointF controlTarget1;
    QPointF controlTarget2;

    // Shows the direction in which control points go
    QPointF controlDirection1 = pi1.pos() + tangent1;
    QPointF controlDirection2 = pi2.pos() - tangent2;

    // Lines in the direction of the control points
    QLineF line1(pi1.pos(), controlDirection1);
    QLineF line2(pi2.pos(), controlDirection2);

    // Lines to check whether the control points lay on the opposite
    // side of the line
    QLineF line3(controlDirection1, controlDirection2);
    QLineF line4(pi1.pos(), pi2.pos());

    QPointF intersection;
    if (line3.intersect(line4, &intersection) == QLineF::BoundedIntersection) {
        qreal controlLength = line4.length() / 2;

        line1.setLength(controlLength);
        line2.setLength(controlLength);

        controlTarget1 = line1.p2();
        controlTarget2 = line2.p2();
    } else {
        QLineF::IntersectType type = line1.intersect(line2, &intersection);

        if (type == QLineF::NoIntersection ||
                intersection.manhattanLength() > maxSanePoint) {

            intersection = 0.5 * (pi1.pos() + pi2.pos());
//            dbgKrita << "WARINING: there is no intersection point "
//                     << "in the basic smoothing algoriths";
        }

        controlTarget1 = intersection;
        controlTarget2 = intersection;
    }

    // shows how near to the controlTarget the value raises
    qreal coeff = 0.8;

    qreal velocity1 = QLineF(QPointF(), tangent1).length();
    qreal velocity2 = QLineF(QPointF(), tangent2).length();

    if (velocity1 == 0.0 || velocity2 == 0.0) {
        velocity1 = 1e-6;
        velocity2 = 1e-6;
        warnKrita << "WARNING: Basic Smoothing: Velocity is Zero! Please report a bug:" << ppVar(velocity1) << ppVar(velocity2);
    }

    qreal similarity = qMin(velocity1/velocity2, velocity2/velocity1);

    // the controls should not differ more than 50%
    similarity = qMax(similarity, qreal(0.5));

    // when the controls are symmetric, their size should be smaller
    // to avoid corner-like curves
    coeff *= 1 - qMax(qreal(0.0), similarity - qreal(0.8));

    Q_ASSERT(coeff > 0);


    QPointF control1;
    QPointF control2;

    if (velocity1 > velocity2) {
        control1 = pi1.pos() * (1.0 - coeff) + coeff * controlTarget1;
        coeff *= similarity;
        control2 = pi2.pos() * (1.0 - coeff) + coeff * controlTarget2;
    } else {
        control2 = pi2.pos() * (1.0 - coeff) + coeff * controlTarget2;
        coeff *= similarity;
        control1 = pi1.pos() * (1.0 - coeff) + coeff * controlTarget1;
    }

    paintBezierCurve(pi1,
                     control1,
                     control2,
                     pi2);
}
Example #15
0
/// \brief Move the enemy along the path
void Enemy::action()
{
	float x = this->getCenterPos().x();
	float y = this->getCenterPos().y();

	const Tile* tile = ((const lo21*)this->game)->getTile(x,y);

	if ( tile != NULL && tile->isEndPoint() )
	{
		game->subLive(1);
		game->removeObject(this);
		return;
	}
	else if ( tile != NULL && tile->isWalkable() ) 
	{
		vec2f vector = tile->getVector();
		QPointF vectorP = vector.second - vector.first;
		qreal angle = 90 - std::atan(vectorP.x() / vectorP.y()) * 360.0 / (2*3.14957);
		qreal speed = this->getSpeed() * TILE_SIZE/FREQUENCY;


		// courbe (changement de direction
		if (  (lastVector != vectorP) && (vectorP.x() && vectorP.y()) )
		{

			// precedent mouvement : haut/bas
			if ( lastVector.x() && !lastVector.y() )
			{
				if (vectorP.y() > 0 && vectorP.x() > 0)
					wantedRotation = 90;
				else
					wantedRotation = -90;
			}
			// precedent mouvement : gauche/droite
			if ( !lastVector.x() && lastVector.y() )
			{
				if (vectorP.x() > 0 && vectorP.y() > 0)
					wantedRotation = -90;
				else
					wantedRotation = 90;
			}
		}

		if(wantedRotation)
		{
			float rotationSpeed = this->getSpeed() * 90./FREQUENCY;
			float lastWantedRotation=wantedRotation;
			if(wantedRotation<0)
			{
				wantedRotation+=rotationSpeed;
				if(wantedRotation>0)
					wantedRotation=0;
				
				this->angle-=wantedRotation-lastWantedRotation;
			}
			else
			{
				wantedRotation-=rotationSpeed;
				if(wantedRotation<0)
					wantedRotation=0;
				
				this->angle+=lastWantedRotation-wantedRotation;
			}
		}

		// rectiligne
		float dx = speed * vectorP.x() / vectorP.manhattanLength();
		float dy = speed * vectorP.y() / vectorP.manhattanLength();
		
		this->moveBy(dx, dy);
		this->lastVector = vectorP;
		
	}
	else
	{
		qDebug() << "There is no Walkbale Tile there : " << x << " , " << y;
	}
}
Example #16
0
void Canvas::fixPosAnimated( bool animate )
{
    QSize   viewportSize = _photoView->size();
    QSize   canvasSize	 = size();
    QPointF canvasPos	 = pos();
    QPointF wantedPos	 = canvasPos;

    if ( canvasSize.width() < viewportSize.width() )
    {
	// Center horizontally

	wantedPos.setX( ( viewportSize.width() - canvasSize.width() ) / 2.0 );
    }
    else
    {
	// Check if we panned too far left or right

	if ( canvasPos.x() > 0.0 ) // Black border left?
	    wantedPos.setX( 0.0 );

	if ( canvasPos.x() + canvasSize.width() < viewportSize.width() )
	    wantedPos.setX( viewportSize.width() - canvasSize.width() );
    }

    if ( canvasSize.height() < viewportSize.height() )
    {
	// Center vertically

	wantedPos.setY( ( viewportSize.height() -
			  canvasSize.height()	 ) / 2.0 );
    }
    else
    {
	// Check if we panned to far up or down

	if ( canvasPos.y() > 0.0 ) // Black border at the top?
	    wantedPos.setY( 0.0 );

	if ( canvasPos.y() + canvasSize.height() < viewportSize.height() )
	    wantedPos.setY( viewportSize.height() - canvasSize.height() );
    }

    QPointF diff = wantedPos - canvasPos;
    qreal manhattanLength = diff.manhattanLength();

#if 0
    logDebug() << "Canvas pos:\t"   << canvasPos    << endl;
    logDebug() << "Canvas size:\t"  << canvasSize   << endl;
    logDebug() << "VP size:\t"      << viewportSize << endl;
    logDebug() << "Wanted pos:\t"   << wantedPos    << endl;

    logDebug() << "Pos diff:\t"         << diff            << endl;
    logDebug() << "Manhattan length:\t" << manhattanLength << endl;
    logDebug() << "\n" << endl;
#endif

    if ( manhattanLength > 0.0 )
    {
	if ( manhattanLength < 5.0 || ! animate )
	{
	    setPos( wantedPos );
	    _photoView->updatePanner();
	}
	else
	{
	    // Animate moving to new position

	    if ( ! _animation )
	    {
		_animation = new GraphicsItemPosAnimation( this );
#if 0
		QObject::connect( _animation, SIGNAL( finished() ),
				  _photoView, SLOT  ( updatePanner() ) );
#endif
		QObject::connect( _animation, SIGNAL( valueChanged(QVariant)),
				  _photoView, SLOT  ( updatePanner() ) );
	    }

	    _animation->setStartValue( canvasPos );
	    _animation->setEndValue  ( wantedPos );
	    _animation->setDuration  ( AnimationDuration );
	    _animation->start();
	}
    }
}