Skip to content

Instantly share code, notes, and snippets.

@andyli
Created January 23, 2011 16:05
Show Gist options
  • Save andyli/792168 to your computer and use it in GitHub Desktop.
Save andyli/792168 to your computer and use it in GitHub Desktop.
onthewings.stuffs.stuff5
/**
* Some of the renderings can be found at
* http://www.flickr.com/photos/andy-li/sets/72157625719497466/
*
*
* Copyright (c) 2011, Andy Li http://www.onthewings.net/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package onthewings.stuffs.stuff5;
import cpp.Sys;
import hxColorToolkit.spaces.HSL;
import nme.geom.Point;
import of.Context;
using of.Context.Functions;
using Math;
using Lambda;
using DateTools;
using StringTools;
using org.casalib.util.NumberUtil;
using org.casalib.util.GeomUtil;
using org.casalib.util.ConversionUtil;
using hxColorToolkit.ColorToolkit;
class Main extends of.app.BaseApp
{
var screenCap:Image;
var width:Int;
var height:Int;
var framenum:Int;
override public function setup():Void {
setFrameRate(30);
enableAlphaBlending();
enableSmoothing();
background(255, 255, 255);
screenCap = new Image();
screenCap.allocate(getWidth(), getHeight(), Constants.OF_IMAGE_COLOR);
var p = screenCap.getPixels();
for (i in 0...p.length) {
p[i] = cast 255;
}
screenCap.update();
width = getWidth();
height = getHeight();
framenum = 10;
}
override public function draw():Void {
setColor(255, 255, 255, 255);
screenCap.draw(0, 0);
var o = new Point();
var pts = [];
var x = width * 0.5;
var y = height * 0.5;
var R = height * 0.45;
var num = framenum;
var a;
do {
var angle = 360 / num;
var r = R * cos(Constants.PI / num);
a = 2 * R * sin(Constants.PI / num);
if (a <= 1) break;
noFill();
for (i in 0...num) {
var pt = new Point(0, -R);
pt.rotatePoint(o, angle * i);
pts.push( { pt:pt, color:new HSL(angle * i, 100, R.map(0, height * 0.5, 0, 90)).toRGB() } );
}
R = r;
++num;
} while (true);
pts.sort(function(pt0, pt1) {
var pt0 = pt0.pt;
var pt1 = pt1.pt;
var diff = pt0.angle(o) - pt1.angle(o);
if (diff != 0) return diff > 0 ? 1 : -1;
var diff = pt0.length - pt1.length;
return diff > 0 ? 1 :
diff < 0 ? -1 :
0;
});
for (i in 0...cast pts.length*0.5) {
var pt = pts[i].pt;
var pt2 = pts[pts.length - i - 1].pt;
var color = i.isEven() ? pts[i].color : pts[pts.length - i - 1].color;
setColor(cast color.red, cast color.green, cast color.blue, cast abs(pt2.angle(pt) - 90.0).map(0, 90, 0, 255));
line(width * 0.5 + pt.x, height * 0.5 + pt.y, width * 0.5 + pt2.x, height * 0.5 + pt2.y);
}
screenCap.grabScreen(0, 0, getWidth(), getHeight());
if (--framenum < 1){
screenCap.saveImage(Date.now().format("%Y%m%d_%H%M%S") + ".png");
Sys.exit(0);
}
}
override public function keyPressed(key:Int):Void {
if (key == 's'.charCodeAt(0)){
screenCap.grabScreen(0, 0, getWidth(), getHeight());
screenCap.saveImage(Date.now().format("%Y%m%d_%H%M%S") + ".png");
Sys.exit(0);
}
}
public static function main():Void {
AppRunner.setupOpenGL(new AppGlutWindow(), 1920, 1080, Constants.OF_FULLSCREEN);
//AppRunner.setupOpenGL(new AppGlutWindow(), 1024, 1024, Constants.OF_WINDOW);
AppRunner.runApp(new Main());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment