Package com.google.common.geometry
Class S2FractalBuilder
java.lang.Object
com.google.common.geometry.S2FractalBuilder
A simple class that generates "Koch snowflake" fractals (see Wikipedia for an introduction).
There is an option to control the fractal dimension (between 1.0 and 2.0); values between 1.02
and 1.50 are reasonable simulations of various coastlines. The default dimension (about 1.26)
corresponds to the standard Koch snowflake. (The west coast of Britain has a fractal dimension of
approximately 1.25)
The fractal is obtained by starting with an equilateral triangle and recursively subdividing each edge into four segments of equal length. Therefore the shape at level 'n' consists of 3 * (4^n) edges. Multi-level fractals are also supported: if you set minLevel() to a non-negative value, then the recursive subdivision has an equal probability of stopping at any of the levels between the given min and max (inclusive). This yields a fractal where the perimeter of the original triangle is approximately equally divided between fractals at the various possible levels. If there are k distinct levels {min, ..., max}, the expected number of edges at each level 'i' is approximately 3 * (4 ^ i) / k.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate double
Standard Koch curveprivate double
The ratio of the sub-edge length to the original edge length at each subdivision step.private int
private int
Actual min level (depends on maxLevel.)private int
Value set by user.private double
The distance from the original edge to the middle vertex at each subdivision step, as a fraction of the original edge length.private Random
-
Constructor Summary
ConstructorsConstructorDescriptionS2FractalBuilder
(Random rand) You must call setMaxLevel() or setLevelForApproxMaxMedges() before calling makeLoop(). -
Method Summary
Modifier and TypeMethodDescriptionprivate void
private void
private void
getR2Vertices
(List<R2Vector> vertices) private void
getR2VerticesHelper
(R2Vector v0, R2Vector v4, int level, List<R2Vector> vertices) Given the two endpoints (v0, v4) of an edge, recursively subdivide the edge to the desired level, and insert all vertices of the resulting curve up to but not including the endpoint "v4".private static int
levelFromEdges
(int edges) Returns level from values in the range [1.5 * (4 ^ n), 6 * (4 ^ n)].Returns a fractal loop centered around the a-axis of the given coordinate frame, with the first vertex in the direction of the positive x-axis, and the given nominal radius.makeVertices
(Matrix3x3 frame, S1Angle nominalRadius) AsmakeLoop(Matrix3x3, S1Angle)
except it returns the vertices instead of loop.double
Returns the ratio (Rmax / R), where 'R' is the radius passed to makeLoop() and 'Rmax' is the maximum distance from the fractal boundary to its center.double
Returns a lower bound on the ratio (Rmin / R), where 'R' is the radius passed to makeLoop() and 'Rmin' is the minimum distance from the fractal boundary to its center.void
setFractalDimension
(double dimension) Sets the fractal dimension.void
setLevelForApproxMaxEdges
(int maxEdges) void
setLevelForApproxMinEdges
(int minEdges) The following two functions set the min and/or max level to produce approximately the given number of edges.void
setMaxLevel
(int maxLevel) Sets the maximum subdivision level for the fractal (see above).void
setMinLevel
(int minLevelArg) Sets the minimum subdivision level for the fractal (see above).
-
Field Details
-
maxLevel
private int maxLevel -
minLevelArg
private int minLevelArgValue set by user. -
minLevel
private int minLevelActual min level (depends on maxLevel.) -
dimension
private double dimensionStandard Koch curve -
edgeFraction
private double edgeFractionThe ratio of the sub-edge length to the original edge length at each subdivision step. -
offsetFraction
private double offsetFractionThe distance from the original edge to the middle vertex at each subdivision step, as a fraction of the original edge length. -
rand
-
-
Constructor Details
-
S2FractalBuilder
You must call setMaxLevel() or setLevelForApproxMaxMedges() before calling makeLoop().
-
-
Method Details
-
setMaxLevel
public void setMaxLevel(int maxLevel) Sets the maximum subdivision level for the fractal (see above). -
setMinLevel
public void setMinLevel(int minLevelArg) Sets the minimum subdivision level for the fractal (see above). The default value of -1 causes the min and max levels to be the same. A minLevel of 0 should be avoided since this creates a significant chance that none of the three original edges will be subdivided at all. -
computeMinLevel
private void computeMinLevel() -
setFractalDimension
public void setFractalDimension(double dimension) Sets the fractal dimension. The default value of approximately 1.26 corresponds to the standard Koch curve. The value must lie in the range [1.0, 2.0). -
computeOffsets
private void computeOffsets() -
setLevelForApproxMinEdges
public void setLevelForApproxMinEdges(int minEdges) The following two functions set the min and/or max level to produce approximately the given number of edges. (The values are rounded to a nearby value of 3 * (4 ^ n).) -
setLevelForApproxMaxEdges
public void setLevelForApproxMaxEdges(int maxEdges) -
levelFromEdges
private static int levelFromEdges(int edges) Returns level from values in the range [1.5 * (4 ^ n), 6 * (4 ^ n)]. -
minRadiusFactor
public double minRadiusFactor()Returns a lower bound on the ratio (Rmin / R), where 'R' is the radius passed to makeLoop() and 'Rmin' is the minimum distance from the fractal boundary to its center. This can be used to inscribe another geometric figure within the fractal without intersection. -
maxRadiusFactor
public double maxRadiusFactor()Returns the ratio (Rmax / R), where 'R' is the radius passed to makeLoop() and 'Rmax' is the maximum distance from the fractal boundary to its center. This can be used to inscribe the fractal within some other geometric figure without intersection. -
getR2Vertices
-
getR2VerticesHelper
Given the two endpoints (v0, v4) of an edge, recursively subdivide the edge to the desired level, and insert all vertices of the resulting curve up to but not including the endpoint "v4". -
makeLoop
Returns a fractal loop centered around the a-axis of the given coordinate frame, with the first vertex in the direction of the positive x-axis, and the given nominal radius. -
makeVertices
AsmakeLoop(Matrix3x3, S1Angle)
except it returns the vertices instead of loop.
-