degrees question

hi ,

this is my code

  formule = Math.abs( Xlength) /  ( Math.sqrt( Math.power(Xlenght,2)+Math.power(Yhight,2) )  )
  now i need to put that formule into arc  (degrees)
  example : the arc from (0.706214689) = 45°


   Is there a Math formule for it to covert it to arc?

example:

http://users.pandora.be/ganon/java/example.JPG

I want that the drawed line is always directed to the player.

thx for helping

double angle = Math.atan2(objectB.y-objectA.y,objectB.x-objectA.x);

thx i’ve found this code :
lijnbreedte is the Xpos of the beginning of the drawed line
lijnhoogte is the Ypos of the beginning of the drawed line
Straal is the length of the drawed line

The code :

if (player.getY() <= lijnhoogte && player.getX() >= lijnbreedte){
graden = (int) (90 - Math.toDegrees(Math.acos((player.getX()- lijnbreedte)/Math.sqrt((Math.pow(player.getX()- lijnbreedte,2)+ Math.pow(player.getY()- lijnhoogte,2))))));
}
if (player.getY() > lijnhoogte){
graden = (int) (90 + Math.toDegrees(Math.acos((player.getX()- lijnbreedte)/Math.sqrt((Math.pow(player.getX()- lijnbreedte,2)+ Math.pow(player.getY()- lijnhoogte,2))))));
}
if (player.getY() <= lijnhoogte && player.getX() < lijnbreedte){
graden = (int) (270+(180-Math.toDegrees(Math.acos((player.getX()- lijnbreedte)/Math.sqrt((Math.pow(player.getX()- lijnbreedte,2)+ Math.pow(player.getY()- lijnhoogte,2)))))));
}

 g.drawOval(lijnbreedte - straal,lijnhoogte - straal,2*straal,2*straal);
 double hoek = (2*Math.PI)/360*graden;
 g.drawLine(lijnbreedte,lijnhoogte,(int)(lijnbreedte+straal* Math.sin(hoek)),(int)(lijnhoogte-straal* Math.cos(hoek)));

And it works :slight_smile:

thx for helping me out, i’m going to try your code now.

You really really don’t want to debug such code… :o

What about:


double xDiff    = player.getX() - lijnbreedte;
double yDiff    = player.getY() - lijnhoogte;
double distance = Math.sqrt(xDiff*xDiff + yDiff*yDiff);
double rad      = Math.acos(xDiff / distance);
double degrees  = Math.toDegrees(rad);



if(player.getY() <= lijnhoogte)
{
   // if(player.getX() >= lijnbreedte)
   // {
   //    degrees = (int) (90 - degrees);
   // }
   // else
   // {
   //    degrees = (int) (270 + 180 - degrees); // (90 - degrees)
   // }

   // as both are 90 - degrees...

   degrees = 90.0 - degrees;
}
else
{
   degrees = 90.0 + degrees;
}



g.drawOval(lijnbreedte-straal, lijnhoogte-straal,2*straal,2*straal);
double hoek = Math.toRadians(degrees);
g.drawLine(lijnbreedte, lijnhoogte, (int)(lijnbreedte+straal*Math.sin(hoek)), (int)(lijnhoogte-straal*Math.cos(hoek)));

You see how much clearer the code is, and how - as a result of this - it became obvious you could optimize it as you calculated the same thing twice in different situations. You’d never have found that in that ‘mess’. :-*