Affirmative. I checked the expression several times for accuracy before I responded and I'm extremely disappointed that I failed to catch that mistake. :(
Hi Evac, nah don't be disappointed.. everyone make mistake right? And im extremely grateful that you response to my thread which lead to me solving my problem. Once more thanks for your help and everyone else too!
Is it possible you could post the code behind this calculation? I'd be interested in seeing how it looks, that is, if you have already completed it.
pt1 = x2 - x1;
pt2 = y2 - y1;
pt3 = x4 - x3;
pt4 = y4 - y3;
angle = ( (pt1 * pt3) + (pt2 * pt4) )/((sqrt(pt1*pt1 + pt2*pt2)) * (sqrt(pt3*pt3 + pt4*pt4)) );
result = acos(angle) *180 /PI;
EVAC, here the code.
One more question, since there are a total of 4 points which is 2 lines. So the angle will be calculated from which line to which line? And what is the calculation order of the angle (clockwise, anti-clockwise)? Also if i were to swap the points around, will the result still be consistent?
Help help! thanks
Last edited by larree; 07-03-2006 at 09:38 AM.
Which way the angle is calculated shouldn't really matter. You can only get the same angle but either positive or negative (same absolute value). Just a matter of which line is used for which point... basically just a matter of convention. Here is website explaining a dot product of vectors and the angle in between Website . Keep in mind we are using a vector representation for the lines which is OK since all we really care about is the orientation of the lines.
what about finding the slopes with respect to the x-axis using
. find the slope of each line.
now slope m1=tan @1. similarly find tan@2=m2.
now find @1 and @2 and add them
or simply use
angle between lines @=tan-1( (m1+m2)/1-m1m2)
hope ur problems r over!!!!!
Last edited by ardentmanufan; 07-13-2006 at 09:13 AM.
cool thanks for your feedback i get what you mean. By using this formula will i be able narrow down to calculate the angle by clockwise direction only?
Hi everyone, having problem again.. Anyone knows how to calculate the angle in only either clockwise or anti clockwise direction only? Cause now when i calculate the angle two different line facing different direction pop out with the same angle! Help Help help!
evac how should i do in order to get angle in a fixed direction?
Example will be 270 and 90 instead of both 90.. cheers
Easy angle check of a Vector
I don't believe your system will work about 360 degrees, however if you are suggesting something like this, where a & b are two vectors & you are looking for the angle between them;
Originally Posted by larree
float theta = (float)( (a.x* b.x) + (a.y* b.y) );
float aScale = Mathf.Pow( (Mathf.Sqrt(a.x) + Mathf.Sqrt(a.y)) , 0.5f );
float bScale = Mathf.Pow( (Mathf.Sqrt(b.x) + Mathf.Sqrt(b.y)) , 0.5f );
float angle = Mathf.Cos( theta / (aScale * bScale) );
then this won't work because getting the power of 0.5 on a negative number will be an imaginary number (as will getting the square root of a negative number)
I've found the best thing to do is work out the angle of each vector seperatly & then check them against each other as bellow;
//do this for each vector
float radians = Mathf.Atan2(testVector.y, testVector.x);
float degrees = radians * Mathf.Rad2Deg;
You could also rotate the second vector by the first and then do this, the result is the same but most of the time knowing the angle acording to the x-axis is useful
Hope this helps =]
keep it simple
looking at this a little bit simpler, conceptually:
1. determine which point on each line is closer to the axes origin by finding minimum distance (vector length) [ d = sqrt (x^2 + y^2) ] -- refer to the Pythagorean theorem; furthest point will be the (x2,y2) in steps 2-4, closest point becomes (x1,y1) in next step.
2. determine slope of the line as mentioned above [ m = (y2-y1)/(x2-x1) ] for each line.
3. use the slope (m) and (x2,y2) for each line to calculate new (x1,y1), AKA y-intercept, where x=0 [ y1 = y2 - ( m*x2 ) ].
4. Each line now has points of [(0,y1),(x2,y2)] that reflect original angles.
5. Translate each line "down" the Y axis so that the first point sits atop the axis origin (0,0), yielding (x3 = x2; y3 = y2-y1 where y1 is >= 0 and y3 = y2 + y1 where y1 < 0), giving points [(0,0), (x3,y3)].
6. Angle for each line in degrees is [ result = atan2 (y3,x3) * 180 / PI; ] as mentioned previously.
7. Use sign of each (x3,y3) to determine quadrant for each line and adjust the sign of each [ result ] to calculate the difference correctly.
Yes, this can be optimized, but I find it easier to optimize once I guarantee I am getting the correct answer for all conditions. HTH!
Last Post: 02-20-2010, 03:17 AM
Last Post: 07-18-2005, 09:46 AM
Last Post: 08-12-2002, 10:40 PM
By Rob Teixeira in forum .NET
Last Post: 06-06-2002, 05:23 AM
By Sean Woods in forum VB Classic
Last Post: 02-04-2002, 07:31 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center