use of org.mariuszgromada.math.mxparser.Expression in project Structurize by ldtteam.
the class Manager method generateRandomShape.
/**
* Randomly generates shape based on an equation.
*
* @param height the height.
* @param width the width.
* @param length the length.
* @param equation the equation.
* @param block the block.
* @return the created blueprint
*/
public static Blueprint generateRandomShape(final int height, final int width, final int length, final String equation, final BlockState block) {
Expression e = new Expression(equation);
final Argument argumentX = new Argument("x = 0");
final Argument argumentY = new Argument("y = 0");
final Argument argumentZ = new Argument("z = 0");
final Argument argumentH = new Argument("h = " + height);
final Argument argumentW = new Argument("w = " + width);
final Argument argumentL = new Argument("l = " + length);
e.addArguments(argumentX, argumentY, argumentZ, argumentH, argumentW, argumentL);
final Map<BlockPos, BlockState> posList = new HashMap<>();
for (double x = -length / 2.0; x <= length / 2; x++) {
for (double y = -height / 2.0; y <= height / 2; y++) {
for (double z = -width / 2.0; z <= width / 2; z++) {
argumentX.setArgumentValue(x);
argumentY.setArgumentValue(y);
argumentZ.setArgumentValue(z);
if (e.calculate() == 1) {
addPosToList(new BlockPos(x + length / 2.0, y + height / 2.0, z + width / 2.0), block, posList);
}
}
}
}
final Blueprint blueprint = new Blueprint((short) (length + 1), (short) (height + 1), (short) (width + 1));
posList.forEach(blueprint::addBlockState);
return blueprint;
}
use of org.mariuszgromada.math.mxparser.Expression in project AdvancedChatBox by DarkKronicle.
the class CalculatorSuggestor method suggest.
@Override
public Optional<List<AdvancedSuggestions>> suggest(String text) {
if (!text.contains("[") || !text.contains("]")) {
return Optional.empty();
}
List<StringMatch> matches = SearchUtils.findMatches(text, BRACKET_REGEX, FindType.REGEX).orElse(null);
if (matches == null) {
return Optional.empty();
}
int last = -1;
ArrayList<AdvancedSuggestions> suggest = new ArrayList<>();
for (StringMatch m : matches) {
if (m.start < last || m.end - m.start < 1) {
// Don't want overlapping matches (just in case) or too small
continue;
}
last = m.end;
String string = m.match.substring(1, m.match.length() - 1);
Expression expression = new Expression(string);
double val = expression.calculate();
String message = NAN;
if (!Double.isNaN(val)) {
message = String.valueOf(val);
}
StringRange range = new StringRange(m.start, m.end);
suggest.add(new AdvancedSuggestions(range, new ArrayList<>(Collections.singleton(new AdvancedSuggestion(range, message)))));
}
if (suggest.isEmpty()) {
return Optional.empty();
}
return Optional.of(suggest);
}
Aggregations