📜  如果给出三个点,检查三角形是否有效

📅  最后修改于: 2021-10-23 08:05:32             🧑  作者: Mango

给定平面P1P2P3中三个点的坐标,任务是检查这三个点是否形成三角形
例子:

方法:问题中的关键观察是三个点只有在它们不在直线上时才形成三角形,即这三个点的三角形形成的面积不为零。
\text{Area of Triangle }= \frac{1}{2}*(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
上面的公式是从鞋带公式推导出来的。
因此,我们将检查三角形形成的面积是否为零。
下面是上述方法的实现:

C++
// C++ implementation to check
// if three points form a triangle
 
#include 
using namespace std;
 
// Function to check if three
// points make a triangle
void checkTriangle(int x1, int y1, int x2,
                   int y2, int x3, int y3)
{
 
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    int a = x1 * (y2 - y3)
            + x2 * (y3 - y1)
            + x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        cout << "No";
    else
        cout << "Yes";
}
 
// Driver Code
int main()
{
    int x1 = 1, x2 = 2, x3 = 3,
        y1 = 1, y2 = 2, y3 = 3;
    checkTriangle(x1, y1, x2,
                  y2, x3, y3);
    return 0;
}


Java
// Java implementation to check
// if three points form a triangle
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to check if three
// points make a triangle
static void checkTriangle(int x1, int y1,
                          int x2, int y2,
                          int x3, int y3)
{
 
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    int a = x1 * (y2 - y3) +
            x2 * (y3 - y1) +
            x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        System.out.println("No");
    else
        System.out.println("Yes");
}
 
// Driver code
public static void main(String[] args)
{
    int x1 = 1, y1 = 1,
        x2 = 2, y2 = 2,
        x3 = 3, y3 = 3;
    checkTriangle(x1, y1, x2, y2, x3, y3);
}
}
 
// This code is contributed by coder001


Python3
# Python3 implementation to check
# if three points form a triangle
 
# Function to check if three
# points make a triangle
def checkTriangle(x1, y1, x2, y2, x3, y3):
     
    # Calculation the area of
    # triangle. We have skipped
    # multiplication with 0.5
    # to avoid floating point
    # computations
    a = (x1 * (y2 - y3) +
         x2 * (y3 - y1) +
         x3 * (y1 - y2))
         
    # Condition to check if
    # area is not equal to 0
    if a == 0:
        print('No')
    else:
        print('Yes')
         
# Driver code
if __name__=='__main__':
     
    (x1, x2, x3) = (1, 2, 3)
    (y1, y2, y3) = (1, 2, 3)
     
    checkTriangle(x1, y1, x2, y2, x3, y3)
     
# This code is contributed by rutvik_56


C#
// C# implementation to check
// if three points form a triangle
using System;
 
class GFG {
     
// Function to check if three
// points make a triangle
static void checkTriangle(int x1, int y1,
                          int x2, int y2,
                          int x3, int y3)
{
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    int a = x1 * (y2 - y3) +
            x2 * (y3 - y1) +
            x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        Console.WriteLine("No");
    else
        Console.WriteLine("Yes");
}
 
// Driver code
public static void Main()
{
    int x1 = 1, y1 = 1,
        x2 = 2, y2 = 2,
        x3 = 3, y3 = 3;
         
    checkTriangle(x1, y1, x2, y2, x3, y3);
}
}
 
//This code is contributed by AbhiThakur


Javascript


输出:
No

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程