how to caculate the point which move towards another point?

mc 4,616 Reputation points
2024-10-07T14:46:17.7566667+00:00

I have one point(x,y) and it will move towards point(x1,y1)

when it move distance =5 pixel how to caculate the point ?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,760 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Darran Rowe 1,041 Reputation points
    2024-10-07T21:26:02.2866667+00:00

    Mathematics, a mixture of geometry and trigonometry.

    First, calculate the length of the line segment between P(x,y) and P1(x1,y1). This would be done using geometry. Once this has been obtained, it is possible to formulate a right angled triangle using the x and y axis. This can be used to obtain the angle that the movement will procede at. This is done using trigonometry. Once this angle has been obtained, then make a right angled triangle with hypotenuse of length 5, starting at P(x,y) and calculate the length of the adjacent and opposite. It is pretty straight forward.

    Just remember that the formula for the length of a stright line is sqrt((x2 - x1)^2 + (y2 - y1)^2). Because they are on the axis, the horizontal side of the triangle that will be made between P(x,y) and P1(x1,y1) will be x1-x, and the vertical side will be y1 - y. If there is any issues picturing this, just draw it out on a piece of paper.


  2. Minxin Yu 12,086 Reputation points Microsoft Vendor
    2024-10-08T07:37:06.1633333+00:00

    Hi,

    Darran Rowe has explained the math.

    Since distance is pixel, std::ceil can be used to computes the least integer value not less than number.

    #include <iostream>
    #include <cmath> 
    struct Point
    {
    	double x;
    	double y;
    };
    
    
    //Point start,  Point target, double distance
    //calculate new point
    double dx = target.x - start.x;
    double dy = target.y - start.y;
    double currentDistance = std::sqrt (dx * dx + dy * dy);
    
    
    if (currentDistance == 0) {
    	;
    }
    
    
    double directionX = dx / currentDistance;
    double directionY = dy / currentDistance;
    
    
    Point newPoint;
    newPoint.x = start.x + directionX * distance;
    newPoint.y = start.y + directionY * distance;
    //newPoint.x = start.x + std::ceil (directionX * distance);
    //newPoint.y = start.y + std::ceil (directionY * distance);
    
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.