1
- struct Point3D
1
+ struct Point3D : CustomStringConvertible
2
2
{
3
3
let x : Double ,
4
4
y : Double ,
@@ -9,6 +9,19 @@ struct Point3D
9
9
return self . x*self. x + self . y*self. y + self . z*self. z
10
10
}
11
11
12
+ var description : String
13
+ {
14
+ guard let xb: Int = Int ( exactly: self . x) ,
15
+ let yb: Int = Int ( exactly: self . y) ,
16
+ let zb: Int = Int ( exactly: self . z)
17
+ else
18
+ {
19
+ return " ( \( self . x) , \( self . y) , \( self . z) ) "
20
+ }
21
+
22
+ return " ( \( xb) , \( yb) , \( zb) ) "
23
+ }
24
+
12
25
init ( _ x: Double , _ y: Double , _ z: Double )
13
26
{
14
27
self . x = x
@@ -34,6 +47,12 @@ struct Point3D
34
47
{
35
48
return Point3D ( a. x + b. x, a. y + b. y, a. z + b. z)
36
49
}
50
+
51
+ static
52
+ prefix func - ( _ a: Point3D ) -> Point3D
53
+ {
54
+ return Point3D ( - a. x, - a. y, - a. z)
55
+ }
37
56
}
38
57
39
58
struct Cell3D
@@ -88,7 +107,9 @@ enum Colors
88
107
89
108
func kernel3d( )
90
109
{
91
- let near : Point3D = Point3D ( - 0.5 , 0.5 , 0.5 )
110
+ let near : Point3D = Point3D ( 0.5 , 0.5 , 0.5 )
111
+
112
+ var cell_classes : [ Double : [ Cell3D ] ] = [ : ]
92
113
93
114
for k in - 3 ..< 3
94
115
{
@@ -110,7 +131,12 @@ func kernel3d()
110
131
r2 = min ( r2, cell. distance_squared ( from: point) )
111
132
}
112
133
113
- var output : String = pad ( String ( describing: r2) , to: 5 )
134
+ if cell_classes [ r2] ? . append ( cell) == nil
135
+ {
136
+ cell_classes [ r2] = [ cell]
137
+ }
138
+
139
+ var output : String = pad ( String ( r2) , to: 5 )
114
140
115
141
if r2 < 3
116
142
{
@@ -123,6 +149,13 @@ func kernel3d()
123
149
}
124
150
print ( )
125
151
}
152
+
153
+ for r2 : Double in cell_classes. keys. sorted ( )
154
+ {
155
+ let cells : [ Cell3D ] = cell_classes [ r2] !
156
+ let class_vector : String = cells. map { String ( describing: - $0. root) } . joined ( separator: " , " )
157
+ print ( " \( Colors . bold) \( pad ( String ( r2) , to: 5 ) ) \( Colors . off) : \( pad ( String ( cells. count) , to: 2 ) ) cells [ \( class_vector) ] " )
158
+ }
126
159
}
127
160
128
161
func pad( _ str: String , to n: Int ) -> String
0 commit comments