Skip to content

Instantly share code, notes, and snippets.

@andyli
Created February 10, 2011 22:47
Show Gist options
  • Select an option

  • Save andyli/821529 to your computer and use it in GitHub Desktop.

Select an option

Save andyli/821529 to your computer and use it in GitHub Desktop.
onthewings.stuffs.stuff6
/**
* 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.stuff6;
import cpp.Sys;
import hxColorToolkit.spaces.HSL;
import nme.geom.Point;
import nme.geom.Rectangle;
import of.Context;
import org.casalib.util.NumberUtil;
using of.Context.Functions;
using Math;
using Lambda;
using DateTools;
using StringTools;
using nme.geom.Point;
using org.casalib.util.ArrayUtil;
using org.casalib.util.NumberUtil;
using org.casalib.util.GeomUtil;
using org.casalib.util.ConversionUtil;
using hxColorToolkit.ColorToolkit;
/*
* Inspired by the icon of http://rootstech.familysearch.org/
*/
class Main extends of.app.BaseApp
{
var screenCap:Image;
var width:Int;
var height:Int;
var ptsGroup:Array<Array<Array<Array<Array<Point>>>>>;
function genPts(num:Int = 10, xValues:Array<Int>, yValues:Array<Int> ):Array<Array<Array<Point>>> {
var pts = [];
for (i in 0...400) {
pts.push([]);
}
for (i in 0...num) {
var pt:Point = new Point(xValues.random(), yValues.random());
pts[cast pt.x + 200].push([pt]);
}
var lines = [];
for (i in 0...400) {
lines.push([]);
}
var vOffset = 0;//height * 0.1;
for (i in 0...200) {
//left side
var col = pts[i];
col.sort(function(b0, b1) return cast b0[0].y - b1[0].y);//sort with y asc
var x = i - 200;
var y = height * 0.5 - abs(x) - i;
for (b in col) {
var offset = 0;
while (lines[i].exists(function(pt) return y + vOffset + offset + 12 > pt.y)) {
--offset;
}
var p0 = new Point(x, y + vOffset + offset);
var p1 = new Point(0, height * 0.5 + vOffset - i + offset);
b.push(p0);
b.push(p1);
b.push(new Point(0, height));
for (li in i...200) {
var pt = p1.interpolate(p0, li.map(i, 200, 0, 1));
lines[li].push(pt);
}
}
//right side
var col = pts[399 - i];
col.sort(function(b0, b1) return cast b0[0].y - b1[0].y);//sort with y asc
var x = 200 - i;
var y = height * 0.5 - abs(x) - i;
for (b in col) {
var offset = 0;
while (lines[399 - i].exists(function(pt) return y + vOffset + offset + 12 > pt.y)) {
--offset;
}
var p0 = new Point(x, y + vOffset + offset);
var p1 = new Point(0, height * 0.5 + vOffset - i + offset);
b.push(p0);
b.push(p1);
b.push(new Point(0, height));
for (li in 200...399 - i) {
var pt = p0.interpolate(p1, li.map(200, 399 - i, 0, 1));
lines[li].push(pt);
}
}
}
//trace(lines.foreach(function(line) return line.foreach(function(p) return (p.x - line[0].x).abs() < 1))); //verify lines
for (col in pts) {
col.sort(function(b0, b1) return cast b0[0].y - b1[0].y);//sort with y asc
}
var vPtsIndex = [];
for (i in 0...400) vPtsIndex.push(i);
vPtsIndex = vPtsIndex.randomize();
for (i in 0...100) {
var coli = vPtsIndex.pop();
var col = pts[coli];
if (col.length > 0) {
var pt = col[0][0];
if (coli < 200 && Math.random() > 0.5) {
var vLen = 10 * NumberUtil.randomIntegerWithinRange(0, 20);
var joinPos = pt.y + vLen;
var joinColi = coli + 1;
while (!hitCol(joinPos, pts[joinColi])) {
if (joinColi >= 200) {
joinColi = coli + 1;
joinPos += 10;
if (joinPos > height * 0.5) break;
} else {
++joinColi;
}
}
if (joinPos > height * 0.5) continue;
col[0] = [pt, new Point(pt.x, joinPos), new Point(joinColi - 200, joinPos)];
vPtsIndex.removeItem(joinColi);
} else if (coli > 200) {
var vLen = 10 * NumberUtil.randomIntegerWithinRange(0, 20);
var joinPos = pt.y + vLen;
var joinColi = coli - 1;
while (!hitCol(joinPos, pts[joinColi])) {
if (joinColi <= 200) {
joinColi = coli - 1;
joinPos += 10;
if (joinPos > height * 0.5) break;
} else {
--joinColi;
}
}
if (joinPos > height * 0.5) continue;
col[0] = [pt, new Point(pt.x, joinPos), new Point(joinColi - 200, joinPos)];
vPtsIndex.removeItem(joinColi);
}
}
}
return pts;
}
override public function setup():Void {
setFrameRate(120);
enableAlphaBlending();
enableSmoothing();
background(25, 25, 25);
screenCap = new Image();
screenCap.allocate(getWidth(), getHeight(), Constants.OF_IMAGE_COLOR);
var p = screenCap.getPixels();
for (i in 0...p.length) {
p[i] = cast 0;
}
screenCap.update();
width = getWidth();
height = getHeight();
ptsGroup = [];
for (j in 0...(width/200).ceil()) {
var _ptsGroup = [];
for (i in 0...6) {
var xValues = [];
var l = 25;//i.map(0, 5, 25, 2).floor();
for (x in -l...l) {
if (x != 0)
xValues.push(x * 8);
}
var yValues = [];
var l = 25;// i.map(0, 5, 25, 4).floor();
var os = 200;// i.map(0, 5, 200, 300).floor();
for (y in -l...l) {
yValues.push(y * 4 + os);
}
_ptsGroup.push(genPts(cast i.map(0, 5, 10, 2), xValues, yValues));
}
ptsGroup.push(_ptsGroup);
}
}
function hitCol(y:Float, col:Array<Array<Point>>):Bool {
for (b in col) {
if (b.length >= 2 && b[0].y < y && y < b[1].y) return true;
}
return false;
}
override public function draw():Void {
translate(100, height + 120);
for (j in 0...ptsGroup.length) {
var ptsGroup = ptsGroup[j];
pushMatrix();
for (i in 0...ptsGroup.length) {
var pts = ptsGroup[i];
pushMatrix();
//translate(0, -height*0.5*0.8);
scale(0.5, 0.5);
translate(0, -height * 0.75);
setColor(39, 181, 182, 255);
setLineWidth(2);
for (col in pts) {
for (b in col) {
for (i in 0...b.length-1) {
line(b[i].x, b[i].y, b[i + 1].x, b[i + 1].y);
}
}
}
for (col in pts) {
for (b in col) {
var pt = b[0];
fill();
setColor(25, 25, 25, 255);
circle(pt.x, pt.y, 5);
noFill();
setColor(39, 181, 182, 255);
circle(pt.x, pt.y, 5);
}
}
popMatrix();
translate(0, -height * 0.2/*i.map(0, ptsGroup.length, 0.18, 0.002)*/);
}
popMatrix();
translate(200, 0);
}
}
override public function keyPressed(key:Int):Void {
if (key == 's'.charCodeAt(0)){
screenCap.grabScreen(0, 0, width, height);
screenCap.saveImage(Date.now().format("%Y%m%d_%H%M%S") + ".png");
Sys.exit(0);
} else if (key == 'a'.charCodeAt(0)) {
setup();
}
}
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