「java圆关系」判断两个圆关系java代码

博主:adminadmin 2023-01-18 09:12:05 444

本篇文章给大家谈谈java圆关系,以及判断两个圆关系java代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java求两个圆关系的代码,,求完整的,正确的源代码,谢谢!!

public class Cycle {

private double x = 0;//圆心横坐标

private double y = 0;//圆心纵坐标

private double r = 0;//圆心半径

public static void main(String[] args) {

String relation = "";

Cycle c = new Cycle(0,0,1);

//相交 外切 内切 相离

Cycle c_xiangJiao = new Cycle(3,4,5);

Cycle c_waiQie = new Cycle(3,4,4);

Cycle c_neiQie = new Cycle(3,4,6);

Cycle c_xiangLi = new Cycle(3,4,2);

relation = c.relationWithOtherCycle(c_xiangJiao);

System.out.println("c c_xiangJiao relationShip :"+relation);

relation = c.relationWithOtherCycle(c_xiangLi);

System.out.println("c c_xiangLi relationShip :"+relation);

relation = c.relationWithOtherCycle(c_neiQie);

System.out.println("c c_neiQie relationShip :"+relation);

relation = c.relationWithOtherCycle(c_waiQie);

System.out.println("c c_waiQie relationShip :"+relation);

}

public Cycle(double x, double y, double r) {

this.r = r;

this.x = x;

this.y = y;

}

public Cycle() {

}

public String relationWithOtherCycle(Cycle c){

String relation = ""; //相交 外切 内切 相离

double length = 0;// (x-x1)*(x-x1)+(y-y1)*(y-y1) 开平方

length = Math.sqrt((this.x-c.getX())*(this.x-c.getX())+(this.y-c.getY())*(this.y-c.getY()));

//System.out.println("length : "+length);

if(length(this.r+c.getR())){

relation = "相离";

}else if (length==(this.r+c.getR())){

relation = "外切";

}else if (length==Math.abs(this.r-c.getR())){

relation = "内切";

}else if (lengthMath.abs(this.r+c.getR())lengthMath.abs(this.r-c.getR())){

relation = "相交";

}

return relation;

}

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 double getR() {

return r;

}

public void setR(double r) {

this.r = r;

}

}

//把测试程序写在Cycle的main方法里了

java,设计一个类,用于计算圆的面积,周长以及判定一个点与这个圆的位置关系

public class Circle{

private float r;//半径

public void setR(float r){this.r = r;}

public float getR(){return this.r;}

public float getS(){//计算面积

return 3.14*r*r;

}

public float getL(){//计算周长

return 2*3.14*r;

}

public int getPlace(float x,float y){//我直接给定某个点的坐标值,你也可以定义一个点Point的对象;

float z = x*x +y*y;

int rs = 0;//默认情况点在圆外

if(r*r z) rs =1;//点在圆内

else if(r*r == z) rs = 2;//点在圆上;

return rs;//这里用整型来代表三种关系

}

}

java判断两个圆关系(相离,相切,相交)求代码

Java程序:

import java.awt.Point;

public class HardWork {

public static void main(String[] args) {

Circle c1 = new Circle(2, new Point(2, 2));

Circle c2 = new Circle(2, new Point(6, 2));

Circle c3 = new Circle(1, new Point(6, 4));

if(c2.isEqual(c3)) {

System.out.println("c2、c3两个圆相同");

}

else {

System.out.println("c2、c3两个圆不相同");

}

if(c1.getRelashionShip(c2) == RelationShip.DISJOINT) {

System.out.println("c1、c2两个圆相离");

}

else if(c1.getRelashionShip(c2) == RelationShip.TANGENCY) {

System.out.println("c1、c2两个圆相切");

}

else {

System.out.println("c1、c2两个圆相交");

}

if(c2.getRelashionShip(c3) == RelationShip.DISJOINT) {

System.out.println("c2、c3两个圆相离");

}

else if(c2.getRelashionShip(c3) == RelationShip.TANGENCY) {

System.out.println("c2、c3两个圆相切");

}

else {

System.out.println("c2、c3两个圆相交");

}

if(c1.getRelashionShip(c3) == RelationShip.DISJOINT) {

System.out.println("c1、c3两个圆相离");

}

else if(c1.getRelashionShip(c3) == RelationShip.TANGENCY) {

System.out.println("c1、c3两个圆相切");

}

else {

System.out.println("c1、c3两个圆相交");

}

}

}

/**

* 圆类

* @author developer

* @version 2016.05.02

*/

class Circle {

/**

* 圆的半径

*/

private double radius;

/**

* 圆心坐标

*/

private Point point;

/**

* 圆周率

*/

public final static double PI = 3.14159;

/**

* 构造方法

*/

public Circle(){

this.radius = 0;

}

/**

* 构造方法

* @param radius 圆的半径

* @param point 圆心坐标

*/

public Circle(double radius, Point point) {

this.radius = radius;

this.point = (Point) point.clone();

}

/**

* 返回圆的面积

* @return 圆的面积

*/

public double getArea() {

return PI * this.radius * this.radius;

}

/**

* 返回圆的周长

* @return 圆的周长

*/

public double getPerimeter() {

return PI * this.radius * 2;

}

/**

* 判断两个圆是否相同,即圆心位置相同并且半径相等

* @param c 另一个圆

* @return 两个圆是否相同,true:相同,false:不相同

*/

public boolean isEqual(Circle c) {

if(this.radius == c.radius this.point.equals(c.point)) {

return true;

}

return false;

}

/**

* 返回两个圆的位置关系,相离、相切或相交

* @param c 另一个圆

* @return 两个圆的位置关系,相离、相切或相交br/

* RelationShip.TANGENCY 相离br/

* RelationShip.DISJOINT 相切br/

* RelationShip.INTERSECT 相交

*/

public RelationShip getRelashionShip(Circle c) {

double distance = 0;

distance = Math.sqrt((c.point.y - this.point.y)*(c.point.y - this.point.y)

+ (c.point.x - this.point.x)*(c.point.x - this.point.x));

if(distance == this.radius + c.radius) {

return RelationShip.TANGENCY;

}

else if(distance this.radius + c.radius) {

return RelationShip.DISJOINT;

}

return RelationShip.INTERSECT;

}

}

/**

* 圆的关系枚举

* @author developer

* @version 2016.05.02

*/

enum RelationShip {

/**

* 相离

*/

DISJOINT,

/**

* 相切

*/

TANGENCY,

/**

* 相交

*/

INTERSECT

};

运行测试:

c2、c3两个圆不相同

c1、c2两个圆相切

c2、c3两个圆相交

c1、c3两个圆相离

关于java圆关系和判断两个圆关系java代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。