写一个Point类,计算两个Point实例之间的距离;写一个判断点是否在圆内的方法。
2009-07-15 09:02
//测试类
public class task0714{
public static void main(String[] args){
Point p1 = new Point(); Point p2 = new Point(); p1.setX(1); p1.setY(1); p2.setX(5); p2.setY(4); double d = Util.distencePP(p1,p2); System.out.println("点("+p1.getX()+","+p1.getY()+")和点("+p2.getX()+","+p2.getY()+")的距离为:"+d); //--------------------------------------------------------------- Circle c = new Circle(); c.setCC(p2); c.setR(5); String s = Util.distencePC(p1,c); System.out.print("点("+p1.getX()+","+p1.getY()+")在以点("+c.getCC().getX()+","+c.getCC().getY()+")为圆心、半径"+c.getR()+"的"+s); } }
//计算方法
public class Util{
public static double distencePP(Point p1, Point p2){//计算两点之间的距离
double d1 = Math.hypot((p1.getX() - p2.getX()),(p1.getY() - p2.getY())); return d1; } public static String distencePC(Point p,Circle c){//判断点与圆心之间的距离和圆半径的关系 String s ; double d2 = Math.hypot( (p.getX() - c.getCC().getX() ), (p.getY() - c.getCC().getY()) ); double r = c.getR(); if(d2 > r){ s = "圆外"; }else if(d2 < r){ s = "圆内"; }else{ s = "圆上"; } return s; } }
//点类
public class Point{
private int x = 0;
private int y = 0; public void setX(int a){ x = a; } public void setY(int b){ y = b; } public int getX(){ return x; } public int getY(){ return y; } } //圆类
public class Circle{
private double r;
private Point cc; public void setR(double a){ r = a; } public void setCC(Point centerOfCir){ cc = centerOfCir; } public double getR(){ return r; } public Point getCC(){ return cc; } }
|