2016-12-04 2 views
1

Ich brauche Hilfe bei meinen Hausaufgaben. Ich habe mich sehr bemüht und ich habe versagt.C# - ist der Punkt im Dreieck?

Wir haben ein Dreieck mit Koordinaten: A = (- 4,4), B = (4, -2), C = (6,6). I müssen Last von Benutzer Koordinaten des Punktes P = (x, y) und dann schreiben auf die Bildschirminformationen zu sagen, wenn der Punkt P in legt, aus, oder auf der Seite des Dreiecks.

Das ist, was ich bis hierher geschafft:

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); 
    } 
} 

Etwas ist falsch, weil es schwer ist, Raum im Dreieck zu treffen. Würde jemand das analysieren? Mein Gehirn brennt.

Danke.

+0

Ist es es einen Unterschied machen, wenn x und y ganze Zahlen Arent .. – Sayse

+3

[Wie um zu bestimmen, ob ein Punkt in einem Dreieck ist? ] (http://stackoverflow.com/q/2049582/1070452) – Plutonix

+0

Mit einem Debugger, um Ihren Code zu analysieren, können Sie Ihr Gehirn vor viel brennen. –

Antwort

0

Das Problem wird das System wie so baryzentrischen Koordinaten unter Verwendung gelöst:

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."); 
    } 
+0

Sorry, ich schreibe so spät, aber danke. :) –