📜  查找是否只有两条平行线包含所有坐标点

📅  最后修改于: 2021-04-24 03:41:16             🧑  作者: Mango

给定一个数组,该数组表示坐标平面上一组点的y坐标,其中(i,arr [i])表示单个点。查找是否有可能绘制一对平行线,其中包括给定的所有坐标点,并且两条线都必须包含一个点。如果可能,打印1 ,如果可能,打印0

例子:

方法:由纵轴(x1,y1)和(x2,y2)构成的直线的斜率是y2-y2 / x2-x1。由于给定的数组由(i,arr [i])的点的坐标组成。因此,(arr [2] -arr [1])/(2-1)是由(1,arr [i])和(2,arr [2])构成的线的斜率。仅考虑三个点,例如P0(0,arr [0]),P1(1,arr [1])和P2(2,arr [2]),因为要求只有两条平行线,这是强制性的这三个点中的两个位于同一条线上。因此,三种可能的情况是:

  • P0和P1在同一行上,因此它们的斜率将为arr [1] -arr [0]
  • P1和P2在同一行上,因此它们的斜率将为arr [2] -arr [1]
  • P0和P2在同一行上,因此它们的斜率将为arr [2] -arr [0] / 2

取三分之二的条件说P0和P1位于同一条线上,在这种情况下,令m = arr [1] -arr [0]是我们的斜率。对于数组(i,arr [i])中的一个一般点,线的方程为:

=> (y-y1) = m (x-x1)
=> y-arr[i] = m (x-i)
=> y-mx = arr[i] - mi

现在,由于y-mx = c是直线的一般方程,这里c = arr [i] -mi。现在,如果解决方案对于给定的数组是可行的,那么我们必须有精确的两个截距(c)。
因此,如果上述三个可能的任何一个存在两个截然不同的截距,则所需的解决方案是可能的,并打印1或0。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Find if slope is good with only two intercept
bool isSlopeGood(double slope, int arr[], int n)
{
    set setOfLines;
    for (int i = 0; i < n; i++)
        setOfLines.insert(arr[i] - slope * (i));
  
    // if set of lines have only two distinct intercept
    return setOfLines.size() == 2;
}
  
// Function to check if required solution exist
bool checkForParallel(int arr[], int n)
{
    // check the result by processing
    // the slope by starting three points
    bool slope1 = isSlopeGood(arr[1] - arr[0], arr, n);
    bool slope2 = isSlopeGood(arr[2] - arr[1], arr, n);
    bool slope3 = isSlopeGood((arr[2] - arr[0]) / 2, arr, n);
  
    return (slope1 || slope2 || slope3);
}
  
// Driver code
int main()
{
    int arr[] = { 1, 6, 3, 8, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (int)checkForParallel(arr, n);
  
    return 0;
}


Java
// Java implementation of the above approach 
import java.util.*;
  
class GfG 
{
  
// Find if slope is good 
// with only two intercept 
static boolean isSlopeGood(double slope,
                        int arr[], int n) 
{ 
    Set setOfLines = new HashSet (); 
    for (int i = 0; i < n; i++) 
        setOfLines.add(arr[i] - slope * (i)); 
  
    // if set of lines have only two distinct intercept 
    return setOfLines.size() == 2; 
} 
  
// Function to check if required solution exist 
static boolean checkForParallel(int arr[], int n) 
{ 
    // check the result by processing 
    // the slope by starting three points 
    boolean slope1 = isSlopeGood(arr[1] - arr[0], arr, n); 
    boolean slope2 = isSlopeGood(arr[2] - arr[1], arr, n); 
    boolean slope3 = isSlopeGood((arr[2] - arr[0]) / 2, arr, n); 
  
    return (slope1 == true || slope2 == true || slope3 == true); 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int arr[] = { 1, 6, 3, 8, 5 }; 
    int n = arr.length; 
    if(checkForParallel(arr, n) == true)
    System.out.println("1");
    else
    System.out.println("0");
}
} 
  
// This code is contributed by Prerna Saini.


Python3
# Python3 implementation of the 
# above approach
  
# Find if slope is good with only 
# two intercept
def isSlopeGood(slope, arr, n):
  
    setOfLines = dict()
    for i in range(n):
        setOfLines[arr[i] - slope * (i)] = 1
  
    # if set of lines have only 
    # two distinct intercept
    return len(setOfLines) == 2
  
# Function to check if required solution exist
def checkForParallel(arr, n):
      
    # check the result by processing
    # the slope by starting three points
    slope1 = isSlopeGood(arr[1] - arr[0], arr, n)
    slope2 = isSlopeGood(arr[2] - arr[1], arr, n)
    slope3 = isSlopeGood((arr[2] - arr[0]) // 2, arr, n)
  
    return (slope1 or slope2 or slope3)
  
# Driver code
arr = [1, 6, 3, 8, 5 ]
n = len(arr)
if checkForParallel(arr, n):
    print(1)
else:
    print(0)
      
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach 
using System;
using System.Collections.Generic;
  
class GfG 
{ 
  
// Find if slope is good 
// with only two intercept 
static bool isSlopeGood(double slope, 
                        int []arr, int n) 
{ 
  
    HashSet setOfLines = new HashSet (); 
    for (int i = 0; i < n; i++) 
        setOfLines.Add(arr[i] - slope * (i)); 
  
    // if set of lines have only two distinct intercept 
    return setOfLines.Count == 2; 
} 
  
// Function to check if required solution exist 
static bool checkForParallel(int []arr, int n) 
{ 
    // check the result by processing 
    // the slope by starting three points 
    bool slope1 = isSlopeGood(arr[1] - arr[0], arr, n); 
    bool slope2 = isSlopeGood(arr[2] - arr[1], arr, n); 
    bool slope3 = isSlopeGood((arr[2] - arr[0]) / 2, arr, n); 
  
    return (slope1 == true || slope2 == true || slope3 == true); 
} 
  
// Driver code 
public static void Main() 
{ 
    int []arr = { 1, 6, 3, 8, 5 }; 
    int n = arr.Length; 
    if(checkForParallel(arr, n) == true) 
        Console.WriteLine("1"); 
    else
        Console.WriteLine("0"); 
} 
} 
  
// This code is contributed by Ryuga.


PHP


输出:
1