Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Shapes and rotation

Hello all ^-^

[Image: 3ff09d0736cf2da449bfc31aa6449b53.gif] [Image: 4010bfc14987e59be5fb3f359808f1bb.gif]

I would mostly like to share (what you all probably already know) but I'm quite proud of it because I took the time to experiment, and learn all of this on my own. I soon come to find out that I've simulated the "mid-point circle" algorithm, but I've applied the math to create shapes of all sides (except circles, being the absolute most trivial).

A circle would be simple.
Code
x=20 * !cos(x):x=angle! + 50"
y=20 * !sin(x):x=angle! + 50"

Expanding my knowledge of this fundamental for circles, I considered dividing up the degrees of a circle, and plotting them evening.

Code
360/sides

But the experimenting didn't stop there. I wanted to not only get different shapes, but I wanted to expand or shrink their size, and I wanted to rotate them, and I wanted them colorful.

I was using Bresenham's Line algorithm as a function before, but found it's performance to be so slow it made me cringe, so I implemented as a macro. This macro has usage commented in the script.

So this is the script I've come up with! CHECK THE "CHANGE ME!!!!" on line 69

I really hope you enjoy!
Code
@echo off & setlocal enableDelayedExpansion
mode 100,100

set ^"LF=^

^" Above empty line is required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

rem capture ESC for VT100 %plot% macro. %line% ultilizes %plot%
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"

   set "_SIN=a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/1875*a/15360*a/15625*a/15625*a/16000*a/44800000"
   set "SIN(x)=(a=(x * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"
   set "COS(x)=(a=(15708 - x * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"
   set "_SIN="

rem %plot% x y COLOR(0-255) COLOR(0-255) CHAR
set plot=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
 set "screen=^!screen^!!esc![%%2;%%1H!esc![38;5;%%3m!esc![48;5;%%4m%%~5!esc![0m"%\n%
)) else set args=

rem %line% Bresenham Line Algorithm for all slopes
rem I will comment this later.. Sorry

rem USAGE:
rem %line% x1 y1 x2 y2 CHAR COLOR(0-255)
set line=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-6" %%1 in ("^!args^!") do (%\n%
if "%%~6" equ "" ( set "hue=30" ) else ( set "hue=%%~6")%\n%
set /a "xa=%%~1", "ya=%%~2", "xb=%%~3", "yb=%%~4", "dx=%%~3 - %%~1", "dy=%%~4 - %%~2"%\n%
for /f "tokens=1-2" %%6 in ("^!dx^! ^!dy^!") do (%\n%
if %%~7 lss 0 ( set /a "dy=-%%~7", "stepy=-1" ) else ( set "stepy=1" )%\n%
if %%~6 lss 0 ( set /a "dx=-%%~6", "stepx=-1" ) else ( set "stepx=1" )%\n%
set /a "dx<<=1", "dy<<=1"%\n%
)%\n%
for /f "tokens=1-9" %%a in ("^!dx^! ^!dy^! ^!xa^! ^!xb^! ^!ya^! ^!yb^! ^!stepx^! ^!stepy^! ^!hue^!") do (%\n%
if %%~a gtr %%~b (%\n%
set /a "fraction=%%~b - (%%~a >> 1)"%\n%
for /l %%x in (%%~c,%%~g,%%~d) do (%\n%
for /f "tokens=1" %%6 in ("^!fraction^!") do if %%~6 geq 0 set /a "ya+=%%~h", "fraction-=%%~a"%\n%
set /a "fraction+=%%~b"%\n%
for /f "tokens=1" %%6 in ("^!ya^!") do (%\n%
if 0 leq %%x if %%x lss 199 if 0 leq %%~6 if %%~6 lss 199 ^!plot^! %%x %%~6 %%i 0 %%~5%\n%
)%\n%
)%\n%
) else (%\n%
set /a "fraction=%%~a - (%%~b >> 1)"%\n%
for /l %%y in (%%~e,%%~h,%%~f) do (%\n%
for /f "tokens=1" %%6 in ("^!fraction^!") do if %%~6 geq 0 set /a "xa+=%%~g", "fraction-=%%~b"%\n%
set /a "fraction+=%%~a"%\n%
for /f "tokens=1" %%6 in ("^!xa^!") do (%\n%
if 0 leq %%~6 if %%~6 lss 199 if 0 leq %%y if %%y lss 199 ^!plot^! %%~6 %%y %%i 0 %%~5%\n%
)%\n%
)%\n%
)%\n%
)%\n%
)) else set args=

:: Hide the cursor
<nul set /p "=!esc![?25l"
cls







REM CHANGE ME --------
set /a "sides=5"
REM ------------------

set /a "x=50", "y=50", "s=-3"
set /a "angle=360 / sides"

for /l %%a in (0,1,16) do (

set /a "a0+=%%a", "s+=3", "hue=%%a %% 255"

for /l %%c in (1,1,%sides%) do (
set /a "b=%%c - 1"

for /f "tokens=1" %%b in ("!b!") do set /a "a%%c=!a%%b! + angle"

if %%c equ %sides% ( set /a "c=%%c + 1" & set /a "x!c!=x1", "y!c!=y1")

set /a "x%%c=s * !cos(x):x=a%%c! + x", "y%%c=s * !sin(x):x=a%%c! + y"
)

for /l %%c in (1,1,%sides%) do (
set /a "d=%%c + 1"
for /f "tokens=1" %%b in ("!d!") do %line% !x%%c! !y%%c! !x%%b! !y%%b! Û !hue!
)

<nul set /p "=!screen!" & set "screen="
)
pause >nul & exit

For a beautiful phyllotaxis, you can change the SIDES to 20, and change LINE 91 to
Code
for /f "tokens=1" %%b in ("!d!") do %plot% !x%%c! !y%%c! !hue! 0 Û
instead of
Code
for /f "tokens=1" %%b in ("!d!") do %line% !x%%c! !y%%c! !x%%b! !y%%b! Û !hue!

[Image: cfeb2c236d8000a57f901bd34b112543.png]
Aweosme job! I have never seen colours used this way in Batch before.

I think my screen is a little small for the entire thing to fit though and it seems the thing you mentioned for line 32 is actually on line 91 :D
(Apr 3, 2018 12:22 AM)Sir-Teabag Wrote:  Aweosme job! I have never seen colours used this way in Batch before.

I think my screen is a little small for the entire thing to fit though and it seems the thing you mentioned for line 32 is actually on line 91 :D

Yesss.... I don't know how I managed to make this mistake but thank you for catching it!
Well this is neat. Where you been anyway?
(Apr 3, 2018 09:43 PM)Nakamoto. Wrote:  Well this is neat. Where you been anyway?

Dostips; mostly since the community there actually cares about/has knowledge of batch.
Hmm, I may have to check it out.
(Apr 3, 2018 11:35 PM)Nakamoto. Wrote:  Hmm, I may have to check it out.

I genuinely hope that you do.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by markzuckerbergJan 15, 2020 03:25 AM 1 491 Jan 15, 2020 03:25 AM
Last Post: Marc Cavanough
[Python] by realsunjesterMar 18, 2019 09:49 PM 6 219 Mar 18, 2019 09:49 PM
Last Post: realsunjester
[Java] by blank.Feb 22, 2018 06:41 PM 2 132 Feb 22, 2018 06:41 PM
Last Post: GenericSkid