📜  两点之间的积分点数

📅  最后修改于: 2021-04-23 18:18:24             🧑  作者: Mango

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

示例:如果点是(0,2)和(4,0),则位于其上的积分点的数量仅为1,即(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. If the edge formed by joining p and q is parallel 
   to the X-axis, then the number of integral points 
   between the vertices is : 
        abs(p.y - q.y)-1

2. Similarly if edge is parallel to the Y-axis, then 
   the number of integral points in between is :
    abs(p.x - q.x)-1

3. Else, we can find the integral points between the
   vertices using below formula:
     GCD(abs(p.x - q.x), abs(p.y - q.y)) - 1


GCD公式如何运作?

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

以下是上述想法的实现。

C++
// C++ code to find the number of integral points
// lying on the line joining the two given points
#include 
#include 
using namespace std;
  
// Class to represent an Integral point on XY plane.
class Point
{
public:
    int x, y;
    Point(int a=0, int b=0):x(a),y(b) {}
};
  
// Utility function to find GCD of two numbers
// GCD of a and b
int gcd(int a, int b)
{
    if (b == 0)
       return a;
    return gcd(b, a%b);
}
  
// Finds the no. of Integral points between
// two given points.
int getCount(Point p, Point 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 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 abs(p.x-q.x) - 1;
  
    return gcd(abs(p.x-q.x), abs(p.y-q.y))-1;
}
  
// Driver program to test above
int main()
{
    Point p(1, 9);
    Point q(8, 16);
  
    cout << "The number of integral points between "
         << "(" << p.x << ", " << p.y << ") and ("
         << q.x << ", " << q.y << ") is "
         << getCount(p, q);
  
    return 0;
}


Java
// Java code to find the number of integral points
// lying on the line joining the two given points
  
class GFG 
{
  
// Class to represent an Integral point on XY plane.
static class Point
{
    int x, y;
    Point(int a, int b)
    {
        this.x = a;
        this.y = b;
    }
};
  
// Utility function to find GCD of two numbers
// GCD of a and b
static int gcd(int a, int b)
{
    if (b == 0)
    return a;
    return gcd(b, a % b);
}
  
// Finds the no. of Integral points between
// two given points.
static int getCount(Point p, Point 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
public static void main(String[] args)
{
    Point p = new Point(1, 9);
    Point q = new Point(8, 16);
  
    System.out.println("The number of integral points between "
        + "(" + p.x + ", " + p.y + ") and ("
        + q.x + ", " + q.y + ") is "
        + getCount(p, q));
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 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:
      
    def __init__(self, a, b):
        self.x = a
        self.y = b
  
# Utility function to find GCD
# of two numbers GCD of a and b
def gcd(a, b):
  
    if b == 0:
        return a
    return gcd(b, a % b)
  
# Finds the no. of Integral points
# between two given points.
def 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 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 abs(p.x - q.x) - 1
  
    return gcd(abs(p.x - q.x), 
               abs(p.y - q.y)) - 1
  
# Driver Code
if __name__ == "__main__":
  
    p = Point(1, 9)
    q = Point(8, 16)
  
    print("The number of integral points", 
          "between ({}, {}) and ({}, {}) is {}" . 
           format(p.x, p.y, q.x, q.y, getCount(p, q)))
  
# This code is contributed by Rituraj Jain


C#
// C# code to find the number of integral points
// lying on the line joining the two given points
using System;
  
class GFG 
{
  
// Class to represent an Integral point on XY plane.
public class Point
{
    public int x, y;
    public Point(int a, int b)
    {
        this.x = a;
        this.y = b;
    }
};
  
// Utility function to find GCD of two numbers
// GCD of a and b
static int gcd(int a, int b)
{
    if (b == 0)
    return a;
    return gcd(b, a % b);
}
  
// Finds the no. of Integral points between
// two given points.
static int getCount(Point p, Point 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 code
public static void Main(String[] args)
{
    Point p = new Point(1, 9);
    Point q = new Point(8, 16);
  
    Console.WriteLine("The number of integral points between "
        + "(" + p.x + ", " + p.y + ") and ("
        + q.x + ", " + q.y + ") is "
        + getCount(p, q));
}
}
  
/* This code contributed by PrinciRaj1992 */



输出:

The number of integral points between (1, 9) and (8, 16) is 6

参考 :
https://www.geeksforgeeks.org/count-integral-points-inside-a-triangle/