ASCII ARTで指定の2点間に線を引くAppleScriptです。(デモ用の)文字で表現するゲームを作る場合に、基礎ルーチンを整備しておく必要性を感じて、書いておきました。
AppleScript名:ASCII ARTで直線を引く v3.1.scpt |
— – Created by: Takaaki Naganoya – Created on: 2025/07/07 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — property widthCount : 40 property heightCount : 25 property spaceChar : " " property drawChar : "■" — 使用例 set canvas to makeBlankCanvas() of me set canvas to drawLine(canvas, 0, 0, 39, 24, 1) of me — 太さ1の線 set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to return set resultText to canvas as text set AppleScript’s text item delimiters to curDelim return resultText on makeBlankCanvas() set blankLine to "" repeat widthCount times set blankLine to blankLine & spaceChar end repeat set canvas to {} repeat heightCount times set end of canvas to blankLine end repeat return canvas end makeBlankCanvas on drawLine(canvas, x0, y0, x1, y1, thickness) script spd property drawnPositions : {} property drawXCoords : {} property canvas : {} property lineChars : {} end script copy canvas to (canvas of spd) set dx to x1 – x0 set dy to y1 – y0 set lineLength to (dx * dx + dy * dy) ^ 0.5 if lineLength = 0 then if x0 ≥ 0 and x0 < widthCount and y0 ≥ 0 and y0 < heightCount then set theLine to item (y0 + 1) of (canvas of spd) set newLine to replaceCharAt(theLine, x0 + 1, drawChar) set item (y0 + 1) of (canvas of spd) to newLine end if return (canvas of spd) end if set nx to –dy / lineLength set ny to dx / lineLength set halfThickness to (thickness – 1) / 2 if halfThickness < 0 then set halfThickness to 0 set x to x0 set y to y0 set dxAbs to absNum(dx) of me set dyAbs to absNum(dy) of me set sx to signNum(dx) of me set sy to signNum(dy) of me set err to dxAbs – dyAbs set (drawnPositions of spd) to {} repeat set (drawXCoords of spd) to {} repeat with t from –halfThickness to halfThickness set pxF to x + nx * t set pxInt to pxF as integer if pxInt ≥ 0 and pxInt < widthCount then if (drawXCoords of spd) does not contain pxInt then set end of (drawXCoords of spd) to pxInt end if end if end repeat set pyInt to y as integer if pyInt ≥ 0 and pyInt < heightCount then set theLine to item (pyInt + 1) of (canvas of spd) set (lineChars of spd) to characters of theLine repeat with pxInt in (drawXCoords of spd) set posKey to (pxInt as string) & "," & (pyInt as string) if (drawnPositions of spd) does not contain posKey then set item (pxInt + 1) of (lineChars of spd) to drawChar set end of (drawnPositions of spd) to posKey end if end repeat set newLine to (lineChars of spd) as string set item (pyInt + 1) of (canvas of spd) to newLine end if if x = x1 and y = y1 then exit repeat set e2 to 2 * err if e2 > –dyAbs then set err to err – dyAbs set x to x + sx end if if e2 < dxAbs then set err to err + dxAbs set y to y + sy end if end repeat return (canvas of spd) end drawLine on replaceCharAt(str, pos, char) if pos < 1 or pos > (length of str) then return str set prefix to text 1 thru (pos – 1) of str set suffix to text (pos + 1) thru -1 of str return prefix & char & suffix end replaceCharAt on absNum(n) if n < 0 then return –n return n end absNum on signNum(n) if n > 0 then return 1 if n < 0 then return -1 return 0 end signNum |