批量在2点之间划线

时间:2018-12-14 20:29:13

标签: batch-file

我有一个二维数组,如下所示:

%v[0][0]%%v[0][1]%%v[0][2]%%v[0][3]% ........

%v[1][0]%%v[1][1]%%v[1][2]%%v[1][3]% .........

和4个变量,它们在数组中保留2个点(A和B)的坐标: xayaxbyb

即使批处理点位于不同的线上,我如何在批处理脚本的2个点之间绘制像素线(通过将数组中的值设置为字符)?

我正在为纯批处理2D射击游戏引擎开发地图编辑器。

我有一个功能,可以在给定坐标上广告点,一个用于正方形,一个用于线,仅当线的末端(给定点)具有相同的X值或相同的{{ 1}}的值。为此,我使用:

Y

现在,我需要一段代码,在不同轴上的2个点之间绘制一条for /L %%i in (%xa%,1,%xb%) do set v[%coord_ya%][%%i]=* 线。如果要在像素艺术程序中完成,它将看起来像这样:

enter image description here

其中的绿色点是给定的点(坐标为#A(1,1)),黄色的点代表我需要使用B(4,7)进行绘制。

1 个答案:

答案 0 :(得分:0)

用作基础的东西

char a = 'a';
char* str = &a;
int* ptr;
ptr = str;

输出:

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "xMax=75"
    set "yMax=23"

    call :clearBoard
    call :drawLine 1 23 75 2 *
    call :drawLine 1 1 75 23 #
    call :drawLine 1 12 75 12 -
    call :drawLine 39 1 39 23 "|"
    call :drawLine 1 16 60 2 "."
    call :drawLine 1 6 23 20 :
    call :drawLine 1 1 4 7 @
    call :dumpBoard
    goto :eof


:clearBoard 
    for /l %%x in (0 1 %xMax%) do for /l %%y in (0 1 %yMax%) do set "v[%%x][%%y]= "
    goto :eof

:drawLine xa ya xb yb char
    rem Retrive arguments
    set "xa=%1"
    set "ya=%2"
    set "xb=%3"
    set "yb=%4"
    set "char=%~5"

    rem Calulate difference between coordinates start/end and its absolute values
    set /a "dx=xb-xa", "dy=yb-ya", "adx=(dx>>31|1)*dx", "ady=(dy>>31|1)*dy"

    rem Not having decimals, from here we will handle all coordinates 
    rem multiplied by 1000 and later divide to determine real coordinate

    rem For the axis with the greater difference we will move 1 cell in each step 
    rem and increment the other coordinate proportinally
    if %adx% gtr %ady% (
        set /a "ix=1000*(dx>>31|1)", "iy=dy*1000/adx", "dMax=adx"
    ) else (
        set /a "iy=1000*(dy>>31|1)", "ix=dx*1000/ady", "dMax=ady"
    )

    rem just for debugging
    if defined debug (
        for %%a in ( xa ya xb yb dx dy adx ady ix iy dMax x y ) do echo %%a = !%%a!
        pause
    )

    rem For each step in what ever axis selected
    for /l %%d in (0 1 %dMax%) do (
        rem Determine board coordinates where to draw
        set /a "vx=(xa*1000+ix*%%d)/1000", "vy=(ya*1000+iy*%%d)/1000"
        rem "Draw" point in board
        set "v[!vx!][!vy!]=%char%"
    )
    goto :eof

:dumpBoard    
    cls
    for /l %%y in (0 1 %yMax%) do (
        set "line[%%y]="
        for /l %%x in (0 1 %xMax%) do set "line[%%y]=!line[%%y]!!v[%%x][%%y]!"
    )
    echo ...:0         1         2         3         4         5         6         7
    echo ...:0123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789   
    for /l %%y in (0 1 %yMax%) do (
        set /a "l=1000+%%y"
        echo(!l:~-3!:!line[%%y]!
    )
    goto :eof

此代码确定在哪个轴上有更大的距离。对于沿此更大轴距离的每个点,将在板上放置一个字符。较短距离轴上的第二个坐标是通过将沿该轴的距离除以我们将沿较大轴放置的点数之间来计算的。

由于批处理文件算术不能处理小数,因此所有操作都是对乘以1000的值进行的。一旦需要移至实坐标,则将这些值除以1000。