drwg89980 2016-04-20 18:43
浏览 15
已采纳

我的简单线条“绘图”代码出了什么问题?


I made a simple .php file that should 'draw' a line for me, for which the user gives the start and end points using a form. And by 'draw', I mean tell you the pixels it's coloring. And yes, I know this only works with very specific lines.
This is my entire file:

<html>
<head>
    <title>Thing</title>
    <?php
        function positiveLowLine($x,$y,$x0,$x1,$y0,$y1){
            return (($x1–$x0)*$y – ($y1–$y0)*$x – $x1*$y0 + $x0*$y1);
        }
    ?>
</head>
<body>
    <form name="Q1" method="post" action="<?php $_SERVER["PHP_SELF"] ?>">
        <table>
            <tr>
                <td align="right">x0</td>
                <td><input type="number" name="x0" value="<?php echo $_POST["x0"]; ?>"></td>
            </tr>
            <tr>
                <td align="right">x1</td>
                <td><input type="number" name="x1" value="<?php echo $_POST["x1"]; ?>"></td>
            </tr>
            <tr>
                <td align="right">y0</td>
                <td><input type="number" name="y0" value="<?php echo $_POST["y0"]; ?>"></td>
            </tr>
            <tr>
                <td align="right">y1</td>
                <td><input type="number" name="y1" value="<?php echo $_POST["y1"]; ?>"></td>
            </tr>
        </table>
    </form>
    <?php
        if (!empty($_POST)){
            $x0 = $_POST["x0"];
            $x1 = $_POST["x1"];
            $y0 = $_POST["y0"];
            $y1 = $_POST["y1"];

            $y = $y0;
            for($x = $x0;$x <= $x1; $x++){
                echo "Step $x | x=$x | y=$y"
                if (positiveLowLine($x,$y,$x0,$x1,$y0,$y1) < 0){
                    $y++;
                }
            }
        }
    ?>
</body>

I'm getting and error that says syntax error, unexpected '$x0' (T_VARIABLE) in /file.php on line 6 (which is the return line)
But I don't understand what's the problem.

EDIT I have indeed taken a look at this post ("PHP Parse/Syntax Errors; and How to solve them?") but the answer wasn't there for me. At least I couldn't find it.

  • 写回答

1条回答 默认 最新

  • dreamlife2014 2016-04-20 18:57
    关注

    You're using wrong minus character. You're using (char code 8211) and minus is - (char code 45). I don't know how you had this different character, but changing it will work.

    <?php
            function positiveLowLine($x,$y,$x0,$x1,$y0,$y1){
                return (($x1 - $x0) * $y - ($y1 - $y0) * $x - $x1 * $y0 + $x0 * $y1);
            }
    ?>
    

    But then it pointed an error right here:

    for($x = $x0;$x <= $x1; $x++){
        echo "Step $x | x=$x | y=$y"
        if (positiveLowLine($x,$y,$x0,$x1,$y0,$y1) < 0){
            $y++;
        }
    

    You're missing a semi-colon ; after the echo, above the if.

    Full working code:

    <html>
    <head>
        <title>Thing</title>
        <?php
            function positiveLowLine($x,$y,$x0,$x1,$y0,$y1){
                return (($x1 - $x0) * $y - ($y1 - $y0) * $x - $x1 * $y0 + $x0 * $y1);
            }
        ?>
    </head>
    <body>
        <form name="Q1" method="post" action="<?php $_SERVER["PHP_SELF"] ?>">
            <table>
                <tr>
                    <td align="right">x0</td>
                    <td><input type="number" name="x0" value="<?php echo $_POST["x0"]; ?>"></td>
                </tr>
                <tr>
                    <td align="right">x1</td>
                    <td><input type="number" name="x1" value="<?php echo $_POST["x1"]; ?>"></td>
                </tr>
                <tr>
                    <td align="right">y0</td>
                    <td><input type="number" name="y0" value="<?php echo $_POST["y0"]; ?>"></td>
                </tr>
                <tr>
                    <td align="right">y1</td>
                    <td><input type="number" name="y1" value="<?php echo $_POST["y1"]; ?>"></td>
                </tr>
            </table>
        </form>
        <?php
            if (!empty($_POST)){
                $x0 = $_POST["x0"];
                $x1 = $_POST["x1"];
                $y0 = $_POST["y0"];
                $y1 = $_POST["y1"];
    
                $y = $y0;
                for($x = $x0;$x <= $x1; $x++){
                    echo "Step $x | x=$x | y=$y";
                    if (positiveLowLine($x,$y,$x0,$x1,$y0,$y1) < 0){
                        $y++;
                    }
                }
            }
        ?>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了