Millar Posted October 20, 2007 Share Posted October 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/ Share on other sites More sharing options...
AndyB Posted October 20, 2007 Share Posted October 20, 2007 Please try that again. The example listing you give is likely to confuse people. 7=1,3 and 10=4,1 ???? Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/#findComment-373995 Share on other sites More sharing options...
Orio Posted October 20, 2007 Share Posted October 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/#findComment-374018 Share on other sites More sharing options...
Millar Posted October 20, 2007 Author Share Posted October 20, 2007 Yeah. Surely it must be possible. I just can't think of it either. Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/#findComment-374031 Share on other sites More sharing options...
Orio Posted October 20, 2007 Share Posted October 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/#findComment-374050 Share on other sites More sharing options...
Millar Posted October 20, 2007 Author Share Posted October 20, 2007 Thankyou very much! Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/#findComment-374054 Share on other sites More sharing options...
Barand Posted October 20, 2007 Share Posted October 20, 2007 if you number like this 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 then X coord = N%4 Y = (int)N/4 Quote Link to comment https://forums.phpfreaks.com/topic/74069-solved-coordinates-from-id/#findComment-374131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.