定义一个圆类,并判断一个点是否在圆的内部(圆心用二维的点来表示)。 用java语言表示
2个回答
展开全部
Note类:
package cycle;
//点的抽象类
public class Note {
private int x; //x轴坐标
private int y; //y轴坐标
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
圆类:
package cycle;
//圆类
public class Cycle {
private Note note; //圆心
private int r; //半径
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
测试类:
package cycle;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Note n = new Note(); //定义圆心
n.setX(0);
n.setY(0); //此处圆心可随意设置
Cycle c = new Cycle(); //定义圆
c.setNote(n);
c.setR(10);//此处半径可随意设置
Note note = new Note();
note.setX(5);
note.setY(5);//此处点可随意设置
isIn(c, note);
}
public static void isIn(Cycle c, Note n){
int x = c.getNote().getX() - n.getX();
int y = c.getNote().getY() - n.getY();
double d = Math.sqrt(x * x + y * y);
if(d > c.getR()){
System.out.println("点在圆外。");
}else if(d < c.getR()){
System.out.println("点在圆内。");
}else{
System.out.println("点在圆上。");
}
}
}
package cycle;
//点的抽象类
public class Note {
private int x; //x轴坐标
private int y; //y轴坐标
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
圆类:
package cycle;
//圆类
public class Cycle {
private Note note; //圆心
private int r; //半径
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
测试类:
package cycle;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Note n = new Note(); //定义圆心
n.setX(0);
n.setY(0); //此处圆心可随意设置
Cycle c = new Cycle(); //定义圆
c.setNote(n);
c.setR(10);//此处半径可随意设置
Note note = new Note();
note.setX(5);
note.setY(5);//此处点可随意设置
isIn(c, note);
}
public static void isIn(Cycle c, Note n){
int x = c.getNote().getX() - n.getX();
int y = c.getNote().getY() - n.getY();
double d = Math.sqrt(x * x + y * y);
if(d > c.getR()){
System.out.println("点在圆外。");
}else if(d < c.getR()){
System.out.println("点在圆内。");
}else{
System.out.println("点在圆上。");
}
}
}
展开全部
package com.Circle;
public class Circle {
//define the center of the circle is (x,y)
private double x;
private double y;
//the rayon of the circle
private double rayon;
public Circle(double x, double y, double rayon){
this.x = x;
this.y = y;
this.rayon = rayon;
}
public double getDistance(double a, double b){
double _x = Math.abs(this.x - a);
double _y = Math.abs(this.y - b);
return Math.sqrt(_x*_x+_y*_y);
}
public boolean isInCircle(double a, double b){
boolean result = true;
System.out.println("The distance: " + this.getDistance(a, b));
if(this.getDistance(a, b) > this.rayon){
result = false;
}
return result;
}
public static void main(String[] args){
Circle circle = new Circle(2.0, 3.0, 5);
System.out.println("The result: " + circle.isInCircle(3.0, 4.0));
}
}
public class Circle {
//define the center of the circle is (x,y)
private double x;
private double y;
//the rayon of the circle
private double rayon;
public Circle(double x, double y, double rayon){
this.x = x;
this.y = y;
this.rayon = rayon;
}
public double getDistance(double a, double b){
double _x = Math.abs(this.x - a);
double _y = Math.abs(this.y - b);
return Math.sqrt(_x*_x+_y*_y);
}
public boolean isInCircle(double a, double b){
boolean result = true;
System.out.println("The distance: " + this.getDistance(a, b));
if(this.getDistance(a, b) > this.rayon){
result = false;
}
return result;
}
public static void main(String[] args){
Circle circle = new Circle(2.0, 3.0, 5);
System.out.println("The result: " + circle.isInCircle(3.0, 4.0));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询