Jump to content

some equation help?


Crusader

Recommended Posts

I've made this grid below:

[0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9]
[1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9]
[2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9]
[3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9]
[4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,7] [4,8] [4,9]
[5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9]
[6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9]
[7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9]
[8,0] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9]
[9,0] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9]

Course plotted to [3,2] from [6,4]. Distances: X(-2) Y(-3) .

Course plotted to [9,7] from [6,4]. Distances: X(3) Y(3) .

[code]<?php
function plotCourse($x,$y,$tX,$t_y){
$dX=($tX-$x);
$dY=($t_y-$y);
print 'Course plotted to ['.$tX.','.$t_y.'] from ['.$x.','.$y.']. Distances: X('.$dY.') Y('.$dX.') .';
}
?>[/code]


If, say, I started off in [6,4] and I'd like to go to [3,2] how would I calculate what other sectors I'd have to go through? I'd also like to know the same thing from [6,4] to [9,7]. Going diagnolly isn't required but if you know how to do that, then that'd be cool too.

I've tried a few times to no avail so I'm asking for anyone's help here.

Thanks for any help!

Note: Edited by KenRbnsn to put code between [nobbc][code][/code][/nobbc] tags
Link to comment
https://forums.phpfreaks.com/topic/30022-some-equation-help/
Share on other sites

now this is easy

Say For example, you are at 6,4 and want to go to 9,7
First you need to do some maths

[code]
<?php
//First, tell the code the co-ordinates;
$start_at = "6,4";
$end_at = "9,7";

//Now we want the X and Y location seperate;
$start_exp = explode(",",$start_at);
$start_x = $start_exp[0]; //Should be 6
$start_y = $start_exp[1]; //Should be 4

$end_exp = explode(",",$end_at);
$end_x = $end_exp[0]; //SHould be 9
$end_y = $end_exp[1]; //Should be 7


//We are going to do basic Maths Way
//Along the corridor and up the stairs
//First X, then Y

//Lets set up some vars
//Makes for easier debugging

$x_route = array();
$y_route = array();

//Now, if Start is BEFORE End
//We want to add one to X till we reach End of X
if($start_x < $end_x){
for($start = $start_x; $start <= $end_x; $start++){
$x_route[] = $start.",".$start_y;
}
}else{
//If start X is Greater than End X
//We want to Take one of X
if($start_x > $end_x){
for($start = $start_x; $start >= $end_x; $start--){
$x_route[] = $start.",".$start_y;
}
}else{
//The only other option is if start_x and end_x are the same
//Therefor, you dont need to move along X
$x_route[] = $start_x.",".$start_y;
}
?>
[/code]

If you do the Same for Y route, following the idea
Then you should have the 2 arrays, with the locations between Start and End

You can create them as one array, by calling the array something like moves

Depends what you want it for
Link to comment
https://forums.phpfreaks.com/topic/30022-some-equation-help/#findComment-138109
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.