希望能尽量完整确实学不懂了TVT
1、我想要达到的结果
定义一个圆类用来表示二维空间中的一个圆,包括了圆心坐标和半径
可以生成一个具体的圆,写出构造函数
可以分别提供设置圆心坐标和半径的方法
提供一个判断空间一个点在圆内还是圆上,还是圆外的方法
实例化一个坐标为4,5,半径为6的圆,计算点(9,9)点是否在圆上
2、问题遇到的现象和发生背景
在站里只找到了怎么建立圆类的,比划着写了一点点但是后面就不会了
4、 我的解答思路和尝试过的方法
试着创建了圆类,但是不知道该怎么生成构造函数什么的,书上也没有
给个例子,你参考一下吧:
class Point {
private double x;
private double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public class Circle{
private double radius;
private Point center;
public Circle(double radius, Point center){
this.radius = radius;
this.center = center;
}
public void caclPosition(Point p){
double dist = Math.sqrt(Math.pow(p.getX()-this.center.getX(), 2)+Math.pow(p.getY()-this.center.getY(), 2));
if(dist==this.radius){
System.out.println("点p在圆上");
}else if(dist<this.radius){
System.out.println("点p在圆内");
}else{
System.out.println("点p在圆外");
}
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public static void main(String[] args) {
Point center = new Point(4,5);
Circle c = new Circle(6, center);
Point p = new Point(9,9);
c.caclPosition(p);
}
}
你这个生成一个具体的圆,是要弹窗画个圆?那需要java桌面开发的库,可以用javafx
可以参考一下这个代码
/**
/*java已知圆点坐标和半径,得到圆上的点
/*CIRCLE_CENTER_X ,CIRCLE_CENTER_Y 圆心坐标
/*CIRCLE_R 圆半径
**/
List points = new LinkedList<Point>();
private void initPointsCircular() {
for (int i = 0; i < 360; i += 1) {
int x = (int) (Configs.CIRCLE_CENTER_X - Configs.CIRCLE_R * Math.sin(Math.PI * (i - 90) / 180));
int y = (int) (Configs.CIRCLE_CENTER_Y - Configs.CIRCLE_R - 10
+ Configs.CIRCLE_R * Math.cos(Math.PI * (i - 90) / 180));
points.add(new Point(x, y));
}
}