Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PolygonArea[pts_] :=
- Abs[Apply[Plus,
- Flatten[pts Map[({1, -1} Reverse[#] &), RotateLeft[pts]]]]/2];
- gensize = 100;
- npoints = 20;
- NextGeneration[gen_] :=
- Module[{fitnesses = Map[PolygonArea[#]^2 &, gen]},
- Map[Mate, Partition[RandomChoice[fitnesses -> gen, 2 gensize], 2]]
- ];
- Normalise[poly_] :=
- Module[{length =
- Total[Map[Norm[#[[1]] - #[[2]]] &,
- Transpose[{poly, RotateLeft[poly]}]]], new},
- new = 2 Pi poly/length; 2
- Map[# - Mean[new] &, new]
- ];
- Mate[{p1_, p2_}] :=
- Normalise[(p1 + p2)/2 +
- RandomReal[NormalDistribution[0, 0.12], {npoints, 2}]];
- Clear[generation];
- RandomPolygon[n_] := Normalise[RandomReal[{-1, 1}, {n, 2}]];
- generation[0] = Table[RandomPolygon[npoints], {i, gensize}];
- generation[n_] := generation[n] = NextGeneration[generation[n - 1]];
- DrawGeneration[n_] :=
- Module[{areas = Map[PolygonArea, generation[n]], poly},
- poly = generation[n][[First@First@Position[areas, Max[areas]]]];
- Graphics[{
- EdgeForm[Darker@Red], Thick, FaceForm[RGBColor[0.8, 0.95, 1.0]],
- Polygon[poly]}, PlotRange -> 3, ImageSize -> 300]
- ];
- Manipulate[DrawGeneration[n], {n, 1, 200, 1}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement