1#!/usr/bin/tclsh
2#
3# This script generates a cluster of random polygons that are useful
4# for testing the geopoly extension.
5#
6# Usage:
7#
8#    tclsh randomshape.tcl | tee x.sql | sqlite3 >x.html
9#
10# The output files are x.sql and x.html.  Run the above multiple times
11# until an interesting "x.html" file is found, then use the "x.sql" inputs
12# to construct test cases.
13#
14proc randomenclosure {cx cy p1 p2 p3 p4} {
15  set r 0
16  set pi 3.145926
17  set pi2 [expr {$pi*2}]
18  set x0 [expr {$cx + rand()*$p3 + $p4}]
19  set ans "\[\[$x0,$cy\]"
20  while {1} {
21    set r [expr {$r+$p1+$p2*rand()}]
22    if {$r>=$pi2} break
23    set m [expr {rand()*$p3 + $p4}]
24    set x [expr {$cx+$m*cos($r)}]
25    set y [expr {$cy+$m*sin($r)}]
26    append ans ",\[$x,$y\]"
27  }
28  append ans ",\[$x0,$cy\]\]"
29  return $ans
30}
31proc randomshape1 {} {
32  set cx [expr {100+int(rand()*800)}]
33  set cy [expr {100+int(rand()*600)}]
34  set p1 [expr {rand()*0.1}]
35  set p2 [expr {rand()*0.5+0.5}]
36  set p3 [expr {rand()*100+25}]
37  set p4 [expr {rand()*25}]
38  return [randomenclosure $cx $cy $p1 $p2 $p3 $p4]
39}
40proc randomshape1_sm {} {
41  set cx [expr {100+int(rand()*800)}]
42  set cy [expr {100+int(rand()*600)}]
43  set p1 [expr {rand()*0.1}]
44  set p2 [expr {rand()*0.5+0.5}]
45  set p3 [expr {rand()*10+25}]
46  set p4 [expr {rand()*5}]
47  return [randomenclosure $cx $cy $p1 $p2 $p3 $p4]
48}
49proc randomshape2 {} {
50  set cx [expr {400+int(rand()*200)}]
51  set cy [expr {300+int(rand()*200)}]
52  set p1 [expr {rand()*0.05}]
53  set p2 [expr {rand()*0.5+0.5}]
54  set p3 [expr {rand()*50+200}]
55  set p4 [expr {rand()*50+100}]
56  return [randomenclosure $cx $cy $p1 $p2 $p3 $p4]
57}
58proc randomcolor {} {
59  set n [expr {int(rand()*5)}]
60  return [lindex {red orange green blue purple} $n]
61}
62
63puts {.print '<html>'}
64puts {.print '<svg width="1000" height="800" style="border:1px solid black">'}
65puts {CREATE TABLE t1(poly,clr);}
66puts {CREATE TABLE t2(poly,clr);}
67for {set i 0} {$i<30} {incr i} {
68  puts "INSERT INTO t1(rowid,poly,clr)"
69  puts " VALUES($i,'[randomshape1]','[randomcolor]');"
70}
71for {set i 30} {$i<80} {incr i} {
72  puts "INSERT INTO t1(rowid,poly,clr)"
73  puts " VALUES($i,'[randomshape1_sm]','[randomcolor]');"
74}
75for {set i 100} {$i<105} {incr i} {
76  puts "INSERT INTO t2(rowid,poly,clr)"
77  puts " VALUES($i,'[randomshape2]','[randomcolor]');"
78}
79
80puts {DELETE FROM t1 WHERE geopoly_json(poly) IS NULL;}
81puts {SELECT geopoly_svg(poly,
82   printf('style="fill:none;stroke:%s;stroke-width:1;"',clr))
83  FROM t1;}
84puts {SELECT geopoly_svg(poly,
85   printf('style="fill:none;stroke:%s;stroke-width:2;"',clr))
86  FROM t2;}
87puts {.print '<svg>'}
88