txt中的数字之间的差异

时间:2017-07-27 01:07:43

标签: php minecraft

我目前正忙于在我的世界中创造3D过山车游乐设施。我使用了一个小的NoLimits 2插件来获取一些输出数据。并预先处理它为PHP。目前只需要x y z坐标

预处理文件的一部分:

47.71394|1.5|-10.34523
47.73439|1.5|-10.34828
47.75566|1.5|-10.35144
47.77783|1.5|-10.35473
47.80088|1.5|-10.35815
47.81917|1.5|-10.36086
47.83271|1.5|-10.36286
47.84702|1.5|-10.36497
47.86219|1.5|-10.36721
47.87823|1.5|-10.36957
47.89509|1.5|-10.37205
47.91286|1.5|-10.37466
47.92018|1.5|-10.37574
47.92836|1.5|-10.37694
47.9374|1.5|-10.37826
47.94727|1.5|-10.37971

虽然它和其他地方没有什么不同,我需要将其转换为我使用的Minecraft插件的相对差异。 PHP到目前为止已经让我了。

我想区分每一步并把它放在像这样的文件中

- 'run:move 0,0,0.1,0,0'
- 'wait:1'
- 'run:move 0,0,0.1,0,0'
- 'wait:1'

或者使用前两个数据

line 2:    47.73439|1.5|-10.34828 
line 1:    47.71394|1.5|-10.34523  -
          ---------------------------
output 1:     0.02045|0|-0.00305

然后输出只需转换为[甚至只是]

- 'run:move  0.02045,0,-0.00305,0,0'

继续接下来的两行     第3行:47.75566 | 1.5 | -10.35144     第2行:47.73439 | 1.5 | -10.34828 -               ---------------------------     输出1:0.02127 | 0 | -0.00316

最后两个移动的东西不需要改变,但我如何继续获得for循环中的差异[也需要是动态的]。 有些文件只有800行,其他文件可能是1500 +

用于添加的当前php代码编码和删除不必要的无限制2位置数据

<?php
//CustomRideMC 3d FROM NL2

$lines = file('CustomRide.txt');
$n = 0;
foreach($lines as $line) {
    $coords = explode(" ", $line);
    echo $coords[0]; // piece1
    echo "|";
    echo $coords[1]; // piece2
    echo "|";
    echo $coords[2]; // piece3
    echo "<br>";
    $n = $n + 1;
}

2 个答案:

答案 0 :(得分:0)

这应该这样做。评论中的解释:

<?php

/* Input TXT file */
$finput = 'inputfile.txt';

/* Array to hold all lines */
$lines = array();

/* Process 2 lines from array */
function processLines($line1, $line2){

    /* Output TXT file */
    $foutput = 'outputfile.txt';

    /* Explode lines to array */
    $line1 = explode('|', $line1);
    $line2 = explode('|', $line2);

    /* Substract and round to 5 digits behind the comma */
    $x = round((floatval($line2[0]) - floatval($line1[0])), 5);
    $y = round((floatval($line2[1]) - floatval($line1[1])), 5);
    $z = round((floatval($line2[2]) - floatval($line1[2])), 5);

    /* Get the lines we've already processed */
    $content = file_get_contents($foutput);

    /* Create new line and add it to content */
    $content .= "run:move ". $x .",". $y .",". $z .",0,0\r\n";

    /* Put content back in file */
    file_put_contents($foutput, $content);
}

/* Read through input file */
$handle = fopen($finput, "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        array_push($lines, $line);
    }

    fclose($handle);
} else {
    echo 'Unable to open file';
    exit;
}

/* Loop through array by 2 steps*/
for($i = 0; $i < count($lines); $i+=2){
    processLines($lines[$i], $lines[($i+1)]);
}

?>

使用示例数据输出:

run:move 0.02045,0,-0.00305,0,0
run:move 0.02217,0,-0.00329,0,0
run:move 0.01829,0,-0.00271,0,0
run:move 0.01431,0,-0.00211,0,0
run:move 0.01604,0,-0.00236,0,0
run:move 0.01777,0,-0.00261,0,0
run:move 0.00818,0,-0.0012,0,0
run:move 0.00987,0,-0.00145,0,0

答案 1 :(得分:0)

我当然没有测试过这个,但是:

$A = $B = $C = 0;  // initialize the holders of the 'previous' point
$first = true;
foreach($lines as $line) 
{
    $coords = explode(" ", $line);
    if($first)
    {
        $A = $coords[0];
        $B = $coords[1];
        $C = $coords[2];
        $first=false;
        continue; // output nothing for the first point, move on
     }

     $a = $coords[0] - $A; // take difference between current point
     $b = $coords[1] - $B; // and previous one
     $c = $coords[2] - $C;

     echo "run:move  $a,$b,$c,0,0"; // Do you need a line feed here?
                                    // Might need to add "<br>" or "\n" 
                                    // to the end of the line, depending
                                    // on what reads the output
}

如果您需要确保输入为浮点数,请使用:

 $A = floatval($coords[0]); // etc for all 3

如果需要控制输出的小数精度,请在输出之前添加这样的行来渲染数字:

 $a = sprintf("%2.5f", $a); // 2.5 means two digits before the decimal
                            // point, and five digits after