📜  正方形内的积分坐标计数

📅  最后修改于: 2021-04-26 08:37:49             🧑  作者: Mango

给定正方形的左下角和右上角坐标(x1,y1)和(x2,y2) ,任务是计算严格位于正方形内的整数坐标的数量。
例子:

方法:给定正方形的右下角坐标和右上角坐标的x和y坐标之差分别给出了x坐标和y坐标在正方形相对两侧之间的数量积分点。严格位于正方形内的点总数由下式给出:

例如:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the integral
// points inside a square
void countIntgralPoints(int x1, int y1,
                        int x2, int y2)
{
    cout << (y2 - y1 - 1) * (x2 - x1 - 1);
}
 
// Driver Code
int main()
{
    int x1 = 1, y1 = 1;
    int x2 = 4, y2 = 4;
 
    countIntgralPoints(x1, y1, x2, y2);
    return 0;
}


Java
// Java program for the above approach
 
class GFG {
 
// Function to calculate the integral
// points inside a square
static void countIntgralPoints(int x1, int y1,
                               int x2, int y2)
{
    System.out.println((y2 - y1 - 1) *
                       (x2 - x1 - 1));
}
 
// Driver Code
public static void main(String args[])
{
    int x1 = 1, y1 = 1;
    int x2 = 4, y2 = 4;
     
    countIntgralPoints(x1, y1, x2, y2);
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
 
# Function to calculate the integral
# points inside a square
def countIntgralPoints(x1, y1, x2, y2):
    print((y2 - y1 - 1) * (x2 - x1 - 1))
 
# Driver Code
if __name__ == '__main__':
 
    x1 = 1
    y1 = 1
    x2 = 4
    y2 = 4
 
    countIntgralPoints(x1, y1, x2, y2)
 
# This code is contributed by Samarth


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to calculate the integral
// points inside a square
static void countIntgralPoints(int x1, int y1,
                               int x2, int y2)
{
    Console.WriteLine((y2 - y1 - 1) *
                      (x2 - x1 - 1));
}
 
// Driver code
static void Main()
{
    int x1 = 1, y1 = 1;
    int x2 = 4, y2 = 4;
     
    countIntgralPoints(x1, y1, x2, y2);
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
4

时间复杂度: O(1)