判断点于圆和矩形的位置关系
//定义点为父类
package com.jredu.ch_Package2;
public class Point {
public double x;
public double y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public Point(){
}
}
//定义圆
package com.jredu.ch_Package2;
public class Circular extends Point{
public double r;
public Circular() {
super();
}
public Circular(double x, double y, double r) {
super(x, y);
this.r = r;
}
public String judge(double x2,double y2){
String c ;
if ((x2-x)*(x2-x)+(y2-y)*(y2-y) < r*r) {
c = "内";
}else if ((x2-x)*(x2-x)+(y2-y)*(y2-y) == r*r) {
c = "上";
}else {
c = "外";
}
return c;
}
}
//定义矩形
package com.jredu.ch_Package2;
public class Rectangle extends Point {
public double a;
public double b;
public Rectangle() {
super();
}
public Rectangle(double x, double y, double a, double b) {
super(x, y);
this.a = a;
this.b = b;
}
public String judge(double x1,double y1){
String c ;
if (x1-x>0&&x1-x<a&&y1-y>0&&y1-y<b) {
c = "内";
}else if ((x1-x == a||x1-x==0)&&y1-y>=0&&(y1-y<=b||
y1-y == b||y1-y==0)&&y1-y>=0&&y1-y<=b) {
c = "上";
}else {
c = "外";
}
return c;
}
}
//测试函数
package com.jredu.ch_001;
import java.util.Scanner;
import com.jredu.ch_Package2.Circular;
import com.jredu.ch_Package2.Rectangle;
public class LiuYiFan20160807_Point {
public static void main(String[] args) {
System.out.println("输入坐标点判断与矩形的关系");
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
double y1 = input.nextDouble();
Rectangle rectangle = new Rectangle(0,0,1,2);
String c = rectangle.judge(x1,y1);
System.out.println(c);
System.out.println("输入坐标点判断与圆的关系");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Circular circular = new Circular(0,0,1);
String d = circular.judge(x2,y2);
System.out.println(d);
input.close();
}
}