-
Notifications
You must be signed in to change notification settings - Fork 1
/
gensites
executable file
·60 lines (50 loc) · 1.1 KB
/
gensites
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
DEFAULT_HEIGHT=600
DEFAULT_WIDTH=600
DEFAULT_NB_POINTS=40
read -r -d '' USAGE <<END
Generator of random sites
Usage: gensites # width=600 height=600 nbPoints=40
or
gensites [width] [height] # nbPoints=40
or
gensites [width] [height] [nbPoints]
END
if [ "$#" -eq 0 ]; then
width=$DEFAULT_WIDTH
height=$DEFAULT_HEIGHT
nbPoints=$DEFAULT_NB_POINTS
elif [ "$#" -eq 1 ]; then
width=$DEFAULT_WIDTH
height=$DEFAULT_HEIGHT
nbPoints=$1
elif [ "$#" -eq 2 ]; then
width=$1
height=$2
nbPoints=$DEFAULT_NB_POINTS
elif [ "$#" -eq 3 ]; then
width=$1
height=$2
nbPoints=$3
else
echo "$USAGE"
exit 1
fi
points=() # generated points
echo -n "["
echo -n "[0, 0], [$width, $height], "
for (( i=1; i<=$nbPoints; i++ ))
do
while : ; do
x=$[($RANDOM % ($width + 1))]
y=$[($RANDOM % ($height + 1))]
p="[$x, $y]"
[[ " ${points[@]} " =~ " ${p} " ]] || break
done
points+=("$p")
echo -n "$p"
if [ "$i" -lt "$nbPoints" ]; then
echo -n ", "
fi
done
echo "]"