C# 点在三角形中吗?

C# 点在三角形中吗?,c#,coordinates,point,C#,Coordinates,Point,我的家庭作业需要一些帮助。我努力做到这一点,但失败了 我们有一个坐标为a=(-4,4),B=(4,-2),C=(6,6)的三角形。我 需要从点P=(x,y)的用户坐标加载,然后写入 屏幕信息显示P点是否位于内,外, 或位于三角形的侧面 这就是我所做的: class Program { static void Main(string[] args) { const int x1 = -4, y1 = 4; const int x2 = 4, y2 =

我的家庭作业需要一些帮助。我努力做到这一点,但失败了

我们有一个坐标为a=(-4,4),B=(4,-2),C=(6,6)的三角形。我 需要从点P=(x,y)的用户坐标加载,然后写入 屏幕信息显示P点是否位于, 或位于三角形的侧面

这就是我所做的:

class Program
{
    static void Main(string[] args)
    {
        const int x1 = -4, y1 = 4;
        const int x2 = 4, y2 = -2;
        const int x3 = 6, y3 = 6;

        int x0, y0;
        Console.Write("Podaj współrzędną x: ");
        x0 = Convert.ToInt32(Console.ReadLine());
        Console.Write("Podaj współrzędną y: ");
        y0 = Convert.ToInt32(Console.ReadLine());

        double A_AB = (y2 - y1 / x2 - x1);
        double B_AB = (y1 - (y2 - y1 / x2 - x1) * x1);

        double A_AC = (y3 - y1 / x3 - x1);
        double B_AC = (y1 - (y3 - y1 / x3 - x1) * x1);

        double A_BC = (y3 - y2 / x3 - x2);
        double B_BC = (y2 - (y3 - y2 / x3 - x2) * x2);

        if ((y0 > (A_AB * x0 + B_AB) && y0 > (A_AC * x0 + B_AC) && y0 > (A_BC * x0 + B_BC))) Console.WriteLine("Point is inside of the triangle.");
        else if (y0 == (A_AB * x0 + B_AB) || y0 == (A_AC * x0 + B_AC) || y0 == (A_BC * x0 + B_BC)) Console.WriteLine("Point is on the side of the triangle");
        else Console.WriteLine("Point is outside of the triangle.");

        Console.ReadKey(true);
    }
}
有点不对劲,因为很难击中三角形中的空格。谁来分析一下?我的大脑在燃烧


谢谢。

使用重心坐标系解决问题,如下所示:

    public static void Main(string[] args)
    {
        const double x1 = -4, y1 = 4;
        const double x2 = 4, y2 = -2;
        const double x3 = 6, y3 = 6;

        double x, y;
        x = 0;
        y = 1;

        double a = ((y2 - y3)*(x - x3) + (x3 - x2)*(y - y3)) / ((y2 - y3)*(x1 - x3) + (x3 - x2)*(y1 - y3));
        double b = ((y3 - y1)*(x - x3) + (x1 - x3)*(y - y3)) / ((y2 - y3)*(x1 - x3) + (x3 - x2)*(y1 - y3));
        double c = 1 - a - b;

        if (a == 0 || b == 0 || c == 0) Console.WriteLine("Point is on the side of the triangle");
        else if (a >= 0 && a <= 1 && b >= 0 && b<= 1 && c >= 0 && c <= 1) Console.WriteLine("Point is inside of the triangle.");
        else Console.WriteLine("Point is outside of the triangle.");
    }
publicstaticvoidmain(字符串[]args)
{
常数双x1=-4,y1=4;
常数双x2=4,y2=-2;
常数双x3=6,y3=6;
双x,y;
x=0;
y=1;
双a=((y2-y3)*(x-x3)+(x3-x2)*(y-y3))/((y2-y3)*(x1-x3)+(x3-x2)*(y1-y3));
双b=((y3-y1)*(x-x3)+(x1-x3)*(y-y3))/((y2-y3)*(x1-x3)+(x3-x2)*(y1-y3));
双c=1-a-b;
如果(a==0 | | b==0 | | c==0)Console.WriteLine(“点在三角形的一侧”);

否则,如果(a>=0&&a=0&&b=0&&c使用重心坐标系解决问题,如下所示:

    public static void Main(string[] args)
    {
        const double x1 = -4, y1 = 4;
        const double x2 = 4, y2 = -2;
        const double x3 = 6, y3 = 6;

        double x, y;
        x = 0;
        y = 1;

        double a = ((y2 - y3)*(x - x3) + (x3 - x2)*(y - y3)) / ((y2 - y3)*(x1 - x3) + (x3 - x2)*(y1 - y3));
        double b = ((y3 - y1)*(x - x3) + (x1 - x3)*(y - y3)) / ((y2 - y3)*(x1 - x3) + (x3 - x2)*(y1 - y3));
        double c = 1 - a - b;

        if (a == 0 || b == 0 || c == 0) Console.WriteLine("Point is on the side of the triangle");
        else if (a >= 0 && a <= 1 && b >= 0 && b<= 1 && c >= 0 && c <= 1) Console.WriteLine("Point is inside of the triangle.");
        else Console.WriteLine("Point is outside of the triangle.");
    }
publicstaticvoidmain(字符串[]args)
{
常数双x1=-4,y1=4;
常数双x2=4,y2=-2;
常数双x3=6,y3=6;
双x,y;
x=0;
y=1;
双a=((y2-y3)*(x-x3)+(x3-x2)*(y-y3))/((y2-y3)*(x1-x3)+(x3-x2)*(y1-y3));
双b=((y3-y1)*(x-x3)+(x1-x3)*(y-y3))/((y2-y3)*(x1-x3)+(x3-x2)*(y1-y3));
双c=1-a-b;
如果(a==0 | | b==0 | | c==0)Console.WriteLine(“点在三角形的一侧”);

否则,如果(a>=0&&a=0&&b=0&&c)当x和y不是整数时,它会有什么区别吗?。使用调试器分析代码可以使您的大脑免于大量烧伤。当x和y不是整数时,它会有什么区别吗?。使用调试器分析代码可以使您的大脑免于大量烧伤。