简体   繁体   English

多个坐标之间的距离

[英]Distance between multiple coordinates

I am completely lost on how to sort the coordinates into an array, and find the distance between them.我完全不知道如何将坐标排序到数组中,并找到它们之间的距离。 This is the question:这是问题:

  1. Create a new class called “Circle” that can be used to create customized circle objects.创建一个名为“Circle”的新类,可用于创建自定义圆形对象。 Your class should include the following – be sure to comment your class appropriately: double radius, double xPosition, double yPosition, and A method that computes the distance from the xPosition and yPosition of one circle to the xPosition and yPosition of another circle.你的类应该包括以下内容——确保对你的类进行适当的评论:双倍半径、双倍 xPosition、双 yPosition,以及一种计算从一个圆的 xPosition 和 yPosition 到另一个圆的 xPosition 和 yPosition 的距离的方法。 Use the standard distance formula to compute this value.使用标准距离公式计算该值。 You only need to compute the distance from center point to center point for the purposes of this method.出于此方法的目的,您只需要计算从中心点到中心点的距离。 Here's a method header to get you started:这是一个让您开始的方法标题:

    public double distanceFrom(Circle test)公共双距离(圆测试)

  2. Create a new class called “Assignment06b”.创建一个名为“Assignment06b”的新类。 Do the following in this class:在这个类中执行以下操作:

    Prompt the user to enter in a number of circles (ie How many circles do you want to create?) Next, ask the user to enter in the radius, xPosition and yPosition for each circle.提示用户输入多个圆(即您要创建多少个圆?) 接下来,让用户输入每个圆的半径、xPosition 和 yPosition。 Store their input in an array of Circles of the appropriate size.将他们的输入存储在适当大小的 Circles 数组中。 Finally, Iterate through your array and display distance information for each circle.最后,遍历数组并显示每个圆的距离信息。 Ensure that you do not calculate the distance from a given circle back to itself (ie no need to compute distance between circle #1 and circle #1) — Here's a sample running of your program.确保您不计算从给定圆回到其自身的距离(即不需要计算圆 #1 和圆 #1 之间的距离)——这是您程序的示例运行。

Here's what I have so far:这是我到目前为止所拥有的:

import java.util.Scanner;

public class Assignment06b 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in); 
        System.out.print("How many circles do you want to create?:");
        int amount = input.nextInt();


        int[] arrayX = new int [amount];
        int[] arrayY = new int [amount];


        int counter = 0;
        for (counter = 0; counter < amount; counter++)
        {
            System.out.println("Enter info for Circle #" + (counter + 1));
            System.out.print("Radius: ");
            double width = input.nextDouble();

            System.out.print("X Position: ");
            arrayX[counter] = input.nextInt();

            System.out.print("Y Position:");
            arrayY[counter] = input.nextInt();
        }

    }

    class Circle 
    {
        double radius;
        double xPosition;
        double yPosition; 

        Circle(double radius, double xPosition, double yPosition)
        {

        }

        public double distanceFrom(Circle test)
        {
            double equation = (xPosition-xPosition)*(xPosition-xPosition) + (yPosition-yPosition)*(yPosition-yPosition);
            double answer = Math.pow(equation, 0.5);
            return answer;
        }
    }
}

You are tip toeing around object orientation here, change things around so you have an array of circles instead of an array of ints:你在这里小心翼翼地围绕对象方向,改变周围的东西,所以你有一个圆圈数组而不是一个整数数组:

Circle[] arrayCircles = new Circle [amount];

Also you aren't setting any values in your circle class, you probably want to fix that:此外,您没有在圆类中设置任何值,您可能想要解决这个问题:

Circle(double radius, double xPosition, double yPosition)
{
     this.radius = radius;
     this.xPosition = xPosition;
     this.yPosition = yPosition; 
}

Then you can add circles to your collection like this:然后,您可以像这样将圈子添加到您的收藏中:

arrayCirles[0] = new Circle(myRadius, myXPosition, myYPosition);

and call your distanceFrom method call like so:并像这样调用您的 distanceFrom 方法调用:

//Obviously do this in a loop of some kind and make sure they exist first
arrayCircles[0].distanceFrom(arrayCircles[1]);

Hopefully the rest you can figure out yourself希望剩下的你能自己弄清楚

(Also take another look at your distanceFrom method, you want to compare the circle you are passed as a parameter, not to yourself) (另外再看看你的 distanceFrom 方法,你想比较你作为参数传递的圆,而不是你自己)

I'm currently working in a CAD software and I needed to find the distance between circles (holes) in a square grid/array and I found that the distance between circles (edge to edge) is given by我目前正在使用 CAD 软件,我需要在方形网格/阵列中找到圆(孔)之间的距离,我发现圆之间的距离(边到边)由下式给出

(LN*D)/(N-1) (LN*D)/(N-1)

Where: L is the distance of the array from edge to edge (not to the centre point of the end circle but to the edge of end circles) N is the number of circles D is the diameter of the circles其中: L是阵列从边到边的距离(不是到末端圆的中心点,而是到末端圆的边缘) N是圆的数量D是圆的直径

if you measure L from centre point to centre point (L+DN*D)/(N-1)如果从中心点到中心点测量 L (L+DN*D)/(N-1)

If you're looking to find the distance between centre point to centre point I'm sure you can derive it如果您想找到中心点到中心点之间的距离,我相信您可以推导出它

Bit late but hopefully this helps someone else!有点晚了,但希望这对其他人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM