Skip to content

Instantly share code, notes, and snippets.

@ntuaha
Created September 21, 2014 14:45
Show Gist options
  • Save ntuaha/05e3084c1d91ed8e288b to your computer and use it in GitHub Desktop.
Save ntuaha/05e3084c1d91ed8e288b to your computer and use it in GitHub Desktop.
ACM478
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] test_input = new String[1000];
String str = "" ;
int i = 0 ;
int star = 0 ;
//可以全部資料讀到陣列test_input裡,*在test_input[star]裡
while(str.equals("*")!=true){
str = input.nextLine() ;
test_input[i] = str ;
if(str.equals("*"))
star = i ;
i++ ;
}
i-- ;
//point_m = test_input[star+m]
float pointX ;
float pointY ;
int m = 0 ;
String test_point = input.nextLine() ;
String[] test_reg ;
int p = 0 ;
while(test_point.equals("9999.9 9999.9") != true){
test_reg = test_point.split(" ") ;
pointX = Float.parseFloat(test_reg[0]);
pointY = Float.parseFloat(test_reg[1]);
int test_fig = 0 ;
int exist_fig = 0;
while(test_fig < star){
float[] position = new float[6];
String[] fig = test_input[test_fig].split(" ");
for(int j = 1 ; j < fig.length ; j++){
position[j-1] = Float.parseFloat(fig[j]);
}
if(fig[0].equals("r"))
if(rectangular(pointX,pointY,position[0],position[1],position[2],position[3])==1){
System.out.println("Point "+(p+1)+" is contained in figure "+(test_fig+1));
exist_fig++ ;
}
if(fig[0].equals("c"))
if(circular(pointX,pointY,position[0],position[1],position[2])==1){
System.out.println("Point "+(p+1)+" is contained in figure "+(test_fig+1));
exist_fig++;
}
if(fig[0].equals("t"))
if(triangular(pointX,pointY,position[0],position[1],position[2],position[3],position[4],position[5])==1){
System.out.println("Point "+(p+1)+" is contained in figure "+(test_fig+1));
exist_fig++ ;
}
test_fig++ ;
}
if(exist_fig == 0)
System.out.println("Point "+(p+1)+" is not contained in any figure");
test_point = input.nextLine() ;
p++ ;
}
}
//(要求的點x,要求的點y,矩形左上x,矩形左上y,矩形右下x,矩形右下y)
public static int rectangular(float x , float y , float ax , float ay , float bx , float by){
if( ax < x && x < bx && by < y && y < ay)
return 1 ;
else {
return 0 ;
}
}
public static int circular(float x , float y , float ox , float oy , float r){
double a = Math.pow(ox-x,2) ;
double b = Math.pow(oy-y,2) ;
double d = a+b;
if (d > r*r)
return 0 ;
else {
return 1 ;
}
}
public static int triangular(float x ,float y , float ax ,float ay, float bx ,float by, float cx ,float cy){
double result = Math.abs((ax * by + bx * cy + cx* ay- bx * ay - cx * by - ax * cy) / 2.0D);
double area = Math.abs((x * by + bx * cy + cx* y- bx * y - cx * by - x * cy) / 2.0D);
area+= Math.abs((ax * y + x * cy + cx* ay- x * ay - cx * y - ax * cy) / 2.0D);
area+= Math.abs((ax * by + bx * y + x* ay- bx * ay - x * by - ax * y) / 2.0D);
double epsilon = 1e-2;
if (Math.abs(result - area) < epsilon) {
return 1;}
else {
return 0 ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment