本题的关键是计算你输入的那个点的坐标与圆心的距离是否小于等于半径就可以了,假设单位圆的圆心是(0,0), 半径是1, 你输入一个坐标为(x, y) 那么计算(x, y)到(0, 0)的距离如果小于等于1就可以了,具体代码如下:
class Circle {
public Point p; // 圆心坐标
public float r; // 半径
public Circle(Point p, float r) {
this.p = p;
this.r = r;
}
}
class Point {
public float x; // x坐标
public float y; // y坐标
public Point(float x, float y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
// 构造单位圆
Circle circle = new Circle(new Point(0, 0), 1);
// 你输入的点坐标
Point point = new Point(0.5, 0.5);
// 输出结果
System.out.println(isInCircle(circle, point));
}
// 判断某个点是否在圆内火圆的边上面
public static String isInCircle(Circle circle, Point point) {
// 计算该点到圆心的距离
float distance = Math.sqrt( // 开平方
Math.pow(Math.abs(point.x - circle.p.x), 2), // 直角边平方
Math.pow(Math.abs(point.y - circle.p.y), 2), // 直角边平方
);
// 本质上就是计算三角形的斜边长度
return distance <= circle.r ? "Y" : "N";
}
}