snibgo's ImageMagick pages

Random lines

These are sometimes useful.

The script

The script mRandLine.bat writes a list of points to an environment variable, that IM can use to draw a line.

The points form a random walk, starting at given coordinates. Each new position is the old one plus a random value in the x-direction and another random value in the y-direction. The values are uniformly distributed in the range offset <= V <offset + fact. Two adjacent points will not have the same coordinates.

A fresh set of points is generated on each call, unless mrlSEED is used. Calling it twice with the same seed will generate the same sequence of points.

The initial coordinate can be anywhere, but no other generated points will be at x=0 or y=0.

It generates points until:

  1. a point has x-ordinate >= given value W, or
  2. a point has y-ordinate >= given value H, or
  3. a given number of points have been generated.

For example:

call %PICTBAT%mRandLine ^
  0x200 . -5x-5 11x11 1000 PNTS

%IM%convert ^
  -size 600x400 xc:Black ^
  -stroke White -draw "polyline %PNTS%" ^
  rln_out1.png
rln_out1.png

By shifting the x-offset, we get a small bias in favour of positive x, so the line generally moves to the right.

call %PICTBAT%mRandLine ^
  0x200 . -4x-5 11x11 1000 PNTS

%IM%convert ^
  -size 600x400 xc:Black ^
  -stroke White -draw "polyline %PNTS%" ^
  rln_out2.png
rln_out2.png

With a seed:

set mrlSEED=1234

call %PICTBAT%mRandLine ^
  0x200 . -4x-5 11x11 1000 PNTS

%IM%convert ^
  -size 600x400 xc:Black ^
  -stroke White -draw "polyline %PNTS%" ^
  rln_out3.png
rln_out3.png

Using the same seed and parameters, we get the same set of points.

call %PICTBAT%mRandLine ^
  0x200 . -4x-5 11x11 1000 PNTS

%IM%convert ^
  -size 600x400 xc:Black ^
  -stroke White -draw "polyline %PNTS%" ^
  rln_out4.png

set mrlSEED=
rln_out4.png

By making the x-offset = 1 and the x-factor = 0, the line has exactly one y-value per x-value. The randomness is only in the y-direction.

set mrlSEED=1234

call %PICTBAT%mRandLine ^
  0x200 . 1x-5 0x11 1000 PNTS

%IM%convert ^
  -size 600x400 xc:Black ^
  -stroke White -draw "polyline %PNTS%" ^
  rln_clut1.png

set mrlSEED=
rln_clut1.png

Scripts

For convenience, .bat scripts are also available in a single zip file. See Zipped BAT files.

mRandLine.bat

rem Make Random Line.
rem Each point is previous + offset + R, where R is random, 0 <= R < fact.
rem Generated points will not reach x=0 or y=0.
rem Does not generate repeated points.
rem
rem %1 XxY initial coordinates. [Default 0,0]
rem %2 WidthxHeight. Stops when either is reached. [600x400]
rem %3 OffsetXxOffsetY [-1x-1]
rem %4 FactXxFactY [3x3]
rem %5 stop after generating N points. [10]
rem %6 output environment variable. [sRANDLINE]
@rem
@rem Also uses:
@rem   mrlSEED if non-blank, use this as seed.
@rem

@if "%1"=="" findstr /B "rem @rem" %~f0 & exit /B 1

@setlocal enabledelayedexpansion

@call echoOffSave

rem call %PICTBAT%setInOut %1 mrl


call parseXxY2 0 0 Init %1
call parseXxY2 600 400 WWHH %2
call parseXxY2 -1 -1 Offset %3
call parseXxY2 3 3 Fact %4

set maxPnts=%5
if "%maxPnts%"=="." set maxPnts=
if "%maxPnts%"=="" set maxPnts=10

set ENVVAR=%6
if "%ENVVAR%"=="." set ENVVAR=
if "%ENVVAR%"=="" set ENVVAR=sRANDLINE

if "%mrlSEED%"=="" (
  set sSEED=
) else (
  set sSEED=-seed %mrlSEED%
)

set sPOINTS=%Init_X%,%Init_Y%

set X=%Init_X%
set Y=%Init_Y%
set nPnts=0
set END_IT=0

:: Setting a %sSEED% gives same result in all calls to rand().


:loop

if "%mrlSEED%"=="" (

  for /F "usebackq tokens=*" %%L in (`%IM%identify ^
    -format "newX=%%[fx:int(%Offset_X%+%X%+rand()*%Fact_X%)]\nnewY=%%[fx:int(%Offset_Y%+%Y%+rand()*%Fact_Y%)]" ^
    xc:`) do set %%L

) else (

  for /F "usebackq tokens=*" %%L in (`%IM%identify ^
    %sSEED% ^
    -precision 19 ^
    -format "newX=%%[fx:int(%Offset_X%+%X%+rand()*%Fact_X%)]\nsSEED=-seed %%[fx:rand()*1e+9]" ^
    xc:`) do set %%L

  for /F "usebackq tokens=*" %%L in (`%IM%identify ^
    !sSEED! ^
    -precision 19 ^
    -format "newY=%%[fx:int(%Offset_Y%+%Y%+rand()*%Fact_Y%)]\nsSEED=-seed %%[fx:rand()*1e+9]" ^
    xc:`) do set %%L
)

set OUT_IT=1
if %newX%==%X% if %newY%==%Y% set OUT_IT=0
if %newX% LEQ 0 set OUT_IT=0
if %newY% LEQ 0 set OUT_IT=0
if %newX% GEQ %WWHH_X% set END_IT=1
if %newY% GEQ %WWHH_Y% set END_IT=1
if %OUT_IT%==1 (
  set X=%newX%
  set Y=%newY%
  set sPOINTS=%sPOINTS% !X!,!Y!
  rem echo !nPnts! !X!,!Y!
  set /A nPnts+=1
)

if %END_IT%==0 if %nPnts% LSS %maxPnts% goto loop


call echoRestore

@endlocal& set %ENVVAR%=%sPOINTS%

All images on this page were created by the commands shown, using:

%IM%identify -version
Version: ImageMagick 6.9.2-5 Q16 x64 2015-10-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180031101
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG to JPG.

Source file for this web page is randline.h1. To re-create this web page, run "procH1 randline".


This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v1.0 21-July-2016.

Page created 21-Jul-2016 13:31:09.

Copyright © 2016 Alan Gibson.