CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2021
    Posts
    2

    Set to coordinates to centre of grid

    I have these buttons created in unity- 27 by 27 which has 9 by 9 subgrids Each button is 10 by 15 pixels. I want to get the coordinates of the centre of each subgrid(which has no grouping) which are numbered 1 to 9. There must be a neater way than coding

    Code:
    private int _num
    private int devy
    private int devx
    private float _Xcood
    private float  _Ycood
    
    if (_num==1 OR _num==2 OR _num==3)
        { devy =1}
    else if  (_num==4 OR _num==5 OR _num==6)
        {devy=0}
    else if   (_num==7 OR _num==8 OR _num==9)
        {devy=-1}
    
    if (_num==3 OR _num==6 OR _num==9)
        { devx =1}
    else if  (_num==2 OR _num==5 OR _num==8)
        {devx=0}
    else if   (_num==1 OR _num==4 OR _num==7)
        {devx=-1}
    
    _Xcood=devx*10+position.y
    _Ycood=dexv*15+position.x
    Last edited by 2kaud; January 1st, 2022 at 05:30 AM. Reason: Added code tags

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: Set to coordinates to centre of grid

    Quote Originally Posted by cloa513 View Post
    There must be a neater way
    I do not know what you mean by neater, but this is another way of assigning values to devy and devx,

    Code:
    if (_num <= 3) {
       devy = 1;
       devx = _num - 2;
    } else if (_num <= 6) {
       devy = 0;
       devx = _num - 5;
    } else  {
       devy = -1;
       devx = _num - 8;
    }

  3. #3
    Join Date
    Dec 2021
    Posts
    2

    Re: Set to coordinates to centre of grid

    Quote Originally Posted by wolle View Post
    I do not know what you mean by neater, but this is another way of assigning values to devy and devx,

    Code:
    if (_num <= 3) {
       devy = 1;
       devx = _num - 2;
    } else if (_num <= 6) {
       devy = 0;
       devx = _num - 5;
    } else  {
       devy = -1;
       devx = _num - 8;
    }
    More efficient- look how lines are needed and I doubt your improved code (while better than mine) fundamentally ensures the right result.
    I run mine with correct syntax and the location of the new object is all over the place - it should never overlap but they do.

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Set to coordinates to centre of grid

    Quote Originally Posted by cloa513 View Post
    I doubt your improved code (while better than mine) fundamentally ensures the right result.
    My code is logically the same as yours. If your code is wrong, so is mine. Your problem description indicates you have a working solution and now want help making your code become "neater".
    Last edited by wolle; January 7th, 2022 at 03:46 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
  •  





Click Here to Expand Forum to Full Width

Featured