-
Oops... Sorry
Affirmative. I checked the expression several times for accuracy before I responded and I'm extremely disappointed that I failed to catch that mistake. :(
-EVAC
-
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!
cheers
-
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.
EVAC
-
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.
-
hey laree,
what about finding the slopes with respect to the x-axis using
slope=(y1-y2)/(x1-x2)=m1=tan@1
. 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.
Reason: error
-
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
 Originally Posted by larree
Hi everyone just to clarify again the formula
Cosine( Theta ) = ( (a x c) + (b x d) )/(Sqrt(a^2 + b^2) x Sqrt(b^2 + d^2) ) = Z.
is correct?
Or is the second part of the sqrt should be (c^2 + d^2) instead of Sqrt(b^2 + d^2)?
please advise... thanks
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;
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) );
return angle;
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;
degrees+=180;
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!
Similar Threads
-
Replies: 4
Last Post: 02-20-2010, 03:17 AM
-
Replies: 0
Last Post: 07-18-2005, 09:46 AM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By Rob Teixeira in forum .NET
Replies: 129
Last Post: 06-06-2002, 05:23 AM
-
By Sean Woods in forum VB Classic
Replies: 0
Last Post: 02-04-2002, 07:31 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks