Skip to content

Instantly share code, notes, and snippets.

@jmschrack
Last active August 4, 2019 04:49
Show Gist options
  • Save jmschrack/9add85a385c3b645c82a48321c9a1885 to your computer and use it in GitHub Desktop.
Save jmschrack/9add85a385c3b645c82a48321c9a1885 to your computer and use it in GitHub Desktop.
Creates a grid with squares bisected into triangles. You can pass in an array of int to color certain sectors. It will save to a png
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TriGridImage {
int cellsX,cellsY,resX,resY;
VirtualTriangle[] sectors;
public TriGridImage() {
cellsY=6;
cellsX=12;
resX=resY=432;
Init();
}
public TriGridImage(int cellsY, int resX, int resY) {
this.cellsX=cellsY*2;
this.cellsY=cellsY;
this.resX=resX;
this.resY=resY;
Init();
}
private void Init() {
sectors = new VirtualTriangle[this.cellsX*this.cellsY];
for(int i=0;i<sectors.length;i++) {
sectors[i]=new VirtualTriangle(i,resY/cellsY,this.cellsX);
}
}
public void SetColor(int[] points) {
SetColor(points, Color.magenta);
}
public void SetColor(int[] points, Color c) {
if(points==null)
return;
if(c==null)
c=Color.white;
for(int i=0;i<points.length;i++) {
sectors[points[i]-1].color=c;
}
}
public void SaveToFile() {
//you have chosen foolishly
SaveToFile("GridOutput.png");
}
public void SaveToFile(String path) {
BufferedImage img = new BufferedImage(resX,resY,BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
for(int i=0;i<sectors.length;i++) {
sectors[i].Draw(graphics);
}
try {
ImageIO.write(img, "PNG", new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class VirtualTriangle{
public Color color=Color.white;
public int index,res,cellsPerRow;
public VirtualTriangle(int index, int res,int cellsPerRow) {
this.index=index;
this.res=res;
this.cellsPerRow=cellsPerRow;
}
public void Draw(Graphics g) {
g.setColor(color);
int[] xPoints = new int[3];
int[] yPoints = new int[3];
//starting row
//Graphics coords start at top left, we need to start bottom left
yPoints[0]=(cellsPerRow/2*res)-(index/cellsPerRow)*res;
//starting column
xPoints[0]=((index%cellsPerRow)/2)*res;
int xText,yText;
if(index%2!=0) {
//even text numbers
xPoints[1]=xPoints[0]+res;
yPoints[1]=yPoints[0];
xText=xPoints[0]+res/2;
yText=yPoints[0]-res/4;
}else {
//odd text numbers
xPoints[1]=xPoints[0];
yPoints[1]=yPoints[0]-res;
xText=xPoints[0]+res/4;
yText=yPoints[0]-res/2;
}
xPoints[2]=xPoints[0]+res;
yPoints[2]=yPoints[0]-res;
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(Color.black);
g.drawPolygon(xPoints,yPoints,3);
//we start counting at 1 like a troglodyte
String text =(index<9?"0":"")+(index+1);
g.drawChars(text.toCharArray(), 0, text.length(), xText, yText);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment