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

2013/5/21

Problems with numbers

1
2
Kartoffelbrot Kartoffelbrot

2013/5/21

#
My class Line should draw itself from point1 (x1/y1) to point2 (x2/y2). Therefore I have used the constructor, but everytime I calculate dy/dx the result is 0.0, but I don't know why. Is it a problem with double?
public Line(int x1, int y1, int x2, int y2)
    {
        int dx = x2-x1;
        int dy = y2-y1;
//this is to make dx and dy positive, if they are negative
        if(dx<0)dx-=2*dx;
        if(dy<0)dy-=2*dy;
//This is for test. The code is much longer than this, but I wanted to find the mistake, so I wanted to see all //results like this:
        System.out.println("dx: "+dx);
        System.out.println("dy: "+dy);
        System.out.println("m: "+dy/dx);
        System.out.println("arctan dy/dx: "+Math.atan(dy/dx));
        System.out.println("arctan to degree: "+Math.toDegrees(Math.atan(dy/dx)));
}
But except of dx and dy all values are 0.0, when I tested it with: x1=60 y1=40 x2=200 y2=150 Why?
davmac davmac

2013/5/21

#
When you divide ints (integers - whole numbers) the result is also an int. Cast one of dy or dx to double when you do the division: System.out.println("m: "+ (double)dy/dx);
Kartoffelbrot Kartoffelbrot

2013/5/21

#
Thanks, stupid mistake by me.
davmac davmac

2013/5/21

#
No problem. A suggestion, too, is to change: if(dx<0)dx-=2*dx; To: if(dx<0) dx = -dx; or even just: dx = Math.abs(dx); I think both of these are slightly easier to understand (but it's really a matter of preference).
Kartoffelbrot Kartoffelbrot

2013/5/21

#
I take dx=-dx; It's the easiest. Now I have another problem. My image scales very nice and it works, but only the image is bigger now, not the object, so I can't use the getOneIntersectingObject()-method. Is there a way to scale the object, too, so that it's as big as its image?
Gevater_Tod4711 Gevater_Tod4711

2013/5/21

#
If you just 'reset' the image it'll work:
GreenfootImage img = getImage();
//scale the image;
setImage(img);
I'm not quite shure if also setImage(getImage()) would work. You'll have to try it.
Kartoffelbrot Kartoffelbrot

2013/5/21

#
setImage(getImage()); works. Thank you.
Gevater_Tod4711 Gevater_Tod4711

2013/5/21

#
Which version of Greenfoot do you use?
Kartoffelbrot Kartoffelbrot

2013/5/21

#
2.2.1
Kartoffelbrot Kartoffelbrot

2013/5/21

#
But how should I use this for my animation?
public void animate()
    {
        setImage("Image"+nummer+".png");
        getImage().scale((int)s, getImage().getHeight());
//if I place setImage(getImage()); here it doesn't work
        nummer++;
        if(nummer==7)
            nummer=1;
//this is for the next act, so that the next picture is choosen
    }
In this case it doesn't work, although I used it in the same case.
JetLennit JetLennit

2013/5/21

#
Try
public void animate()  
    {  
        GreenfootImage img = ("Image"+nummer+".png");
        img.scale((int)s, getImage().getHeight());  
        setImage(img); 
        nummer++;  
        if(nummer==7)  
        nummer=1;  
    }  
Kartoffelbrot Kartoffelbrot

2013/5/21

#
It has to be:
GreenfootImage img = new GreenfootImage("Image"+nummer+".png");
Then it works fine. Thank you! Now I can finish the first boss-enemy in Tiny Tank.
JetLennit JetLennit

2013/5/21

#
Sorry, I wrote it in browser.... but your welcome! I am exited for the boss
Kartoffelbrot Kartoffelbrot

2013/5/21

#
How can I write this in java: dx² It isn't necessary, but it would be interesting for me to know.
danpost danpost

2013/5/21

#
U can use: dx * dx or Math.pos(dx, 2) If you are using int values, use the first case (using Math.pow will return a double value, which would require re-casting back to an int).
There are more replies on the next page.
1
2