give a number x and y coordinates

hi, i have this code and i was wondering how can i give a random number from a matrix, x an y coordinates. for example say i have a matrix with dimensions of 8 by 8 containing numbers from 1 to 64. and i took the number 31, how can i give it x and y coordinates, can anyone help me with this ?

Re: give a number x and y coordinates

Is your question how to convert a number in the range 0-63 into the row and column values to index a 2D array?

To go the other way, given row and column: row*row_length+column

Use some algebra on that equation to get the other one. Look at using % and /

Re: give a number x and y coordinates

Quote:

Originally Posted by

**Norm** Is your question how to convert a number in the range 0-63 into the row and column values to index a 2D array?

To go the other way, given row and column: row*row_length+column

Use some algebra on that equation to get the other one. Look at using % and /

hi sorry about the delay it was late last night when i posted and just checked in the morning to see if i had any replies,

yeah so my question to make it clearer. just say i have colums and rows 4 * 4 and you populate it with the numbers 1-16 .

1***

****

****

**16

in the first row and colum is the number 1 and the last row and colum is the number 16. so just say the x y coordinates for the number 1 are 0,0 . and the the x y coordinate for the number 16 are 4, 4. i was just wondering if i take a random number how could i automatically give it x y coordinates using java

Re: give a number x and y coordinates

Are the numbers in that matrix in proper ascending order?

kind regards,

Jos

Re: give a number x and y coordinates

Quote:

Originally Posted by

**JosAH** Are the numbers in that matrix in proper ascending order?

kind regards,

Jos

yes they are

Re: give a number x and y coordinates

Using the formula I suggested you compute, you'd subtract 1 from the numbers to make them 0 based.

Re: give a number x and y coordinates

The problem is easy then (aamof, you don't need a matrix at all). For any number i in an n*n matrix, that number is stored in row #(i-1)/n and column #(i-1)%n. That's all there is to it.

kind regards,

Jos

Re: give a number x and y coordinates

great guys thanks for help i appreciate, sorry if question was a bit dumb i just new to java

regards

Derek

1 Attachment(s)

Re: give a number x and y coordinates

Hello again i want to change the program i explained above to give x and y coordinates as illustrated in the picture below.

Can anyone help ?

Attachment 3375

regards Derek

Re: give a number x and y coordinates

Your y axis is the opposite direction of how it is used in java programming (java is top down). You will have to convert the java coordinates to your coordinates.

Re: give a number x and y coordinates

my code to assign x y coordinates to positions on the grid is not working properly

any ideas why this would be ?

x = (number-1)%9;

y = (number-1)/9;

Re: give a number x and y coordinates

Can you show some examples?

Have several values of number and show the x,y values that are generated by your code.

Also show how the squares are numbered. Were are square 1 and square 2

Re: give a number x and y coordinates

my code is below , basically i want to take 2 numbers from the user to create a grid, then take a random number from this grid and give it corresponding x y coordinates.

import java.util.Scanner;

import java.util.Random;

public class baby {

public static void main(String args[])

{

System.out.println("enter first number for room length: ");

Scanner scan = new Scanner(System.in);

int num1 = scan.nextInt();

System.out.println("enter second number for room length: ");

int num2 = scan.nextInt();

int room_length = num1 * num2;

System.out.println("The room length is :"+num1 +" x "+num2+" = "+room_length);

Random furniture = new Random();

int table = furniture.nextInt(room_length);

int chair = furniture.nextInt(room_length);

System.out.println("table random number is: "+table);

System.out.println("chair random number is: "+chair);

int x_coordinate = (table -1)%num1;

int y_coordinate = (table -1)/num2;

System.out.println("x coordinate is: "+ x_coordinate +" y coordinate is :" +y_coordinate);

}

}

Re: give a number x and y coordinates

Please execute the program and copy and post here the output showing the value of the number and the values of x and y for several values. Add comments to the output that show what the x, and y values should be.

How are the squares in the grid numbered? Where is square # 1 and square #2?

Re: give a number x and y coordinates

i want grid to be populated as so

1 2 3 4

5 6 7 8

9........

so 1 xy coordinates to be 0,3

2 xy coordinates to 1,3

3 xy coordinates to be 2,3

and so on

////////////////////////

examples of program run

enter first number for room length:

4

enter second number for room length:

4

The room length is :4 x 4 = 16

table random number is: 4

x coordinate is: 3 y coordinate is :0

///////////////////////////////////////////

enter first number for room length:

4

enter second number for room length:

4

The room length is :4 x 4 = 16

table random number is: 9

x coordinate is: 0 y coordinate is :2

///////////////////////////////////////

enter first number for room length:

4

enter second number for room length:

4

The room length is :4 x 4 = 16

table random number is: 7

x coordinate is: 2 y coordinate is :1

Re: give a number x and y coordinates

You want> square 1 (top left corner) xy coordinates to be 0,3

That is not what you said in post#3???

In your post you needed to add comments to the output to show what the values of x,y should be.

7 gives x coordinate: 2 y coordinate: 1 >>> What are the correct values?

Take a piece od paper and draw a grid with the squares. In each square write the square number and the x,y values it should have. Then look at the drawing and find the formulas that will generate the desired values from the square number.

Post the square contents for a couple of squares. Say the top left and the bottom right squares.

Re: give a number x and y coordinates

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

1 = 0,3

2= 1,3

3 = 2,3

4 = 3,3

5 = 0,2

6 = 1,2

7 = 2,2

8 = 3,2

9 = 0,1

10 = 1,1

11 = 2,1

12 = 3,1

13 = 0,0

14 = 1,0

15= 2,0

16 = 3,0

Re: give a number x and y coordinates

thats all i can post for tonight, i'll reply to any futher post tomorrow

regards

derek

Re: give a number x and y coordinates

Now you need to find the formulas to compute those values.

Your x,y values appear to be in column, row order which is the reverse of what I have seen used before.

Normally x is the row number and y is the column number. Are you reversing their common usage also?

You want> x is the column and y is the row?