Change in geometric vector length



  • There is a function setLength to change the vector length (geometric) on the specified parameter No change of direction

    This is a function.

    Vector2f setLength(Vector2f startPos, Vector2f currentPos, double neededLength)
    {
        double currentLength = getLength(startPos, currentPos); 
        //getLength - отдельная написанная мною функция, возвращает длину вектора
    
    double factorLength = neededLength / currentLength; 
    //factorLength - коэффициент разности нужной длины и текущей
    
    return Vector2f(currentPos.x * factorLength, currentPos.y * factorLength);
    

    }

    The function itself shall return the vector ' s end coordinates given length and the same direction

    but why the final length of the vector does not correspond to the necessary (more, less). In addition, the direction of the vector itself is changing. It's definitely not a mistake in getLength, it's all as simple as possible.

    double getLength(Vector2f firstdot, Vector2f seconddot)
    {
    return sqrt(pow(firstdot.x - seconddot.y, 2) + pow(firstdot.y - seconddot.y, 2));
    }

    Thank you.



  • Because you don't know what a vector is.

    return Vector2f(
      (currentPos.x - startPos.x) * factorLength + startPos.x, 
      (currentPos.y - startPos.y) * factorLength + startPos.y
    );
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2