Pintac
Pintac

Reputation: 1585

Trig triangles angle

i have a start point and a end point. I want to work out the angle i have this formula that seems to work

double dx = end.X - start.X;
double dy = end.Y - start.Y;

double degrees = Math.Acos((-Math.Pow(dy, 2) + Math.Pow(dx, 2) + Math.Pow(dx, 2)) / (2 * Math.Pow(dx, 2)));
degrees = degrees * 180 / Math.PI;

Then i want to take the angle and extend the line's length. i have this so far

end.Y = (start.Y + (len * Math.Sin(angle)));
end.X = (start.X + (len * Math.Cos(angle)));

now this does not give me the right value.

white is original line and red is the extending

white is original line and red is the extending

what am i doing wro

Upvotes: 0

Views: 859

Answers (3)

mrjames
mrjames

Reputation: 232

This is what I meant in code:

double dx = end.X - start.X;
double dy = end.Y - start.Y;

double dlen = Math.Sqrt(dx * dx + dy * dy);

dx = dx / dlen;
dy = dy / dlen;

end.X = start.X + (dx * len);
end.Y = start.Y + (dy * len);

Upvotes: 1

timh
timh

Reputation: 371

if you don't have the hypotenuse (which you don't need) you should use a tangent trig function Like

  double rads = Math.Atan(dy/dx);

Your degree calc is quite convoluted although my way involves keeping track of quadrants. See: http://www.mathwizz.com/algebra/help/help29.htm

Upvotes: 0

Guillaume
Guillaume

Reputation: 1

If you just want to continue your line, first you'll have to find the function which defines your line.

this is a "simple" line ... it's function is f(x)=ax+b. Find a and b.

To find a :

a = (start.y - end.y) / (start.x - end.x)
// easy, isn't it ?

To find b :

b = (start.y) - (a * start.x)
// you can check switching "start" by "end"

No deal with angles, cosinus or sinus ...

Bye

Upvotes: 0

Related Questions