Results 1 to 19 of 19
  1. #1
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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 ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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 /
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: give a number x and y coordinates

    Quote Originally Posted by Norm View Post
    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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: give a number x and y coordinates

    Are the numbers in that matrix in proper ascending order?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: give a number x and y coordinates

    Quote Originally Posted by JosAH View Post
    Are the numbers in that matrix in proper ascending order?

    kind regards,

    Jos
    yes they are

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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

  9. #9
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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 ?

    give a number x and y coordinates-x_y.jpg

    regards Derek

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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;

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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);


    }

    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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

  18. #18
    dmcb is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 02:14 AM
  2. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-05-2011, 12:08 AM
  3. Replies: 1
    Last Post: 10-28-2011, 09:18 AM
  4. Coordinates
    By Witik in forum New To Java
    Replies: 3
    Last Post: 09-06-2010, 08:05 AM
  5. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •