 A Flash Developer Resource Site

# Thread: Triange Side Lengts to anles

1. ## Triange Side Lengts to anles

http://davidw.da.funpic.org/embed.ph...on&w=550&h=400
I was working on the math to find the angles of the inside of a triange when you only have the lengths of the sides.
I was just trying to figure it out myself and I was wondering if anyone has the precise calculations.
The link above should go to the flash file I was working with this on.
When you hit solve with the default values in place the lower left angle should equal 90.
It is close.
Anyway.. Check it out, and help me if you can.  Reply With Quote

2. You have to use sin cosin or tangent to find it out, using ratio, I cant remember it exactly.
sorry i could give you more  Reply With Quote

3. have you thought of putting 7.07 in exact form as 5 square roots of three, that would clear up the problem completely  Reply With Quote

4. Isent Sin, Cos, and tangent for right triangles only.
The formula I tryed to build myself deals with the angles of any triangle.

"have you thought of putting 7.07 in exact form as 5 square roots of three, that would clear up the problem completely"
it already is. but this is not the problem. My formula is off.  Reply With Quote

5. use the following identities and play around with them:

in any triangle with angles A, B, C and sides a, b, c opposite respectively (you have A opposite b and B opp c which is not the standard way of labelling)

a/sinA = b/sinB = c/sinC
or
sinA/a = sinB/b = sinC/c

(sinA)^2 + (cosA)^2 = 1

a^2 = b^2 + c^2 - 2bc(cosA)

remember that you already know a, b & c and you want to make 3 equations (not forms of 1 equation, completely different) and solve simultaneously to find your 3 variables A, B & C

sorry i'm really too tired to do anything (it's almost midnight) and i've been on a break since the beginning of novemember so i reckon i've become dumber  Reply With Quote

6. PHP Code:
``` a=7.07, b=5, c=5; //--------------------- function getAngle(faceSide, leftSide, rightSide) {     // draw a vertical line from vertex point of angleAB to side C. It bissect the side C into 2 segment with length of x and (c-x);     //The height of vertical line is h, since both small triangle share the common height, we get a*a-x*x=b*b-(c-x)*(c-x);     //solve the equation , we get x = (a*a+c*c-b*b)/(2*c);  We get h from x value, and get angle from Math.atan2;     var a = leftSide;     var c = rightSide;     var b = faceSide;     var x = (a*a+c*c-b*b)/(2*c);     var h = Math.sqrt(a*a-x*x);     var angleB = Math.atan2(h, x);     var aB = (180/Math.PI)*angleB;     trace(aB); } getAngle(a, b, c); getAngle(b, c, a); getAngle(c, a, b);  ```  Reply With Quote

7. i can't believe i never even though of this (too tired) but it's just using the cos rule like i showed you earlier:

a^2 = b^2 + c^2 - 2bc(cosA)

therefore:

cosA = (b^2 + c^2 - a^2)/2bc

and find the inverse cos of that and likewise for B and C  Reply With Quote

8. Ok, Thanks for all your help.
Im not going to bother with it right away, but hopefully now I have the equations needed.  Reply With Quote

9. ok, thanks i figured it out now.  Reply With Quote

10. I used that formula to calculate the angle between two 3d vectors. For example: What is the angle between the vector (2,5,6) and the vector (-4,5,-8) ?  Reply With Quote

11. Just curious, ericlin, but why not use vector algebra (i.e. dot product) for working with vectors?  Reply With Quote

12. I know how to calculate the angle between two 2d (x,y) vector (0,0)->(4,5) and (0,0)->(7,9);

But I dont know how to calculate the angle between two 3d (x,y,z) vector such as the angle between (0,0,0)->(4,5,6) and (0,0,0)->(-4,5,7);

I believe there should be some formula to do this. Anyway, I just make a triangle (0,0,0),(4,5,6),(4,5,7). then measure the length of each side (easy), and then calculate out the angle.

Hi, jonmack, could you show an example to calculate the angle between these two 3d vectors ? I am not familiar with vector algebra or dot product.  Reply With Quote

13. Hi ericlin,

It's really easy. The dot product basically projects one vector into the direction of the other. There's two methods to calculate it, one which needs the angle between the two vectors and one without. Work out the dot product using the method where you don't need to know the angle, then rearrange the other method using the previous answer to find the angle Can't put equations in here very well, so check out something like http://mathworld.wolfram.com/DotProduct.html . So equation (1) and equation (8) are equivalent.

So in code it would be something like

code:
```
// vectors A and B, each with (x,y,z) components

// equation 8
dotprod = A.x*B.x + A.y*B.y + A.z*B.z;

// norms
normA = Math.sqrt(A.x*A.x + A.y*A.y + A.z*A.z);
normB = Math.sqrt(B.x*B.x + B.y*B.y + B.z*B.z);

// equation 1
anlge = dotprod / (normA*normB);
```

then convert your angle into whatever you want.

Great thing is it can be used in 2D, 3D, 34D... whatever you want P.S. Just typed this in real fast, code should work.  Reply With Quote

14. Oh, thank you very much.

It is a bit difficult to me. I need time to digest.

Thank you again.  Reply With Quote

15. Hi jonmack,
I understand that dot product in 2d now but still have difficulties to extend the idea to 3d. I think I need more time. That idea is very good.

Anyway, if I extend the equation post by s_slosh, it matches.

cosA = (b^2 + c^2 - a^2)/2bc
cosA = (b^2 + c^2 - a^2)/(2*normB*normC);
cosA=((bx^2+by^2+bz^2)+(cx^2+cy^2+cz^2)-(bx-cx)^2-(by-cy)^2-(bz-cz)^2)/(2*normB*normC);
cosA = 2*(bx*cx+by*cy+bz*cz)/(2*normB*normC);
cosA= (bx*cx+by*cy+bz*cz)/(normB*normC);
cosA= dotProduct/(normB*normC);  Reply With Quote

16. Hi ericlin,

The beautiful thing about vectors is that they have completely arbitrary spaces and dimensions, since they are purely representational. So, although we've stuck to Cartesian space here, with x, y and z, there's no reason that they have to be those. Likewise with dimension. You can easily extend the exact same dotproduct method to work out the angle between two ninety-seven dimensional vectors if you really wanted (although I wouldn't like to try and sketch that out!) I wouldn't have a clue what that angle would represent, mindyou , but the theory should work. You would just have to sum up over more components in equation 8, and the norm calculation would have more stuff in too.

I'll be honest, I've known of, and been a fan of, your flash work for years. I would have thought you knew much about vector analysis. I guess you do learn something new everyday, and I also believe you teach something every day. Glad I could give you something interesting to think about.

Regards,  Reply With Quote

17. Thank you. I am going to take time to study vector thing and other stuff in that link you post.  Reply With Quote

18. Hi,

I was also blown away that you 'Ericlin' were not familiar with all details of vector math. I also have followed your work and been very impressed by your inuitive 3D code. I always thought that your simple and elegant use of math came from a great understanding of vector math.

I quess I was wrong ... your code is even more original and intuitive than I thought... awesome.

I really enjoy optimizing code and have spent many hours trying to apply a more conventional 3D vector approach to some of your work, I have yet to really improve on your originals.

Shipstern  Reply With Quote

19. At least I proved that, by simple concepts of sin, cos and Pythagon theory, I can go very far.

I know vector represent a thing about one point pointing to another point, and they can be added to form another vector by joining the first head and second tail.

That Math link post by jonMack is too difficult to me. However, the relationship between dot product and projection along another vector is very intersting and useful.

So, I re-write my Cube. By that formula, I can calculate a vertical axis that perpendicular to a plane defined by two vectors. I use it to rotate my cube to make any clicked face to face on us.

I am thinking about IK now. I would adopt the concepts of dot product.

I ran before, now I can fly.  Reply With Quote

20. The vector perpendicular to the plane is often called the normal and is found by calculating the cross product of 2 directional vectors on the plane. It is very usefull when backface culling and lighting 3D mesh surfaces. And also in motion and exertion of surface forces.

Here is an example code which gets the two directional vectors, finds the normal to the plane using the cross product. Then using a vector from the View Position to a point on the plane backface culls the plane using the dot product.

Code:
```//View position
var view_X=0;
var view_Y=0;
var view_Z=-700;

//Get vector from View position to point on plane
var V_X=pointA_X-view_X;
var V_Y=pointA_Y-view_Y;
var V_Z=pointA_Z-view_Z;

//Get two directional vectors from the points on the plane.
var A_X=pointA_X-pointB_X;
var A_Y=pointA_Y-pointB_Y;
var A_Z=pointA_Z-pointB_Z;

var B_X=pointA_X-pointC_X;
var B_Y=pointA_Y-pointC_Y;
var B_Z=pointA_Z-pointC_Z;

//calculate surface Normal.
var norm_X = (A_Y * B_Z) - (A_Z * B_Y);
var norm_Y = (A_Z * B_X) - (A_X * B_Z);
var norm_Z = (A_X * B_Y) - (A_Y * B_X);

/*NOTE:
you could go on and normalize this vector, find its unit length.
ie:
var l=Math.sqrt(norm_X*norm_X+Norm_Y*norm_Y+norm_Z*norm_Z);
norm_X/=l;
norm_Y/=l;
norm_Z/=l;
But that is not really needed for backface culling. But is for
lighting when you need to calculate the angle a light vector is
striking a plane.
*/

//cull face using dot product.
if(V_X*norm_X + V_Y*norm_Y+V_Z*norm_Z)>0){
//face is visible}```
Anyway hope you find it of some interest.

Shipstern  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 