-
Notifications
You must be signed in to change notification settings - Fork 0
/
dla.tcl
executable file
·207 lines (142 loc) · 4.47 KB
/
dla.tcl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/wish
oo::class create Program {
constructor {} {
variable boardSize 330.
variable boardCenter [expr {$boardSize/2}]
# Main parameters
variable shots 5000
variable pointSize 1.0
variable distanceToConnect 10
variable vectorScale 10
# Colors
variable pointColor red
variable lineColor cyan
variable halfPoint [expr {$pointSize/2}]
variable minPos $halfPoint
variable maxPos [expr {$boardSize-$halfPoint}]
variable points {}
my createCanvas
}
method createCanvas {} {
variable boardCenter
variable halfPoint
variable pointSize
variable boardSize
variable points
variable pointColor
set topLeft [expr {$boardCenter-$halfPoint}]
set botRight [expr {$topLeft+$pointSize}]
canvas .can
.can configure -width 854
.can configure -height 480
pack .can
wm title . "DLA"
# Background
.can create rect 0 0 $boardSize $boardSize -outline black -fill black
# Initial middle dot
.can create rect\
$topLeft $topLeft $botRight $botRight\
-outline $pointColor -fill $pointColor
lappend points "$boardCenter $boardCenter"
}
method rnd {min max} { expr {int(rand()*($max+1-$min))+$min} }
method inBounds {p} {
variable minPos
variable maxPos
set x [lindex $p 0] ; set y [lindex $p 1]
return [expr {$x>=$minPos&&$x<=$maxPos&&
$y>=$minPos&&$y<=$maxPos}]
}
method checkCollision {p} {
variable points
variable distanceToConnect
set cpX [lindex $p 0]
set cpY [lindex $p 1]
for {set j 0} {$j < [llength $points]} {incr j} {
set currentFrozenPoint [lindex $points $j]
set fX [lindex $currentFrozenPoint 0]
set fY [lindex $currentFrozenPoint 1]
set distance [expr {sqrt(pow($cpX-$fX,2)+pow($cpY-$fY,2))}]
if {$distance<=$distanceToConnect} { return "1 {$fX $fY}"}
}
return "0 {}"
}
method main {} {
variable shots
variable minPos
variable maxPos
variable points
variable halfPoint
variable vectorScale
variable pointColor
variable lineColor
for {set i 0} {$i < $shots} {incr i} {
puts "Shots: $i, Frozen points: [llength $points]"
# Random wall
set wall [my rnd 1 4]
# Position in wall
set position [my rnd $minPos $maxPos]
# Angle range
set ang [my rnd 0 180]
# Left(-90 to 90)
# Top(-180 to 0)
# Bot(0 to 180)
# Right(90 to 270)
switch $wall {
# Left
1 {
set ang [expr {$ang-90}]
set midCoords "$minPos $position"
}
# Top
2 {
set ang [expr {$ang-180}]
set midCoords "$position $minPos"
}
# Right
3 {
set ang [expr {$ang+90}]
set midCoords "$maxPos $position"
}
# Bot(no change required)
4 {
set midCoords "$position $maxPos"
}
default {
puts "Error"
}
}
# Middle side position
set currentPosition $midCoords
set radians [expr {$ang*0.0174532925}]
# Vector components
set x [expr {cos($radians)*$vectorScale}]
set y [expr {sin($radians)*$vectorScale}]
# Move while position in bounds
while {[my inBounds $currentPosition]} {
# Move point
lset currentPosition 0 [expr {[lindex $currentPosition 0]+$x}]
lset currentPosition 1 [expr {[lindex $currentPosition 1]+$y}]
set result [my checkCollision $currentPosition]
if {[lindex $result 0]} {
set cpX [lindex $currentPosition 0]
set cpY [lindex $currentPosition 1]
set fX [lindex $result 1 0]
set fY [lindex $result 1 1]
lappend points $currentPosition
# Create rectangle
.can create rect\
[expr {$cpX-$halfPoint}] [expr {$cpY-$halfPoint}]\
[expr {$cpX+$halfPoint}] [expr {$cpY+$halfPoint}]\
-outline $pointColor -fill $pointColor
# Connect lines
.can create line $fX $fY $cpX $cpY -fill $lineColor -tag lines
break
}
}
}
}
}
# -------------------------
[Program new] main
# -------------------------