Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created April 28, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/e307db96015fb30be7fc965a97e6145b to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/e307db96015fb30be7fc965a97e6145b to your computer and use it in GitHub Desktop.
package maricka.kolokolova;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
double x;
double y;
System.out.println("Input x:");
x = sc.nextDouble();
System.out.println("Input y:");
y = sc.nextDouble();
int xa = 0;
int ya = 0;
int xb = 4;
int yb = 4;
int xc = 6;
int yc = 1;
double s;
double sbc;
double sac;
double sab;
s = (xa - xc) * (yb - yc) - (xb - xc) * (ya - yc);
sbc = (x - xc) * (yb - yc) - (xb - xc) * (y - yc);
sac = (xa - xc) * (y - yc) - (x - xc) * (ya - yc);
sab = (x - xb) * (ya - yb) - (xa - xb) * (y - yb);
if ((s < 0 && s <= sbc && sbc <= 0 && s <= sac && sac <= 0 && s <= sab && sab <= 0)
|| (s > 0 && s >= sbc && sbc >= 0 && s >= sac && sac >= 0 && s >= sab && sab >= 0)) {
System.out.println("Your point is inside the triangle.");
} else {
System.out.println("Your point is outside the triangle.");
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment