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

时间:2015-09-20 11:41:37

标签: 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) {
        //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);
    }
} 

因此,我将我的圈子存储在名为circles[]的数组中,并使用我固定的第二个圈viking来计算距离。{变量arrowCircle应该存储最近的圆的名称。然后我想在最近的圆圈到维京圆圈之间划一条线。

任何想法有什么不对?

提前致谢 我认为if(i>=1) {...的部分可能不正确。

编辑于21.09.15:

这是deleteCircle()上发生的事情:

    public static void deleteCircle(Circle circle) {
    circles.remove(circle);
    circlesOnTheField--;
}

addCircle()

    public static void addCircle() {
    if(circlesOnTheField >= 25) {
        circlesOnTheField = 25;
    }
    else{
    circlesOnTheField++;
    }
}

我有一个执行addCircle()的计时器和另一个执行moveCircle()的计时器:

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++;
    }

1 个答案:

答案 0 :(得分:0)

我假设getR()给出半径?! 然后尝试这个而不是你的for循环:

--debug-output

只需复制粘贴即可神奇地工作╰(͡°͜ʖ͡°)つ──☆*:·゚

无需经历数组两次,只需在变量中保存最近圆的索引