From 6e83f8c0d9c793fdf7702be1d197b7014f384d2c Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 13 Apr 2021 17:38:49 +0200 Subject: [PATCH] add comment to methods in lsystem.engine --- build.xml | 4 ++-- src/lsystem/engine/Element.java | 16 +++++++++++++++ src/lsystem/engine/ElementProperties.java | 13 ++++++++++-- src/lsystem/engine/Parser.java | 23 ++++++++++++++++++++- src/lsystem/engine/Rewrite.java | 25 ++++++++++++++++++++++- 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/build.xml b/build.xml index 8c1aa16..b225359 100644 --- a/build.xml +++ b/build.xml @@ -33,12 +33,12 @@ - + - + diff --git a/src/lsystem/engine/Element.java b/src/lsystem/engine/Element.java index 1aed8ab..ba2ac71 100644 --- a/src/lsystem/engine/Element.java +++ b/src/lsystem/engine/Element.java @@ -2,11 +2,27 @@ package lsystem.engine; import java.util.ArrayList; +/** + * Object created by {@link Parser#parse(String)}, represent a tree with {@link Element#children branches} + * @see ElementProperties + * @see Parser#parse(String) + */ public class Element { + /** + * stored value can be {@link ElementProperties#NOTHING} or {@link ElementProperties#DRAW}
+ * {@linkplain ElementProperties#NOTHING NOTHING} is used to control the evolution
+ * and {@linkplain ElementProperties#DRAW DRAW} is used to draw a 1 unit high cylinder + */ public final ElementProperties property; public final Element parent; + /** + * Rotation applied to this element, 3-dim tab which represent yaw, pitch and roll rotation + */ public final float[] rotation; + /** + * Branches of the tree + */ public final ArrayList children = new ArrayList<>(); public Element(ElementProperties property, Element parent) { diff --git a/src/lsystem/engine/ElementProperties.java b/src/lsystem/engine/ElementProperties.java index 1222dd1..3d7da8f 100644 --- a/src/lsystem/engine/ElementProperties.java +++ b/src/lsystem/engine/ElementProperties.java @@ -1,13 +1,22 @@ package lsystem.engine; +/** + * @see Element + * @see Parser#parse(String) + */ public enum ElementProperties { - + /** + * used to draw a 1 unit high cylinder + */ DRAW('X'), + /** + * used to control the evolution + */ NOTHING('Y'), ROTATION_X('x', (byte) 0), ROTATION_Y('y', (byte) 1); - + // char is a java reserved word, we replace "a" by a "4" to avoid this problem private final char ch4r; private final byte direction; diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index df3766a..ffffddb 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -8,6 +8,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +/** + * check if given axiom and rules are correct and parse {@link Rewrite rewritten} word to an {@link Element} object + */ public class Parser { private final String axiom; @@ -37,7 +40,12 @@ public class Parser { return bl; } - + /** + * + * @param stringToCheck check if the string respect the syntax + * @param type to help the program know if the string is an axiom or a rule to check if is correct + * @return {@code true} if {@code stringToCheck} respect the syntax, {@code false} otherwise + */ private boolean isCorrect(String stringToCheck, Type type) { if(type == Type.RULE && !stringToCheck.contains("=")) return false; @@ -91,6 +99,12 @@ public class Parser { return rules; } + /** + * convert a rewritten string to an organized L-System stored like a tree as an {@link Element} object + * @param word rewritten word + * @return the organized L-System object + * @throws NumberFormatException if an number has been written incorrectly in {@code word} param + */ public static Element parse(String word) throws NumberFormatException { char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-'}; Element root = null; @@ -146,6 +160,13 @@ public class Parser { return root; } + /** + * Add some pre condition before execute {@link Float#parseFloat(String)} like if {@code number} give a {@code +} + * or an empty {@link String}, method will return 0.25° and {@code -} will return 0.25° + * @param number the number which will be converted + * @return the converted float from {@code number} param + * @throws NumberFormatException throw by {@link Float#parseFloat(String)} + */ private static float getFloat(String number) throws NumberFormatException { float n; if(number.equals("") || number.equals("+")) diff --git a/src/lsystem/engine/Rewrite.java b/src/lsystem/engine/Rewrite.java index 329806f..c85be8d 100644 --- a/src/lsystem/engine/Rewrite.java +++ b/src/lsystem/engine/Rewrite.java @@ -4,9 +4,18 @@ import lsystem.utils.Pair; import java.util.List; +/** + * Used to rewrite axiom depending of given rules + */ public class Rewrite { - + /** + * replace occurrences of left side of {@code rules} to ${#ruleId} in {@code rewritten} + * @see Pair + * @param rewritten initial word + * @param rules list of rules to rewrite {@code rewritten} + * @return modified rewritten word + */ private static String replaceRulesByID(final String rewritten, List> rules) { String toRewrite = rewritten; for(int j = 0; j < rules.size(); ++j){ @@ -16,6 +25,13 @@ public class Rewrite { return toRewrite; } + /** + * replace every occurrences of ${#ruleId} to the right side of {@code rules} in {@code toRewrite} + * @see Pair + * @param toRewrite initial word + * @param rules list of rules to rewrite {@code toRewrite} + * @return final rewritten word + */ private static String replaceIDByRuleApplication(final String toRewrite, List> rules) { String rewritten = toRewrite; for(int j = 0; j < rules.size(); ++j){ @@ -24,6 +40,13 @@ public class Rewrite { return rewritten; } + /** + * rewrite {@code axiom} iteratively + * @param axiom initial word + * @param rules list of rules to rewrite {@code axiom} + * @param recurrences number a time we will execute the loop + * @return rewritten word + */ public static String rewrite(String axiom, List> rules, int recurrences) { String rewritten = axiom; for(int i = 0; i < recurrences; ++i) {