javascript 两点之间的积分点数(Number of Integral Points between Two Points)

给定两点p (x1, y1) 和q (x2, y2),计算连接它们线上的积分点的数量。

输入:x1 = 2,y1 = 2,x2 = 5,y2 = 5

输出:2

解释:连接 (2,2) 和 (5,5) 的线上只有 2 个整数点。这两个点是 (3,3) 和 (4,4)。

输入:x1 = 1,y1 = 9,x2 = 8,y2 = 16

输出:6

解释:连接 (1,9) 和 (8,16) 的线上有 6 个整数点。

更多示例:

    如果点为 (0, 2) 和 (4, 0),则位于其上的整数点数只有一个,即 (2, 1)。 同样,如果点为 (1, 9) 和 (8, 16),则位于其上的整数点为 6,它们分别为 (2, 10)、(3, 11)、(4, 12)、(5, 13)、(6, 14) 和 (7, 15)。 

简单方法

    从给定的任意点开始,使用循环到达另一个端点。对于循环内的每个点,检查它是否位于连接给定两个点的线上。如果是,则将计数增加 1。此方法的时间复杂度为 O(min(x2-x1, y2-y1))。
    
最佳方法

    1. 如果连接p和q形成的边平行于 Y 轴,则   顶点之间的积分点数为:abs(p.y - q.y)-1 
    
    2. 类似地,如果边平行于 X 轴,则   两者之间的积分点数为:abs(p.x - q.x)-1 
    
    3. 否则,我们可以   使用以下公式找到顶点之间的积分点:GCD(abs(p.x - q.x), abs(p.y - q.y)) - 1

GCD 公式如何工作?

    其原理是找到最简形式的直线方程,即在方程 ax + by +c 中,系数 a、b 和 c 互质。我们可以通过计算 a、b 和 c 的 GCD(最大公约数)并将 a、b 和 c 转换为最简形式来实现这一点。 
    然后,答案将是(y 坐标差)除以 (a) – 1。这是因为在计算 ax + by + c = 0 后,对于不同的 y 值,x 将是可以被 a 整除的 y 值的数量。

以下是上述想法的实现:

<script>
// javascript code to find the number of integral points
// lying on the line joining the two given points
    // Class to represent an Integral point on XY plane.
     class Point {
    
        constructor(a , b) {
            this.x = a;
            this.y = b;
        }
    }

    // Utility function to find GCD of two numbers
    // GCD of a and b
    function gcd(a , b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    // Finds the no. of Integral points between
    // two given points.
    function getCount( p,  q)
    {
    
        // If line joining p and q is parallel to
        // x axis, then count is difference of y
        // values
        if (p.x == q.x)
            return Math.abs(p.y - q.y) - 1;

        // If line joining p and q is parallel to
        // y axis, then count is difference of x
        // values
        if (p.y == q.y)
            return Math.abs(p.x - q.x) - 1;

        return gcd(Math.abs(p.x - q.x), Math.abs(p.y - q.y)) - 1;
    }

    // Driver program to test above    
         p = new Point(1, 9);
         q = new Point(8, 16);

        document.write("The number of integral points between " + "(" + p.x + ", " + p.y + ") and (" + q.x + ", "
                + q.y + ") is " + getCount(p, q));

// This code is contributed by gauravrajput1
</script>

输出: 

(1, 9)和(8, 16)之间的积分点数为 6

时间复杂度: O(log(min(a,b))),因为我们使用递归来查找 GCD。

辅助空间: O(log(min(a,b))),用于递归堆栈空间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值