NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on June 29, 2010, 02:52:02 pm



Title: how to make the angle always acute between two lines?
Post by: lookang on June 29, 2010, 02:52:02 pm
how to make the angle always acute angle (0 < 90 degree) between two lines defined between (x,y) and (xs,ys)?

i have been able to make a polygon to be drawn be using this method

// dcangle is to spilt the angle between cta and ctas into parts
dcangle = (Math.atan2(y,x)-Math.atan2(ys,xs))/(n-2);// modify to draw from black line sing dx and dy as inputs
// px[0] and px[0] define the centre of polygon
px[0]=x0; // for drawing angle polygon
py[0]=y0; //
// if statement to draw polygon angle
for(int i = 1;i//  c=Math.atan2(y,x)-(i-1)*dcangle;  // modify to work drawing from black line
  c=Math.atan2(y,x)-(i-1)*dcangle;
// if(c>pi)c-=2*pi; // to overcome pi to -pi problem ?? didnt work for me  ;D
  px[ i ]=x0+d*Math.cos(c); //
  py[ i ]=y0+d*Math.sin(c); //
}
xdeltatheta= px[17]  ; // x coordinate for text angle delta theta
ydeltatheta =py[17]  ; // y coordinate for text angle delta theta

this method is remixed from http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=634.0 so i learn from changing it.

is there any elegant way to do this? how would an expert do this?

i want to make delta theta always acute angle 0 < 90 degree
the 'problem' occurs when in 2 situations:
1 (xs,ys) is in the 2nd quadrant (top right corner) and (x,y) is in 3rd quadrant (bottom left corner)
2 (x,y) is in the 2nd quadrant (top right corner) and (xs,ys) is in 3rd quadrant (bottom left corner)

the problem is captured here (http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=1874.0;attach=3154;image)

usually is work well thanks to all your source codes i was able to refer to them! :) (http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=1874.0;attach=3156;image)

Thx!


Title: Re: how to make the angle always acute between two lines?
Post by: lookang on June 29, 2010, 09:46:00 pm
don't to worry! i did it!

done!

tn = Math.atan2(y,x);
tns =Math.atan2(ys,xs);

if (Math.abs(tn-tns)>pi) // test whether tn = +3.1 and tns = -2.9 , pi is an arbitrary large angle that i thought was convience
{
dcangle = (Math.atan2(y,x)-2*pi-Math.atan2(ys,xs))/(n-2);
}
else
{
dcangle = (Math.atan2(y,x)-Math.atan2(ys,xs))/(n-2);// same as before
}


Title: Re: how to make the angle always acute between two lines?
Post by: Fu-Kwun Hwang on June 29, 2010, 11:06:13 pm
I will define two variables:
ca=Math.atan2(y,x);
cb=Math.atan2(ys,xs);

And replace all the Math.atan2(y,x) with ca, and Math.atan2(ys,xs) with cb.
So that the code do not to call Math.atan2 a lot of times (especially inside loops).

Try to reduce calculation inside any loop. It will make the program more efficient.