1
+ package progen .dla ;
2
+ import progen .dla .bounds .IBounds ;
3
+
4
+ /**
5
+ * DLA for generating weird structures of segments and points.
6
+ * @author P Svilans
7
+ */
8
+
9
+ class DLA
10
+ {
11
+
12
+ /**
13
+ * The number of active points in the simulation.
14
+ */
15
+ public var points : Array <DLAPoint >;
16
+
17
+ /**
18
+ * Generated segments from the simulation.
19
+ */
20
+ public var segments : Array <DLASegment >;
21
+
22
+ public var origin : IBounds ;
23
+ public var target : IBounds ;
24
+
25
+ public var gravity : Float = 1.0 ;
26
+ public var threshold : Float ;
27
+ public var motion : DLAPoint ;
28
+
29
+ /**
30
+ * Creates a DLA with the given origin and target bounds. Points will be spawned along the origin bound,
31
+ * and will freeze once they come in range of the target bound. All generated segments are guaranteed to
32
+ * be interconnected with one another (no islands).
33
+ * @param origin Origin bound on which to spawn points.
34
+ * @param target Bound that freezes points on contact.
35
+ */
36
+ public function new (origin : IBounds , target : IBounds , gravity : Float = 1.0 , threshold : Float = 10.0 , motion : DLAPoint = null )
37
+ {
38
+ this .origin = origin ;
39
+ this .target = target ;
40
+
41
+ this .gravity = gravity ;
42
+ this .threshold = threshold ;
43
+ this .motion = (motion != null ) ? motion : new DLAPoint (10.0 , 10.0 );
44
+ }
45
+
46
+ /**
47
+ * Simulate a certain number of points to construct the DLA.
48
+ * @param amount The amount of points to simulate.
49
+ * @param iterations How many iterations before terminating.
50
+ * @return If all points have been frozen, returns true. If some point haven't stuck to the DLA yet, returns false.
51
+ */
52
+ public function construct (amount : Int = 500 , iterations : Int = 1000 ): Bool
53
+ {
54
+ var joined : Array <DLAPoint > = new Array <DLAPoint >();
55
+ points = [];
56
+ segments = [];
57
+
58
+ for (i in 0 ... amount ) points .push (origin .create (Math .random ()));
59
+
60
+ for (i in 0 ... iterations )
61
+ {
62
+ for (point in points )
63
+ {
64
+ var d = target .distanceToBound (point );
65
+ var o = target .getClosestVector (point );
66
+
67
+ point .x + = o .x * gravity + Math .random () * motion .x - motion .x * 0.5 ;
68
+ point .y + = o .y * gravity + Math .random () * motion .y - motion .y * 0.5 ;
69
+
70
+ var minimumDistance : Float = Math .min (d * d + 1 , threshold * threshold );
71
+ var minimumPoint : DLAPoint = null ;
72
+
73
+ for (set in joined )
74
+ {
75
+ var d = set .distance_2 (point );
76
+
77
+ if (d < minimumDistance )
78
+ {
79
+ minimumDistance = d ;
80
+ minimumPoint = set ;
81
+ }
82
+ }
83
+
84
+ if (minimumDistance < threshold * threshold && (minimumPoint != null || joined .length == 0 ))
85
+ {
86
+ point .isSet = true ;
87
+
88
+ points .remove (point );
89
+ joined .push (point );
90
+ }
91
+
92
+ if (minimumPoint != null ) segments .push (new DLASegment (point , minimumPoint ));
93
+ }
94
+
95
+ if (points .length == 0 ) return true ;
96
+ }
97
+
98
+ return false ;
99
+ }
100
+
101
+ }
0 commit comments