Festive Flakes
It’s got to that time of the year again: the designer is bugging me to make programmatic snow in a new and interesting way. This was rejected, but is fun:
roll-over to activate
The code for creating the flake pattern is FlakePattern.as, and can be downloaded here! I’ve also published the entire source code below the fold, if you’d prefer to browse.
The mechanism came straight out of the way I used to get KS3 (11-14) students to make snowflakes in Maths class at the end of term, translated into a more computational form. I didn’t add cut-outs to the mechanism; I would be interested to see the code if anyone extends the idea.
/**
* Copyright (c) 2009 Alec McEachran
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package
{
import flash.geom.Point;
public class FlakePattern
{
private const d60:Number = Math.PI / 3;
private const COS60:Number = Math.cos(d60);
private const SIN60:Number = Math.sin(d60);
private const d120:Number = d60 * 2;
private const COS120:Number = Math.cos(d120);
private const SIN120:Number = Math.sin(d120);
public function generate(points:uint, radius:uint):Array
{
var pattern:Array = randomPattern(points, radius);
return generateFromPattern(pattern);
}
public function randomPattern(points:uint, radius:uint):Array
{
var pattern:Array = [];
var angle:Number = Math.PI / (points << 1);
while (--points > -1)
{
var len:int = Math.random() * radius;
var ang:Number = angle * points;
pattern[points] = new Point(Math.cos(ang) * len, Math.sin(ang) * len);
}
return pattern;
}
public function generateFromPattern(points:Array):Array
{
var a:Array = convertPointsToSixtyDegrees(points);
var b:Array = reflectPointsInXAxis(a);
var c:Array = b.concat(a);
var c2:Array = rotatePointsAround60Degrees(c);
var d:Array = c2.concat(c);
var d2:Array = rotatePointsAround120Degrees(d);
var e:Array = d2.concat(d);
var e2:Array = rotatePointsAround120Degrees(e);
return e2.concat(c2).concat(b).concat(a);
}
private function convertPointsToSixtyDegrees(points:Array):Array
{
return transformPoints(points, convertPointToSixtyDegrees);
}
private function convertPointToSixtyDegrees(point:Point):Point
{
return new Point(point.x + point.y * SIN60, point.y * COS60);
}
private function reflectPointsInXAxis(points:Array):Array
{
var reflected:Array = transformPoints(points, reflectPointInXAxis);
return reflected.reverse();
}
private function reflectPointInXAxis(point:Point):Point
{
return new Point(point.x, -point.y);
}
private function rotatePointsAround60Degrees(points:Array):Array
{
return transformPoints(points, rotatePointAround60Degrees);
}
private function rotatePointAround60Degrees(point:Point):Point
{
var x:Number = point.x;
var y:Number = point.y;
return new Point(x * COS60 + y * SIN60, y * COS60 - x * SIN60);
}
private function rotatePointsAround120Degrees(points:Array):Array
{
return transformPoints(points, rotatePointAround120Degrees);
}
private function rotatePointAround120Degrees(point:Point):Point
{
var x:Number = point.x;
var y:Number = point.y;
return new Point(x * COS120 + y * SIN120, y * COS120 - x * SIN120);
}
private function transformPoints(points:Array, method:Function):Array
{
var transformed:Array = [];
var i:int = points.length;
while (i--)
{
var point:Point = points[i];
point = method(point);
transformed[i] = point;
}
return transformed;
}
}
}


