Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee34f39
commit a14c7af
Showing
10 changed files
with
535 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.fleen.bread.app.hairball; | ||
|
||
/* | ||
* a cell in our chain-loop of cells | ||
*/ | ||
public class Cell{ | ||
|
||
//location of this cell | ||
double x,y; | ||
//neighboring cells in the chain | ||
//n0 is probably counterclockwise, n1 is probably clockwise, nut not necessarily | ||
Cell n0,n1; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.fleen.bread.app.hairball; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import org.fleen.bread.app.hairball.production.HairballObserver; | ||
import org.fleen.geom_2D.GD; | ||
|
||
public class Hairball{ | ||
|
||
public static final int WIDTH=100,HEIGHT=100; | ||
|
||
|
||
public Hairball(){ | ||
initChain(); | ||
|
||
} | ||
|
||
/* | ||
* ################################ | ||
* CELL CHAIN | ||
* ################################ | ||
*/ | ||
|
||
/* | ||
* a cell in the chain. Any cell in the chain. | ||
* If, in the process of development, this cell is removed, then we use another one. | ||
*/ | ||
Cell reference; | ||
|
||
//chain init is a circle of cells | ||
static final int INITCHAINCELLCOUNT=100; | ||
static final double INITCHAINRADIUS=20; | ||
|
||
private void initChain(){ | ||
for(int i=0;i<INITCHAINCELLCOUNT;i++){ | ||
|
||
} | ||
|
||
} | ||
|
||
/* | ||
* ################################ | ||
* STATE | ||
* ################################ | ||
*/ | ||
|
||
public int age=0; | ||
|
||
public void advanceState(){ | ||
age++; | ||
runAutomaton(); | ||
notifyObservers();} | ||
|
||
/* | ||
* ################################ | ||
* CELL AUTOMATON | ||
* ################################ | ||
*/ | ||
|
||
void runAutomaton(){} | ||
|
||
/* | ||
* ################################ | ||
* OBSERVERS | ||
* ################################ | ||
*/ | ||
|
||
public List<HairballObserver> observers=new ArrayList<HairballObserver>(); | ||
|
||
void notifyObservers(){ | ||
for(HairballObserver a:observers) | ||
a.advanced();} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package org.fleen.bread.app.hairball; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import org.fleen.bread.app.hairball.production.HairballObserver; | ||
import org.fleen.geom_2D.GD; | ||
|
||
public class HairballOLD{ | ||
|
||
public static final int WIDTH=100,HEIGHT=100; | ||
|
||
|
||
public HairballOLD(){ | ||
randomize(); | ||
|
||
} | ||
|
||
/* | ||
* ################################ | ||
* CELLFIELD | ||
* ################################ | ||
*/ | ||
|
||
public int cellfield[][]=new int[WIDTH][HEIGHT]; | ||
|
||
/* | ||
* cells are 0 or 1 | ||
* offmap returns 0 | ||
*/ | ||
public int getCell(int x,int y){ | ||
if(x<0||x>=WIDTH||y<0||y>=HEIGHT)return 0; | ||
return cellfield[x][y];} | ||
|
||
/* | ||
* cells are 0 or 1 | ||
* offmap returns 0 | ||
*/ | ||
public int[] getNeighbors(int x,int y){ | ||
int[] a=new int[8]; | ||
a[0]=getCell(x-1,y-1); | ||
a[1]=getCell(x,y-1); | ||
a[2]=getCell(x+1,y-1); | ||
a[3]=getCell(x+1,y); | ||
a[4]=getCell(x+1,y+1); | ||
a[5]=getCell(x,y+1); | ||
a[6]=getCell(x-1,y+1); | ||
a[7]=getCell(x-1,y); | ||
return a;} | ||
|
||
int getLive(int x,int y){ | ||
int[] n=getNeighbors(x,y); | ||
int l=0; | ||
for(int i:n) | ||
if(i==LIVE)l++; | ||
return l;} | ||
|
||
/* | ||
* ################################ | ||
* CIRCLE | ||
* ################################ | ||
*/ | ||
|
||
public double radius=WIDTH/3; | ||
|
||
public int getCenterDist(int x,int y){ | ||
double cx=((double)WIDTH)/2,cy=((double)HEIGHT)/2; | ||
return (int)GD.getDistance_PointPoint(x,y,cx,cy);} | ||
|
||
/* | ||
* ################################ | ||
* STATE | ||
* ################################ | ||
*/ | ||
|
||
public int age=0; | ||
|
||
public void advanceState(){ | ||
age++; | ||
if(age==0)randomize(); | ||
runAutomaton(); | ||
notifyObservers(); | ||
} | ||
|
||
Random random=new Random(); | ||
|
||
void randomize(){ | ||
for(int x=0;x<WIDTH;x++){ | ||
for(int y=0;y<HEIGHT;y++){ | ||
cellfield[x][y]=random.nextInt(2);}}} | ||
|
||
/* | ||
* ################################ | ||
* CELL AUTOMATON | ||
* ################################ | ||
*/ | ||
|
||
void runAutomaton(){ | ||
int[][] nextstate=new int[WIDTH][HEIGHT]; | ||
for(int x=0;x<WIDTH;x++){ | ||
for(int y=0;y<HEIGHT;y++){ | ||
nextstate[x][y]=doRule(x,y);}} | ||
cellfield=nextstate;} | ||
|
||
static final int LIVE=1,DEAD=0; | ||
|
||
int doRule(int x,int y){ | ||
int d=getCenterDist(x,y); | ||
if(d<radius) | ||
return doRule0(x,y); | ||
else | ||
return doRule1(x,y);} | ||
|
||
int doRule0(int x,int y){ | ||
int | ||
cell=cellfield[x][y], | ||
live=getLive(x,y); | ||
if(cell==DEAD){ | ||
if(live>4) | ||
return LIVE; | ||
else | ||
return DEAD; | ||
}else{//cell==LIVE | ||
if(live>0&&live<8) | ||
return LIVE; | ||
else | ||
return DEAD;}} | ||
|
||
int doRule1(int x,int y){ | ||
int | ||
cell=cellfield[x][y], | ||
live=getLive(x,y); | ||
if(cell==DEAD){ | ||
if(live>2&&live<7) | ||
return LIVE; | ||
else | ||
return DEAD; | ||
}else{//cell==LIVE | ||
if(live>2&&live<8) | ||
return LIVE; | ||
else | ||
return DEAD;}} | ||
|
||
|
||
|
||
/* | ||
* ################################ | ||
* OBSERVERS | ||
* ################################ | ||
*/ | ||
|
||
public List<HairballObserver> observers=new ArrayList<HairballObserver>(); | ||
|
||
void notifyObservers(){ | ||
for(HairballObserver a:observers) | ||
a.advanced();} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Hairball | ||
|
||
a loop of cells. | ||
|
||
add random kinks. | ||
|
||
gets progressively tangled. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.fleen.bread.app.hairball.production; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.util.Iterator; | ||
|
||
import javax.imageio.IIOImage; | ||
import javax.imageio.ImageIO; | ||
import javax.imageio.ImageTypeSpecifier; | ||
import javax.imageio.ImageWriter; | ||
import javax.imageio.stream.ImageOutputStream; | ||
|
||
import com.sun.imageio.plugins.png.PNGMetadata; | ||
|
||
public class Exporter{ | ||
|
||
public Exporter(Test test){ | ||
this.test=test;} | ||
|
||
Test test; | ||
|
||
//To get our pixelsPerUnitXAxis value for the PNG image metadata we multiply this by our | ||
//specified DPI value. | ||
private static final double INCHES_IN_A_METER=39.3700787; | ||
private static final int DPI=300; | ||
|
||
public void export(){ | ||
File file=getExportFile(test.exportdir,test.hairball.age); | ||
write(test.renderer.image,file);} | ||
|
||
private File getExportFile(File exportdir,int index){ | ||
String s = String.format("%1$05d",index); | ||
File test=new File(exportdir.getPath()+"/"+s+".png"); | ||
return test;} | ||
|
||
private void write(BufferedImage image,File file){ | ||
Iterator<ImageWriter> i=ImageIO.getImageWritersBySuffix("png"); | ||
ImageWriter writer=(ImageWriter)i.next(); | ||
ImageOutputStream imageOutputstream=null; | ||
try{ | ||
imageOutputstream=ImageIO.createImageOutputStream(file); | ||
}catch(Exception e){ | ||
e.printStackTrace();} | ||
writer.setOutput(imageOutputstream); | ||
PNGMetadata metaData= | ||
(PNGMetadata)writer.getDefaultImageMetadata(new ImageTypeSpecifier(image),null); | ||
metaData.pHYs_pixelsPerUnitXAxis=(int)(DPI*INCHES_IN_A_METER); | ||
metaData.pHYs_pixelsPerUnitYAxis=(int)(DPI*INCHES_IN_A_METER); | ||
metaData.pHYs_present=true; | ||
metaData.pHYs_unitSpecifier=PNGMetadata.PHYS_UNIT_METER; | ||
try{ | ||
writer.write(null,new IIOImage(image,null,metaData),null); | ||
imageOutputstream.flush(); | ||
imageOutputstream.close(); | ||
}catch(Exception e){ | ||
e.printStackTrace();}} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.fleen.bread.app.hairball.production; | ||
|
||
public interface HairballObserver{ | ||
|
||
void advanced(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.fleen.bread.app.hairball.production; | ||
|
||
import java.awt.BasicStroke; | ||
import java.awt.Color; | ||
import java.awt.Graphics2D; | ||
import java.awt.RenderingHints; | ||
import java.awt.geom.Ellipse2D; | ||
import java.awt.image.BufferedImage; | ||
import java.util.HashMap; | ||
|
||
import org.fleen.bread.app.dancingSquishyThing.DST; | ||
|
||
public class Renderer{ | ||
|
||
public static final HashMap<RenderingHints.Key,Object> RENDERING_HINTS= | ||
new HashMap<RenderingHints.Key,Object>(); | ||
|
||
static{ | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_DITHERING,RenderingHints.VALUE_DITHER_DISABLE); | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC); | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_COLOR_RENDER_QUALITY); | ||
RENDERING_HINTS.put( | ||
RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_NORMALIZE);} | ||
|
||
Renderer(Test test){ | ||
this.test=test;} | ||
|
||
Test test; | ||
|
||
BufferedImage image; | ||
|
||
static final int[] COLORS={Color.red.getRGB(),Color.black.getRGB(),Color.yellow.getRGB()}; | ||
|
||
void render(){ | ||
int | ||
w=DST.WIDTH, | ||
h=DST.HEIGHT; | ||
image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); | ||
for(int x=0;x<w;x++){ | ||
for(int y=0;y<h;y++){ | ||
if(test.hairball.cellfield[x][y]==0){ | ||
if(test.hairball.getCenterDist(x,y)<test.hairball.radius) | ||
image.setRGB(x,y,COLORS[0]); | ||
else | ||
image.setRGB(x,y,COLORS[2]); | ||
}else{ | ||
image.setRGB(x,y,COLORS[1]); }}}} | ||
|
||
} |
Oops, something went wrong.