简体   繁体   中英

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. 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. 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”. 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. Store their input in an array of Circles of the appropriate size. 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.

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:

//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)

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

(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

if you measure L from centre point to centre point (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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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