This site requires JavaScript, please enable it in your browser!
Greenfoot back
gusbus123
gusbus123 wrote ...

2012/8/30

in need of a complicated mathamatical equation

gusbus123 gusbus123

2012/8/30

#
In one of my scenario's i need a mathamatical equation that needs to work without using if statements and those stuff, its for the world generation. The equation uses an input of an amount. If the amount is less than 5 it will need to = 0. otherwise it will be as the amount is inputed. Is there a way to do this?
nccb nccb

2012/8/30

#
Why can't you use if statements? Options:
  • Use an if statement!
  • Use a conditional: (x < 5 ? 0 : x)
  • Use something stupid, like Math.signum(1 + Math.signum(x - 5)) * x
gusbus123 gusbus123

2012/8/30

#
i canr use if statements cause its in the world size generator, and it doesnt allow it to be used. EDIT: more or less it just needs to work inside the world size generator, cause i need it to generate the width but i want to see if theres a way to just use 1 world generation instead of 2 for this
nccb nccb

2012/8/30

#
Ah, I understand: you need to pass the equation to the superclass constructor, which has to be the first line of the constructor. You can either use the conditional syntax (second option above), or you can define a private static method in your class to do the calculation, and call that, e.g.
class MyWorld extends World
{
    public MyWorld(int someParam)
    {
        super(calcX(someParam), calcY(someParam), 1);
    }

    private static int calcX(int param) { /* code with if */ }
    private static int calcY(int param) { /* code with if */ }
}
gusbus123 gusbus123

2012/8/30

#
ok thx. seems like theres always something simple behind every hidden corner.
You need to login to post a reply.