2013-02-03 14 views
5

Ich bin seit Stunden dabei und versuche verschiedene Methoden, um fast jede Frage zu betrachten. Vielleicht habe ich es völlig falsch, aber ich habe das Gefühl, dass ich meine Mathematik richtig verstanden habe, aber egal welche Zahlen ich eingib, ich bekomme die gleiche Ausgabe. Mein Code ist irgendwo weg und ich muss ihn um Mitternacht abgeben.Finden, ob ein Punkt innerhalb eines Dreiecks ist

Es ist alles so lustig: Finden Sie, ob ein Punkt innerhalb eines Dreiecks Code ist. (Für Anfänger)

import java.util.Scanner; 

public class PointsTriangle { 

    // checks if point entered is within the triangle 
    //given points of triangle are (0,0) (0,100) (200,0) 
    public static void main (String [] args) { 
     //obtain point (x,y) from user 
     System.out.print("Enter a point's x- and y-coordinates: "); 
     Scanner input = new Scanner(System.in); 
     double x = input.nextDouble(); 
     double y = input.nextDouble(); 

     //find area of triangle with given points 
     double ABC = ((0*(100-0 )+0*(0 -0)+200*(0-100))/2.0); 
     double PAB = ((x*(0 -100)+0*(100-y)+0 *(y- 0))/2.0); 
     double PBC = ((x*(100-0 )+0*(0 -y)+200*(y-100))/2.0); 
     double PAC = ((x*(0 -100)+0*(100-y)+200*(y- 0))/2.0); 

     boolean isInTriangle = PAB + PBC + PAC == ABC; 

     if (isInTriangle) 
      System.out.println("The point is in the triangle"); 
     else 
      System.out.println("The point is not in the triangle"); 
    }//end main 
}//end PointsTriangle 
+0

Es ist wahrscheinlich lohnt sich, die Werte ausgibt, Sie denken Sie über das Debuggen lesen als Teil ... – Floris

Antwort

5

Wenn Sie ein Bild zeichnen, können Sie den Punkt sehen, hat einfache Ungleichungen (unten/oben/rechts von bestimmten Linien). Ob „am Rande“ in oder aus werde ich dir überlassen:

Y > 0 (above the X axis) 
X > 0 (to the right of the Y axis) 
X + 2* Y < 200 (below the hypotenuse) 

Schreiben Sie eine if-Anweisung um diese drei und fertig:

if((y > 0) && (x > 0) && (x + 2*y < 200)) 
    System.out.println("The point is in the triangle"); 
else 
    System.out.println("The point is not in the triangle"); 
+0

ich Ihnen nicht genug danken kann. Ich überlege immer alles. Du hast mein Wochenende gemacht! – Lish

+0

@Lish - Gern geschehen. Späte Nächte tun das für dein Gehirn ... – Floris

5

Sie den falschen Wert Bestellung in deine Formel; Daher ist das Ergebnis falsch. Wenn die drei Eckpunkte wie folgt

sind
A(x1, y1) B(x2, y2), C(x3, y3) 

dann wird die Fläche berechnet als

double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))/2; 

Danach werden Sie nur jede Ecke mit dem Eingangspunkt ersetzen, haben wir die folgenden Dreiecke: PBC, APC, ABP.

Setzen Sie alles zusammen, haben wir die richtige

int x1 = 0, y1 = 0; 
int x2 = 0, y2 = 100; 
int x3 = 200, y3 = 0; 

// no need to divide by 2.0 here, since it is not necessary in the equation 
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); 
double ABP = Math.abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2)); 
double APC = Math.abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y)); 
double PBC = Math.abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2)); 

boolean isInTriangle = ABP + APC + PBC == ABC; 
Verwandte Themen