我在 java 中做Koch 分形雪花并将其保存在 svg 文件中。
我正在使用 LineStrip2D 类来记忆分形(它是实现可迭代的 Vec2D 的 ArrayList 的包装器)。
主要功能是这个:
public static LineStrip2D repeatPatternIntoLineStrip2D(
LineStrip2D pattern,
LineStrip2D polygon,
boolean repeatUp) {
/*
* pattern: must be a pattern between Vec(0,0) and Vec(1,0)
* (normalized beetween 0-1 and in X axis)
* */
float angle, distance;
Vec2D pivot, a, b, direction, b1;
LineStrip2D new_polygon = new LineStrip2D();
new_polygon.add(pattern.vertices.get(0));
a = polygon.vertices.get(0);
int count=0;
for (int i = 1; i < polygon.vertices.size(); i++) {
b = polygon.vertices.get(i);
a = polygon.vertices.get(i-1);
distance = b.distanceTo(a);
direction = b.sub(a).normalize();
angle = PApplet.atan2(direction.y, direction.x);
pivot = a;
for (int j = 1; j < pattern.vertices.size(); j++) {
Vec2D _b1 = pattern.vertices.get(j);
b1 = _b1.copy() ;
if(repeatUp)b1.y *= -1;
b1.scaleSelf(distance);
b1 = b1.rotate(angle);
b1.addSelf(pivot);
new_polygon.add(b1);
count++;
}
a = b;
}
System.out.println(count);
return new_polygon;
}
我有一个带有初始科赫曲线的模式:
我打电话给:
pattern = GeometryHelper.repeatPatternIntoLineStrip2D(pattern, pattern, false);
现在的问题:
经过一些迭代(851968)我有一个java.lang.OutOfMemoryError: Java heap space。如何避免此错误并获得巨大的 svg 文件?我想我可以通过不同的步骤来完成这个过程,但我不明白如何以一种聪明的方式实现它。