Skip to content

Instantly share code, notes, and snippets.

@malte0811
Created December 1, 2017 17:24
Show Gist options
  • Save malte0811/af446dd42583fe690d48ec35a2a49ed5 to your computer and use it in GitHub Desktop.
Save malte0811/af446dd42583fe690d48ec35a2a49ed5 to your computer and use it in GitHub Desktop.
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.function.Function;
public class MatrixGrafik {
public static BufferedImage draw(double min, double max, int sizePx, double[][] mat, double[][] startVecs, int iterations) {
BufferedImage ret = new BufferedImage(sizePx, sizePx, BufferedImage.TYPE_4BYTE_ABGR);
Graphics g = ret.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, sizePx, sizePx);
//Draw coordinate system
g.setColor(Color.BLACK);
g.drawLine(0, sizePx/2, sizePx, sizePx/2);
g.drawLine(sizePx/2, 0, sizePx/2, sizePx);
g.fillPolygon(
new int[]{sizePx-10, sizePx-10, sizePx},
new int[]{sizePx/2+3, sizePx/2-3, sizePx/2},
3);
g.fillPolygon(
new int[]{sizePx/2+3, sizePx/2-3, sizePx/2},
new int[]{10, 10, 0},
3);
Function<Double, Integer> posFromValue = (d)->(int)((d-min)/(max-min)*sizePx);
for (double i = Math.ceil(min);i<Math.floor(max); i++) {
g.drawLine(posFromValue.apply(i), sizePx/2-3, posFromValue.apply(i), sizePx/2+3);
g.drawLine(sizePx/2-3, sizePx-posFromValue.apply(i), sizePx/2+3, sizePx-posFromValue.apply(i));
}
double factor = 1/(mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0]);
double[][] inv = {{factor*mat[1][1], -factor*mat[0][1]}, {-factor*mat[1][0], factor*mat[0][0]}};
for (int i = 0;i<startVecs.length;i++) {
drawMat(mat, startVecs[i], posFromValue, g, iterations, Color.RED);
drawMat(inv, startVecs[i], posFromValue, g, iterations, Color.BLUE);
}
return ret;
}
private static void drawMat(double[][] mat, double[] startVec, Function<Double, Integer> posFromValue, Graphics out,
int iterations, Color point) {
double[] last = null;
out.setColor(Color.YELLOW);
for (int i = 0;i<iterations;i++) {
out.fillOval(posFromValue.apply(startVec[0])-3, posFromValue.apply(startVec[1])-3, 6, 6);
if (last!=null) {
out.setColor(Color.GREEN);
out.drawLine(posFromValue.apply(last[0]), posFromValue.apply(last[1]),
posFromValue.apply(startVec[0]), posFromValue.apply(startVec[1]));
}
out.setColor(point);
last = startVec;
double[] tmp = new double[2];
tmp[0] = mat[0][0]*startVec[0]+mat[0][1]*startVec[1];
tmp[1] = mat[1][0]*startVec[0]+mat[1][1]*startVec[1];
startVec = tmp;
}
}
public static void generateImage(double[][] mat, int id, double startC) throws IOException {
double[][] start = {{0, startC}, {startC, 0}, {-startC, 0}, {0, -startC}, {0, 0},
{startC, startC}, {-startC, startC}, {startC, -startC}, {-startC, -startC}};
BufferedImage img = draw(-10, 10, 1000, mat, start, 5);
ImageIO.write(img, "png", new File("matrix_"+id+".png"));
}
public static void main(String[] args) throws IOException {
int id = 1;
generateImage(new double[][]{{2, 0}, {0, 3}}, id++, 1);
generateImage(new double[][]{{2, 0}, {0, 1}}, id++, 1);
generateImage(new double[][]{{2, 0}, {0, .5}}, id++, 1);
generateImage(new double[][]{{1, 0}, {0, .5}}, id++, 5);
generateImage(new double[][]{{.75, 0}, {0, .25}}, id++, 2);
generateImage(new double[][]{{2, 0}, {0, 2}}, id++, 5);
generateImage(new double[][]{{1, 0}, {0, 1}}, id++, 5);
generateImage(new double[][]{{.5, 0}, {0, .5}}, id++, 5);
generateImage(new double[][]{{2, 1}, {0, 2}}, id++, 5);
generateImage(new double[][]{{1, 1}, {0, 1}}, id++, 5);
generateImage(new double[][]{{.5, 1}, {0, .5}}, id++, 5);
double a = .5, b = 1.1;
generateImage(new double[][]{{a, -b}, {b, a}}, id++, 5);
a = Math.cos(Math.toRadians(10));
b = Math.sin(Math.toRadians(10));
generateImage(new double[][]{{a, -b}, {b, a}}, id++, 5);
a = .9;
b = .8;
generateImage(new double[][]{{a, -b}, {b, a}}, id++, 5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment