Click to See Complete Forum and Search --> : Angles?
gamer150
July 6th, 2007, 10:42 AM
if you have three points that form an angle is there a way to find that angle?
if you have two points that form a line, is there a way to find a third point that would make a given angle?
Zachm
July 6th, 2007, 12:10 PM
Assume that the 3 points are p1, p2 and p3, and you want to find the angle that is adjacent to point p2.
The vector v1 = p1-p2
The vector v2 = p3-p2
The angle is equal to acos( (v1*v2) / |v1||v2|).
where v1*v2 = v1.x*v2.x + v1.y*v2.y (this is for 2D, for 3D, add the Z)
and |v1| is the length of the vector v1 (calculated by sqrt(v1.x^2 + v1.y^2) ).
Note that there are always 2 angles - internal and external, one is smaller or equal to 90 degrees and the other is equal or bigger than 90 degrees.
Usually the internal angle is wanted, and a check should be done to see if the angle is bigger than 90.
An example code will be something like:
#include <cmath>
#include <iostream>
using namespace std;
struct Point2D
{
float x;
float y;
};
float FindAngleInDegrees(Point2D p1, Point2D p2, Point2D p3)
{
Point2D v1, v2;
float len1, len2;
//v1 = p1-p2
v1.x = p1.x - p2.x;
v1.y = p1.y - p2.y;
//v2 = p3-p2
v2.x = p3.x - p2.x;
v2.y = p3.y - p2.y;
//compute vector lengths
len1 = sqrt(v1.x * v1.x + v1.y * v1.y);
len2 = sqrt(v2.x * v2.x + v2.y * v2.y);
float angleInRads = acos((v1.x * v2.x + v1.y * v2.y) / (len1 * len2));
float angleInDegs = angleInRads * 180.0f / 3.141f;
//find the internal angle
if (angleInDegs > 90.0f)
angleInDegs = 180 - angleInDegs;
return angleInDegs;
}
int main()
{
Point2D p1, p2, p3;
p1.x = 10;
p1.y = 10;
p2.x = 0;
p2.y = 0;
p3.x = 10;
p3.y = 0;
cout << "The angle is " << FindAngleInDegrees(p1,p2,p3) << " degrees.\n";
return 0;
}
As for your second question - yes it is possible - by creating a vector from the 2 points and then rotating it by the wanted angle and adding it to the pivot point. To rotate a vector, you must multiply it by an appropriate rotation matrix (http://mathworld.wolfram.com/RotationMatrix.html).
You can read more about it.
gamer150
July 6th, 2007, 05:30 PM
Thanks
Three276Seven
July 6th, 2007, 09:15 PM
You can learn a lot more about this stuff by learning trigonometry. Law of sines, cosines, etc.
gamer150
July 8th, 2007, 10:37 AM
I'm only 13
Zachm
July 8th, 2007, 11:26 AM
I'm only 13
So ? You can probably self tutor yourself into these stuff quicker than most adults. You can always use this forum / other forums to ask questions.
I was about your age when I coded my 1st 3D application using nothing more that sine / cosine functions - believe me that I didn't learn those stuff in school by that time. I'm not trying to brag about my skills, rather to convince you that you shouldn't let your age be an obstacle.
Learn what functions are, then go on to basic trigonometry, then to vectors and matrices.
All you need to know is just a short google search away.
Best of luck,
Zachm
gamer150
July 9th, 2007, 11:14 AM
just after I posted the questions I realised I could use sines and cosines to answer the second question
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.