get an angle from two points

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I need to get the angle of a right triangle, so I can extrapulate the points between them. I know how to extrapulate the points once I have the angle, however I do not know how to get it. I have 2 sets of points(x,y). How could I get the angle from these?

Share on other sites
I may be misunderstanding you, but you cannot get an angle from two points; you can just get a line. Also you say you need the angle of a right triangle, but that's always 90 degrees!

Maybe you can clarify what you mean?

Share on other sites
With only two sets of points you have no triangle. Two points only define a line, for which there is no angle. If you know all three points of the triangle then this becomes a relatively simple problem with a little bit of vector math.

Let's say you have three points of a triangle, A, B, and C. You can find a vector between two sets of these points. So lets say you get the vector from point A to point B and then another vector from Point B to Point C. The vector from A to C should then just be the first vector plus the second one. Now that you have three vectors defining your triangle you can do dot product operations on any two of them and that will tell you the angle between them (more correctly, it will tell you about the difference in the direction that they point, but the angle can be extracted with A.B = ||A||*||B|| cos(a)

Share on other sites
Ok, lets try another approach, I guess (I could get the 3rd point, but this might be easier). I need to be able to figure out the points along a line at X intervals. I know where the starting and ending point of a line is. So how could I get the points in the line thats lets say 10u long and a new point will be drawn every .7. How would I be able to get the x,y cords for each point that id have to draw. The remaining space that wouldnt be used would just be ignored.

Share on other sites
Point 1: (0, 0)
Point 2: (10, 10)

What is the halfway point?
(5, 5)

What is .75?
(7.5, 7.5)

Think you can figure it out from here?

Take the difference between the 2 points and multiply it by the fraction you want to move and add it to the 1st point.

Share on other sites
I do not see how this would work because in a hyp triange, which is pretty much a line(the hypontnuse) which is what I want to move along .7. However, I do not know what that would translate to be the difference in the x,y cords. Or am I totally mis-understanding you. I know exactly how many points id have to add but using pythagoreans theorm. However I do not know the difference of the x,y values.

Share on other sites
let r be the distance you want to travel.

x3 = ((x2 - x1) * r) + x1;
y3 = ((y2 - y1) * r) + y1;

Share on other sites
Quote:
 Original post by starfleetrpI do not see how this would work because in a hyp triange, which is pretty much a line(the hypontnuse) which is what I want to move along...

If you only want to move along the hypotenuse, you can, I would suppose, effectively ignore the two other legs of the triangle. So if you want to move in increments of .7, just find the points that are .7 units away from the first point. I'm not going to be redundant and reiterate the math, just trying to help you understand the concept. Hope it helps.

-AJ

EDIT: I think tstrimp's latest explanation sums it up pretty nicely.

Share on other sites
Well I didnt think this would work, and it didnt. My result is a line of dots that procedes off of the screne, even though the line has a hyponeuse of 11u and there should be a dot at .7u incriments. And I know that there should then by dist/inc or 11/.7 = 15.71 lines (how would I round down) so 15 dots. So how would I calculate the distance? Since I am doing something wrong

so
offset = (x1-x2)*dist*inc
(11)*.7*1 = 7.7
...
(11)*.7*15 = 115.5

however I should have only gotten
.7 1.4 2.1 etc. as the final offset

I do not know of anyway to fix this. Well atleast I can not think of any with the forumal I have been given. I have tried this on paper a few times and just could not think of anyting.

Share on other sites
In the formula I provided, r is the distance as a fraction of the magnitude of the vector. The distance you are moving is 70% of 11u or 7.7u per jump. Since you only want to move .7u, you need to figure out what percentage .7u is of 11u. .7 / 11 = 0.0636363. So in your case the formula should use 0.0636363 or 0.7/11.0 as r.

Share on other sites
int xcurrent = x1;int ycurrent = y1;int x;int y;while (xcurrent <= x2 && ycurrent <= y2){    drawPoint(x, y);    x = ((x2 - xcurrent) * r) + xcurrent;    y = ((y2 - ycurrent) * r) + ycurrent;    xcurrent = x;    ycurrent = y;}

That will limit the drawing of points to only be within the bounds of your line.

I have to go in a few minutes, but I'm sure tstrimp or anyone else could explain that for you. I hope it's right. If not, I'm sure tstrimp or anyone else could fix it for me [smile]. Hope it helps.

-AJ

Share on other sites
You only need to recalculate the move distance if the vector changes or the distance you want to move changes.

float x1, y1, x2, y2, x3, y3;//  Assign x1, y1, x2, y2x3 = ((x2 - x1) * r);y3 = ((y2 - y1) * r);float x = x1;float y = y1;while(something){  DrawPoint(x, y);  x += x3;  y += y3;}

Share on other sites
Yes, that makes perfect sence. Thank-you. I somehow put a /11 on the paper, but I forgot to do it in my code and post.... I feel so stupid. Thanks so much, you have been extrememly helpful! I will try this out!!!

Share on other sites
Well there seem to still be a few problems... the lines seem to always be going at an angle of about 25ish (if 0 is up). No matter what direction I am moving in eg. last point(4,4) new point (2,2) so im moving down but the line is going up. Ill post my current code:

spaceInterval(used to be .7) is set at 5.0f now
//xPos and yPos are the varialbes/cords I am passing in//I hope this kinda makes sence....		Particle particle;		particle.LifeTime = 0.0f;		if(continueTrail)		{			//Get the distance between current particle and the last one			distance = sqrt((xPos - lastX)*(xPos - lastX) + (yPos - lastY)*(yPos - lastY));  //Pythagoreans theorm			if(distance >= spaceInterval)				addCount = distance / spaceInterval;			if(distance == 0)				distance = 1;			//Get the point offset/change			xChange = (xPos - lastX) * (spaceInterval / distance);			yChange = (yPos - lastY) * (spaceInterval / distance);			for(int offset = 0; offset <= addCount; offset++)			{				x = xChange * offset;				y = yChange * offset;				particle.xPos = x + xPos;				particle.yPos = y + yPos;				Particles.push_back(particle);			}		}		else		{			//We just want to add one particle			x = xPos;			y = yPos;			particle.xPos = x;			particle.yPos = y;			Particles.push_back(particle);		}	//}	lastX = x;	lastY = y;...

Edit: I fixed the problem.

[Edited by - starfleetrp on April 20, 2006 10:45:01 PM]

Share on other sites
It isn't most likely isn't the cause of your problems but shouldn't this loop:
			for(int offset = 0; offset <= addCount; offset++)			{				x = xChange * offset;				y = yChange * offset;				particle.xPos = x + xPos;				particle.yPos = y + yPos;				Particles.push_back(particle);			}

be

			for(int offset = 0; offset <= addCount; offset++)			{				particle.xPos = xChange * offset + xPos;				particle.yPos = yChange * offset + yPos;				Particles.push_back(particle);			}

Since once you're done you're setting lastX to x and lastY to y. It doesn't look like the problem is in this little section though. Would it be possible to get some more of the source?

Share on other sites
Well, angle of a line is just its gradient isnt it?
y = y2-y1 x = x2-x1

angle = atan2(y,x) ?? :/

Share on other sites
I fixed the problem, it kept calculating from the cords 0,0 instead of its last position.