bitart: Both x and y must occur in the expression

The expression may still result in stripes, but less often.
This commit is contained in:
Juergen Stuber
2026-03-13 14:53:02 +01:00
parent baa5fba044
commit 889ad71c62
2 changed files with 20 additions and 2 deletions

View File

@@ -23,7 +23,7 @@ impl fmt::Display for Literal {
} }
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Variable { pub enum Variable {
X, X,
Y, Y,
@@ -93,6 +93,9 @@ impl UnaryOperation {
fn eval(&self, env: &Env) -> i64 { fn eval(&self, env: &Env) -> i64 {
self.operator.apply(self.operand.eval(env)) self.operator.apply(self.operand.eval(env))
} }
fn contains_var(&self, variable: Variable) -> bool {
self.operand.contains_var(variable)
}
} }
impl fmt::Display for UnaryOperation { impl fmt::Display for UnaryOperation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -180,6 +183,9 @@ impl BinaryOperation {
self.operator self.operator
.apply(self.operands[0].eval(env), self.operands[1].eval(env)) .apply(self.operands[0].eval(env), self.operands[1].eval(env))
} }
fn contains_var(&self, variable: Variable) -> bool {
self.operands.iter().any(|e| e.contains_var(variable))
}
} }
impl fmt::Display for BinaryOperation { impl fmt::Display for BinaryOperation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -207,6 +213,14 @@ impl InnerExpression {
InnerExpression::BinaryOperation(op) => op.eval(env), InnerExpression::BinaryOperation(op) => op.eval(env),
} }
} }
pub fn contains_var(&self, variable: Variable) -> bool {
match self {
InnerExpression::Literal(_) => false,
InnerExpression::Variable(v) => *v == variable,
InnerExpression::UnaryOperation(op) => op.contains_var(variable),
InnerExpression::BinaryOperation(op) => op.contains_var(variable),
}
}
} }
impl fmt::Display for InnerExpression { impl fmt::Display for InnerExpression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@@ -18,6 +18,7 @@ use pixelfoo_apps::color::Color;
mod expression; mod expression;
use expression::Env; use expression::Env;
use expression::RandomExpressionBuilder; use expression::RandomExpressionBuilder;
use expression::Variable;
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Square { enum Square {
@@ -129,7 +130,10 @@ fn main() -> std::io::Result<()> {
let percent = 100 * max_count / bbox.area(); let percent = 100 * max_count / bbox.area();
if (min_percent..=max_percent).contains(&percent) { if expression.contains_var(Variable::X)
&& expression.contains_var(Variable::Y)
&& (min_percent..=max_percent).contains(&percent)
{
eprintln!("chose expression {expression}"); eprintln!("chose expression {expression}");
old_board = new_board; old_board = new_board;