求两圆的交点(Java)

</pre><pre name="code" class="java">package com.hj.graphics;


import java.util.ArrayList;


import com.hj.tools.NumberTools;


public class Circle {
public static final int INSIDE = 1;
public static final int ON = 2;
public static final int OUTSIDE = 3;
// ----------------------------------
private int x;
private int y;
private int r;


public Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}


// **************************************************
public ArrayList<Point> getIntersections(Circle mc) {
ArrayList<Point> intersections = new ArrayList<Point>();
int count = getIntersectionCount(mc);
// 如果没有交点 返回
if (count == 0)
return intersections;
// -----------------------
long p = 2 * r * (x - mc.getX());
long q = 2 * r * (y - mc.getY());
long k = mc.getR() * mc.getR() - r * r - (x - mc.getX())
* (x - mc.getX()) - (y - getY()) * (y - getY());
long a = p * p + q * q;
long b = -2 * p * k;
long c = k * k - q * q;
long det = b * b - 4 * a * c;
double cos, sin;
if (det >= 0) {
cos = (Math.pow(det, 0.5) - b) / (2 * a);
sin = Math.pow(1 - Math.pow(cos, 2), 0.5);
Point pt = getPointByRadius(cos, sin);
if (point2Circle(pt, mc) == ON && intersections.size() < count) {
intersections.add(pt);
}
pt = getPointByRadius(-cos, sin);
if (point2Circle(pt, mc) == ON && intersections.size() < count) {
intersections.add(pt);
}
pt = getPointByRadius(cos, -sin);
if (point2Circle(pt, mc) == ON && intersections.size() < count) {
intersections.add(pt);
}
pt = getPointByRadius(-cos, -sin);
if (point2Circle(pt, mc) == ON && intersections.size() < count) {
intersections.add(pt);
}
}
// -----------------------
return intersections;
}


public int getIntersectionCount(Circle mc) {
int count = 0;
long diss = (long) (Math.pow((x - mc.getX()), 2) + Math.pow((y - mc
.getY()), 2));
int dis = NumberTools.double2int(Math.pow(diss, 0.5));
int i = dis - r - mc.getR();
if (i == 0) {
count = 1;
} else if (i < 0) {
count = 2;
}
System.out.println("count is:" + count + "   " + dis + " vs "
+ (r + mc.getR()));
return count;
}


// **************************************************
public static int point2Circle(Point pt, Circle mc) {
long diss = (long) (Math.pow((pt.getX() - mc.getX()), 2) + Math.pow((pt
.getY() - mc.getY()), 2));
int dis = NumberTools.double2int(Math.pow(diss, 0.5));
System.out.println("is on circle:" + dis + " vs " + mc.getR());
int i = -1;
if (dis > mc.getR())
i = OUTSIDE;
else if (dis == mc.getR())
i = ON;
else
i = INSIDE;
return i;
}


// **************************************************
public int getX() {
return x;
}


public int getY() {
return y;
}


public int getR() {
return r;
}


// 
public int getDiameter() {
return 2 * r;
}


// 
public int getLeftTopX() {
return x - r;
}


public int getLeftTopY() {
return y - r;
}


// get x,y according to radius
public Point getPointByRadius(double cos, double sin) {
double x0 = (r * cos + x), y0 = (r * sin + y);
return new Point(NumberTools.double2int(x0), NumberTools.double2int(y0));
}


}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值