void ComputeFitness(Nematode painter) { var fitness = new Dictionary <int, bool>(); var points = painter.GetPoints(resolution); const int fineness = 10; const float step = 1f / fineness; for (int i = 1, n = points.Count; i < n; i++) { var i0 = i - 1; var i1 = i; var i2 = (i + 1) % n; var i3 = (i + 2) % n; for (var t = 0f; t < 1f; t += step) { var p0 = Spline.GetPosition(t, points[i0], points[i1], points[i2], points[i3]); var p1 = Spline.GetPosition(t + step, points[i0], points[i1], points[i2], points[i3]); var dir = (p1 - p0); var norm = dir.normalized; var m = dir.magnitude; for (var tt = 0f; tt < 1f; tt += step) { var p = p0 + norm * m * tt; int x = Mathf.FloorToInt(p.x); int y = Mathf.FloorToInt(p.y); int address = y * resolution + x; /* * if(Fit(x, y) && !fitness.ContainsKey(address)) { * fitness[address] = true; * } * */ bool fit = Fit(x, y); if (!fitness.ContainsKey(address)) { fitness[address] = fit; } else { fitness[address] &= fit; } } } } // painter.Fitness = (1f * fitness.Values.ToList().FindAll((v) => v).Count) / (points.Count * fineness * fineness); var values = fitness.Values.ToList(); painter.Fitness = (1f * values.FindAll((v) => v).Count) / values.Count; }
void DrawPainterGizmos(Nematode painter) { var points = painter.GetPoints(resolution); const float step = 0.1f; for (int i = 1, n = points.Count; i < n; i++) { var i0 = i - 1; var i1 = i; var i2 = (i + 1) % n; var i3 = (i + 2) % n; for (var t = 0f; t < 1f; t += step) { var p0 = Spline.GetPosition(t, points[i0], points[i1], points[i2], points[i3]); var p1 = Spline.GetPosition(t + step, points[i0], points[i1], points[i2], points[i3]); Gizmos.DrawLine(p0, p1); /* * var dir = (p1 - p0); * var norm = dir.normalized; * var m = dir.magnitude; * for(var tt = 0f; tt < 1f; tt += step) * { * var p = p0 + norm * m * tt; * int x = Mathf.FloorToInt(p.x); * int y = Mathf.FloorToInt(p.y); * int address = y * resolution + x; * if(Fit(x, y)) { * Gizmos.DrawSphere(p, 0.1f); * } * } */ } } }