Java 计算哪个绘制的圆最接近另一个圆

Java 计算哪个绘制的圆最接近另一个圆,java,android,position,distance,geometry,Java,Android,Position,Distance,Geometry,我想得到距离我的特殊圆最小的圆的x和y。我用计时器创建了25个圆圈,需要检查场上绘制的每个圆圈。我已经拥有的是: protected void onDraw (android.graphics.Canvas canvas){ //If arrow-button was clicked, do ... get the circle with the lowest distance to viking circle if (buttonClicked == true) {

我想得到距离我的特殊圆最小的圆的x和y。我用计时器创建了25个圆圈,需要检查场上绘制的每个圆圈。我已经拥有的是:

protected void onDraw (android.graphics.Canvas canvas){

    //If arrow-button was clicked, do ... get the circle with the lowest distance to viking circle
    if (buttonClicked == true) {
        //distance of the current circle from the viking
        int tempCircleDistance = 0;

        //the minimum distance we have found so far in our loop
        int minCircleDistance = 0;

        //index of the min circle we have found so far
        int indexOfNearest=0;
        for(int i = 0; i<circlesOnTheField; i++) 
        {
            //help me Phytagoras
            tempCircleDistance = (int) (Math.sqrt((viking.getX() - circles.get(i).getX())*
                    (viking.getX() - circles.get(i).getX())+
                    (viking.getY() - circles.get(i).getY())*
                    (viking.getY() - circles.get(i).getY()))-
                    (viking.getR() + circles.get(i).getR()));
          //first cycle or did we find the nearest circle? If so update our variables
            if(i==0||tempCircleDistance<minCircleDistance)
            {    
                   indexOfNearest=i;
                   minCircleDistance=tempCircleDistance;
            }
        }
        if(circles.get(indexOfNearest).getIsDrawn() == true) {

            //draw the line with the given index of the nearest circle
            //At this point, nearest circle is calculated and we draw a line from viking to that circle
            canvas.drawLine(viking.getX(), viking.getY(), 
                            circles.get(indexOfNearest).getX(), 
                            circles.get(indexOfNearest).getY(), 
                            pgoal);
            //Here we delete the circle and increase our variable frags for one more killed opponent.
            deleteCircle(circles.get(indexOfNearest));
            circlesOnTheField--;
            frags++;
            buttonClicked = false;
        }
     }
    //END

    //This is where the circles are drawn
    for(int k = 0; k<circlesOnTheField; k++) {
            canvas.drawCircle(circles.get(k).getX(), circles.get(k).getY(), circles.get(k).getR(), p3);
            circles.get(k).setIsDrawn(true);
    }
} 
以及
addCircle()

我有一个计时器执行
addCircle()
,另一个计时器执行
moveCircle()

publicstaticvoidmovecircle(){
对于(inti=0;iIm,假设getR()给出半径?!
然后尝试以下操作,而不是for循环:

//distance of the current circle from the viking
int tempCircleDistance = 0;

//the minimum distance we have found so far in our loop
int minCircleDistance = 0;

//index of the min circle we have found so far
int indexOfNearest=0;

for(int i = 0; i<circlesOnTheField; i++) 
{
        //help me Phytagoras
        tempCircleDistance = (int) (Math.sqrt((viking.getX() - circles[i].getX())*
                                              (viking.getX() - circles[i].getX())+
                                              (viking.getY() - circles[i].getY())*
                                              (viking.getY() - circles[i].getY()))-
                                              (viking.getR() + circles[i].getR()));

        //first cycle or did we find the nearest circle? If so update our variables
        if(i==0||tempCircleDistance<minCircleDistance)
        {    
               indexOfNearest=i;
               minCircleDistance=tempCircleDistance;
        }
}

//draw the line with the given index of the nearest circle
canvas.drawLine(viking.getX(), viking.getY(), 
                circles[indexOfNearest].getX(), 
                circles[indexOfNearest].getY(), 
                pgoal);
//当前圆与viking的距离
int tempCircleDistance=0;
//到目前为止,我们在环路中找到的最小距离
int minCircleDistance=0;
//到目前为止我们已经找到的最小圆的索引
int indexOfNearest=0;

对于
if(i>=1){…
部分中的(int i=0;iI),为什么只检查if
circledistance[i]
比上一个小,而不是比所有其他距离都小?如果从圆到您的特殊圆的距离小于任何其他圆到特殊圆的距离,则该圆应仅分配给
箭头圆
。我认为如果从第一个圆开始,只看一眼就可以了在下一个,因为它应该总是一个较小的值,对吗?考虑如下:画一个特殊的圆。然后画第二个圆,叫它A。假设它与特殊圆的距离是5(根据距离的一些定义)。现在画第三个和第四个圆,分别是B和C。在画B之后,检查它与a的距离。让我们假设B与特殊圆的距离为7,因此a仍然是最近的。在画C之后,我们检查它与B的距离,让我们假设C的距离为6,然后<代码>距离(C)<距离(B)
。但是仍然要对照A检查,否则它会错误地导致C是最接近的。哇,你是个魔术师:D!这真的很有效!哈哈!我只是在想一个新主意。我可以使用直线作为向量,并以预定义的速度移动位图(如果可能的话,旋转)使用这个向量指向最近的圆?只是想法!非常感谢!!!可能是的。你有起点和终点,你有长度,没有什么可以反对它。我假设像一个炮弹攻击最近的圆或什么?!对!像一把投掷斧!继续尝试吧!用所需的变量创建一个向量类,然后尝试utOk在尝试之前,我对删除最近的圆有一个问题。只要我不使用删除方法,我的viking circle就会得到一条到最近圆的连接线(他们总是向下移动)。然后,当我把删除放进去时,这条线会非常快地画成两到三个圆圈,有时会在两秒钟后画出一个圆圈。看看我编辑的代码。谢谢
    public static void addCircle() {
    if(circlesOnTheField >= 25) {
        circlesOnTheField = 25;
    }
    else{
    circlesOnTheField++;
    }
}
public static void moveCircle() {
    for(int i=0; i<circlesOnTheField; i++) {
        //Move circles downwards
        circles.get(i).setY(circles.get(i).getY()+5);

        //Check if the circle collides with the viking
        if(detectCollision(viking, circles.get(i))) {
            deleteCircle(circles.get(i));
            circles.get(i).setIsDrawn(false);
            life--;
        }

        //Check if the circle intersects the goal line and recreate it if yes
        if(intersects(circles.get(i).getX(), circles.get(i).getY(), circles.get(i).getR(), 0, 750, 500, 760)) {
            deleteCircle(circles.get(i));
            circles.get(i).setIsDrawn(false);
            circlesInGoal++;
        }
    }
}
public static void createNewCircleOnCanvas() {
    //Collision Detection
    circles.clear();
    int createdCircles = 0;
     outer: while (createdCircles < 25) {
        int randomX = r.nextInt(500);
        int randomY = r.nextInt(300);
       candidate = new Circle(randomX, randomY, 33, "Circle"+createdCircles, false);
        inner: for (int z = 0; z<createdCircles;z++) {
            //If new created circle collides with any already created circle or viking, continue with outer
              if (detectCollision(candidate, circles.get(z))) continue outer;
        }
       circles.add(candidate);
       createdCircles++;
    }
//distance of the current circle from the viking
int tempCircleDistance = 0;

//the minimum distance we have found so far in our loop
int minCircleDistance = 0;

//index of the min circle we have found so far
int indexOfNearest=0;

for(int i = 0; i<circlesOnTheField; i++) 
{
        //help me Phytagoras
        tempCircleDistance = (int) (Math.sqrt((viking.getX() - circles[i].getX())*
                                              (viking.getX() - circles[i].getX())+
                                              (viking.getY() - circles[i].getY())*
                                              (viking.getY() - circles[i].getY()))-
                                              (viking.getR() + circles[i].getR()));

        //first cycle or did we find the nearest circle? If so update our variables
        if(i==0||tempCircleDistance<minCircleDistance)
        {    
               indexOfNearest=i;
               minCircleDistance=tempCircleDistance;
        }
}

//draw the line with the given index of the nearest circle
canvas.drawLine(viking.getX(), viking.getY(), 
                circles[indexOfNearest].getX(), 
                circles[indexOfNearest].getY(), 
                pgoal);