Jump to content

Recommended Posts

Hello.

 

I have been racking my brain, but can't think up a function to do this.

 

I want to make a function to convert and ID into a coordinate.

 

E.g.

 

1 = 1,1

2 = 2,1

3 = 1,2

4 = 2,2

5 = 3,1

6 = 3,2

7 = 1,3

8 = 2,3

9 = 3,3

10 = 4,1

11 = 4,2

12 = 4,3

13 = 1,4

14 = 2,4

15 = 3,4

16 = 4,4

 

I realise that this most probably would be based on square roots.

 

e.g 16 = 4,4 and 4 is the square root of 16. But I can't think of a way to find it for non square number ID's.

 

Thanks in advanced,

Millar.

Link to comment
https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/
Share on other sites

This is an example for 5x5 table, where horizontal is X and vectorial is Y. So for an example 10 = (4,1) and 9 = (3,3).

1 2 5 10
3 4 6 11
7 8 9 12
13 14 15 16

 

I tried to solve this problem with no success...

I found out that the X values of coordinates and the Y values of the coordinates (each coordinate is (X,Y) of course) have a rather similar pattern, but I couldn't find out how to get the X/Y of a certain number =/ I'll try a bit more.

 

Orio.

Well, I've built an algorithm that solves the problem, but I don't really like it because it's not a real mathematical solution... I was hoping to find a way to find the coordinates for a given number N in a mathematical way. But anyways, this solves the problem:

 

<?php

function get_coords($i)
{
$sqr_root = ceil(sqrt($i));
$sqr = $sqr_root * $sqr_root;
$i2 = ($i+$sqr_root <= $sqr) ? $i+$sqr_root-1 : $i;
$distance = $sqr - $i2;
if($i+$sqr_root <= $sqr)
	return "(".$sqr_root.", ".($sqr_root-$distance).")";
else
	return "(".($sqr_root-$distance).", ".$sqr_root.")";
}

for($i=1; $i<=36; $i++)
echo $i." = ".get_coords($i)."<br />";

?>

 

 

Orio.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.