[Thousand-question case] TypeScript gets the distance between two points | midpoint | supplementary point | vector | angle

When we write some functions such as aiming, drawing, and erasing, we often encounter the calculation of some parameters between two points, so this article will talk about a series of parameter calculations between two points.

Directory

1 The distance between two points

① Implementation principle

② Code implementation and results

2 Midpoint between two points

① Implementation principle

② Code implementation and results

3 Supplementary points between two points

① Implementation principle

② Code implementation and results

4 A vector between two points

① Implementation principle

② Code implementation and results

5Angle between two points

① Implementation principle

② Code implementation and results

1 Distance between two points

①Principle of Implementation

The straight-line distance between two points is the square root of the sum of the square of the x-axis distance and the square of the y-axis distance. Suppose two points A, B and their coordinates are A(X1,Y1), B(X2,Y2), then the distance between A and B is:

②Code implementation and results

*code block

 /**
     * Calculate the distance between two points
     * @param startPoint starting point coordinates
     * @param endPoint end point coordinates
     * @returns return distance
     */
    getDistancetweenTwoPoint(startPoint, endPoint) {
        let lngDiff = endPoint.x - startPoint.x; //The x difference between the start point and the end point
        let latDiff = endPoint.y - startPoint.y; //The y difference between the start point and the end point
        return Math.sqrt(lngDiff * lngDiff + latDiff * latDiff);//Return the result calculated according to the formula
    }

*Implementation and Results

UseFunction() {
        let startPoint = cc.v2(0, 1);
        let endPoint = cc.v2(0, 3)
        let result = this. getDistancetweenTwoPoint(startPoint, endPoint)
        cc. log(result)
    }

//result: 2

2 Midpoint between two points

①Implementation principle

There are two points A(x1, y1) B(x2, y2) and the coordinates of their midpoint P are ((x1 + x2)/2, (y1 + y2)/2)

②Code implementation and results

*code block

 /**
     * Calculate the midpoint between two points
     * @param a starting point coordinates
     * @param b end point coordinates
     * @returns returns the coordinates of the midpoint
     */
    getMidBetweenTwoPoint(a, b) {
        let Midx = (a.x + b.x) / 2;
        let Midy = (a.y + b.y) / 2;
        return cc.v2(Midx, Midy)
    }

*Implementation and Results

 UseFunction() {
        let startPoint = cc.v2(0, 1);
        let endPoint = cc.v2(0, 3)
        let result = this. getMidBetweenTwoPoint(startPoint, endPoint)
        cc. log(result)
    }

//result: (0, 2) 

3 A supplementary point between two points

①Implementation principle

There are two points A(x1, y1) B(x2, y2). Calculate the distance between the two points. Calculate the total number of points that need to be supplemented by dividing the distance by the step size (one point per meter), and use X2 of point B , Y2 minus X1 at point A, Y1 to get the difference between X and Y, divide the difference between X and Y by the total number of supplementary points to get the difference between X and Y at each step, the total number of circular supplementary points, each time at point A On the basis of X1, Y1, add the latitude and longitude difference and multiply the total.

②Code implementation and results

*code block

 /**
        * Calculate the complement point between two points
        * @param startPoint starting point
        * @param endPoint end point
        * @param distance the distance between two points
        * @param stepSize the distance of each step
        * @param containBoth whether to include the starting point and the ending point
        * @returns returns the coordinate array of the point
        */
    getFillPoints(startPoint, endPoint, distance, stepSize, containBoth) {
        let lngDiff = endPoint.x - startPoint.x; //The x difference between the start point and the end point
        let latDiff = endPoint.y - startPoint.y; //The y difference between the start point and the end point
        let n = Math.ceil(distance / stepSize); //The total number of supplementary points
        let a = lngDiff / n; // x difference of each step
        let b = latDiff / n; // y difference at each step
        let pointsArr = []; //Return the coordinate array of points
        // Put the supplementary point into the coordinate array (excluding the start and end points)
        for (let i = 1; i < n; i ++ ) {
            let x = startPoint. x + a * i;
            let y = startPoint.y + b * i;
            pointsArr.push(cc.v2(x, y));
        }
        // Put the start and end points into the coordinate array
        if (containBoth) {
            pointsArr.unshift(startPoint); //Add starting point
            pointsArr.push(endPoint); //add end point
        }
        return pointsArr;
    }

*Implementation and Results

 UseFunction() {
        // coordinates of two points
        let Pointa = cc.v2(0, 0)
        let Pointb = cc.v2(0, 5)
        let distance = Tools.getDistance(Pointa, Pointb); //The distance between two points
        let stepSize = 1; //The distance of each step
        let containBoth = true; //contain both ends
        let allPoints = this. getFillPoints(Pointa, Pointb, distance, stepSize, containBoth);
        cc. log(allPoints)
    }
Result: (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5)

4 Vector between two points

①Implementation principle

There are two points A(a1,b1), B(a2,b2,), then the vector AB is the coordinates of point B minus the coordinates of point A, that is, vector AB=(a2-a1,b2-b1)

②Code implementation and results

*code block

 /**
     * Calculate the vector between two points
     * @param a coordinate a
     * @param b coordinate b
     * @returns return vector
     */
    getVectorBetweenTwoPoint(a, b) {
        let Midx = (b.x - a.x)
        let Midy = (b.y - a.y)
        return cc.v2(Midx, Midy)
    }

*Implementation and Results

 UseFunction() {
        let startPoint = cc.v2(0, 1);
        let endPoint = cc.v2(0, 3)
        let result = this. getVectorBetweenTwoPoint(startPoint, endPoint)
        cc. log(result)
    }

//result: (0, 2) 

5 Angle between two points

①Principle of Implementation

Calculate the tangent of two points and get the angle: point one (X1,Y1), point two (X2,Y2): Math.Atan2((Y2 – Y1), (X2 – X2)) * 180 / Math.PI

Note: Math.Atan2() returns the plane angle (radians) between the line segment from the origin (0,0) to the point (x,y) and the positive direction of the x-axis value), that is, Math.atan2(y,x)

math.pi Returns a floating-point value π, generally referred to as pi, PI (3.1415…)

②Code implementation and results

*code block

 /**
     * Calculate the angle between two points
     * @param a coordinate a
     * @param b coordinate b
     * @returns return angle
     */
    getAngleBetweenTwoPoint(a, b) {
        let result=Math.Atan2((b.y- a.y), (b.x - a.x)) * 180 / Math.PI;
        return result
    }

*Implementation and Results

 UseFunction() {
        let startPoint = cc.v2(0, 0);
        let endPoint = cc.v2(1, 1)
        let result = this. getAngleBetweenTwoPoint(startPoint, endPoint)
        cc. log(result)
    }

//result: 45 

When performing operations such as these, the test is more on the mathematical formula and the mastery of the Math class, which contains methods for performing basic mathematical operations, such as elementary exponentials, logarithms, square roots, and trigonometric functions Wait, practice more and you will be able to use it proficiently.