mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -56,7 +56,8 @@ public class KochSnowflake {
|
||||
assert image.getRGB(0, 0) == new Color(255, 255, 255).getRGB();
|
||||
|
||||
// The snowflake is drawn in black and this is the position of the first vector
|
||||
assert image.getRGB((int) offsetX, (int) offsetY) == new Color(0, 0, 0).getRGB();
|
||||
assert image.getRGB((int) offsetX, (int) offsetY) ==
|
||||
new Color(0, 0, 0).getRGB();
|
||||
|
||||
// Save image
|
||||
try {
|
||||
@ -76,7 +77,10 @@ public class KochSnowflake {
|
||||
* @param steps The number of iterations.
|
||||
* @return The transformed vectors after the iteration-steps.
|
||||
*/
|
||||
public static ArrayList<Vector2> Iterate(ArrayList<Vector2> initialVectors, int steps) {
|
||||
public static ArrayList<Vector2> Iterate(
|
||||
ArrayList<Vector2> initialVectors,
|
||||
int steps
|
||||
) {
|
||||
ArrayList<Vector2> vectors = initialVectors;
|
||||
for (int i = 0; i < steps; i++) {
|
||||
vectors = IterationStep(vectors);
|
||||
@ -94,14 +98,18 @@ public class KochSnowflake {
|
||||
*/
|
||||
public static BufferedImage GetKochSnowflake(int imageWidth, int steps) {
|
||||
if (imageWidth <= 0) {
|
||||
throw new IllegalArgumentException("imageWidth should be greater than zero");
|
||||
throw new IllegalArgumentException(
|
||||
"imageWidth should be greater than zero"
|
||||
);
|
||||
}
|
||||
|
||||
double offsetX = imageWidth / 10.;
|
||||
double offsetY = imageWidth / 3.7;
|
||||
Vector2 vector1 = new Vector2(offsetX, offsetY);
|
||||
Vector2 vector2
|
||||
= new Vector2(imageWidth / 2, Math.sin(Math.PI / 3) * imageWidth * 0.8 + offsetY);
|
||||
Vector2 vector2 = new Vector2(
|
||||
imageWidth / 2,
|
||||
Math.sin(Math.PI / 3) * imageWidth * 0.8 + offsetY
|
||||
);
|
||||
Vector2 vector3 = new Vector2(imageWidth - offsetX, offsetY);
|
||||
ArrayList<Vector2> initialVectors = new ArrayList<Vector2>();
|
||||
initialVectors.add(vector1);
|
||||
@ -122,15 +130,23 @@ public class KochSnowflake {
|
||||
* applied.
|
||||
* @return The transformed vectors after the iteration-step.
|
||||
*/
|
||||
private static ArrayList<Vector2> IterationStep(ArrayList<Vector2> vectors) {
|
||||
private static ArrayList<Vector2> IterationStep(
|
||||
ArrayList<Vector2> vectors
|
||||
) {
|
||||
ArrayList<Vector2> newVectors = new ArrayList<Vector2>();
|
||||
for (int i = 0; i < vectors.size() - 1; i++) {
|
||||
Vector2 startVector = vectors.get(i);
|
||||
Vector2 endVector = vectors.get(i + 1);
|
||||
newVectors.add(startVector);
|
||||
Vector2 differenceVector = endVector.subtract(startVector).multiply(1. / 3);
|
||||
Vector2 differenceVector = endVector
|
||||
.subtract(startVector)
|
||||
.multiply(1. / 3);
|
||||
newVectors.add(startVector.add(differenceVector));
|
||||
newVectors.add(startVector.add(differenceVector).add(differenceVector.rotate(60)));
|
||||
newVectors.add(
|
||||
startVector
|
||||
.add(differenceVector)
|
||||
.add(differenceVector.rotate(60))
|
||||
);
|
||||
newVectors.add(startVector.add(differenceVector.multiply(2)));
|
||||
}
|
||||
|
||||
@ -147,8 +163,15 @@ public class KochSnowflake {
|
||||
* @return The image of the rendered edges.
|
||||
*/
|
||||
private static BufferedImage GetImage(
|
||||
ArrayList<Vector2> vectors, int imageWidth, int imageHeight) {
|
||||
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
|
||||
ArrayList<Vector2> vectors,
|
||||
int imageWidth,
|
||||
int imageHeight
|
||||
) {
|
||||
BufferedImage image = new BufferedImage(
|
||||
imageWidth,
|
||||
imageHeight,
|
||||
BufferedImage.TYPE_INT_RGB
|
||||
);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
|
||||
// Set the background white
|
||||
|
Reference in New Issue
Block a user