Skip to content

Commit 85da3d9

Browse files
committed
improve metaprogram to produce point list and early exits
1 parent 11b48f4 commit 85da3d9

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

sources/meta/cell.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct Point3D
1+
struct Point3D:CustomStringConvertible
22
{
33
let x:Double,
44
y:Double,
@@ -9,6 +9,19 @@ struct Point3D
99
return self.x*self.x + self.y*self.y + self.z*self.z
1010
}
1111

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+
1225
init(_ x:Double, _ y:Double, _ z:Double)
1326
{
1427
self.x = x
@@ -34,6 +47,12 @@ struct Point3D
3447
{
3548
return Point3D(a.x + b.x, a.y + b.y, a.z + b.z)
3649
}
50+
51+
static
52+
prefix func - (_ a:Point3D) -> Point3D
53+
{
54+
return Point3D(-a.x, -a.y, -a.z)
55+
}
3756
}
3857

3958
struct Cell3D
@@ -88,7 +107,9 @@ enum Colors
88107

89108
func kernel3d()
90109
{
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]] = [:]
92113

93114
for k in -3 ..< 3
94115
{
@@ -110,7 +131,12 @@ func kernel3d()
110131
r2 = min(r2, cell.distance_squared(from: point))
111132
}
112133

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)
114140

115141
if r2 < 3
116142
{
@@ -123,6 +149,13 @@ func kernel3d()
123149
}
124150
print()
125151
}
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+
}
126159
}
127160

128161
func pad(_ str:String, to n:Int) -> String

0 commit comments

Comments
 (0)