计算哪个绘制的圆最接近另一个圆
Calculating which drawn circle is the nearest to another one
我想得到离我的特殊圆最近距离的圆的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) {...
的部分可能不正确。
2015 年 9 月 21 日编辑:
这就是 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++;
}
我假设 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);
直接复制粘贴就可以了 ╰( ͡° ͜ʖ ͡° )つ──☆*:・゚
无需遍历Array两次,只需将最近的圆的索引保存在一个变量中
我想得到离我的特殊圆最近距离的圆的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) {...
的部分可能不正确。
2015 年 9 月 21 日编辑:
这就是 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++;
}
我假设 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);
直接复制粘贴就可以了 ╰( ͡° ͜ʖ ͡° )つ──☆*:・゚
无需遍历Array两次,只需将最近的圆的索引保存在一个变量中