NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
January 27, 2021, 12:19:20 pm
 Welcome, Guest. Please login or register.Did you miss your activation email? 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length

 Home Help Search Login Register
"Vision, mission and action." ...Wisdom

 Pages: [1]   Go Down
 Author Topic: how to make the angle always acute between two lines?  (Read 4460 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message on: June 29, 2010, 02:52:02 pm » posted from:Singapore,,Singapore

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
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

usually is work well thanks to all your source codes i was able to refer to them!

Thx!
 wrongdelattheta.png (9.98 KB, 513x529 - viewed 414 times.)  wrongdelatthetacorrectmostofthetime.png (11.17 KB, 505x534 - viewed 426 times.) *** There are 1 more attached files. You need to login to acces it! « Last Edit: June 29, 2010, 03:02:25 pm by lookang » Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #1 on: June 29, 2010, 09:46:00 pm » posted from:Singapore,,Singapore

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
}
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #2 on: June 29, 2010, 11:06:13 pm » posted from:Taipei,T\'ai-pei,Taiwan

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.
 Logged
 Pages: [1]   Go Up
"Vision, mission and action." ...Wisdom