So I have a triangle with the point (100, 90) the distance (11) and the angle of the line (45º) Can I find the other point of the line? How?

StackOverflow https://stackoverflow.com/questions/21941121

Вопрос

okay so I am trying to draw a triangle, the triangle can be completely random, on a canvas in JavaScript

so I got the angles and the side for triangle ABC(this is not what I'm calling it in the code)

the sides

AB(11)
AC(12)
BC(13)

the angles which are solved in a function

BAC(69)
ABC(52)
BCA(59)

And the starting Point of the triangle at (100, 90)

The question I am having is how do I find the other points to the Triangle I thought the easiest way to draw it would be to draw a line that goes to each point

So I tired the mathematics with this code (I found on another page but )

function FindTriPoints(){
//Y2 = H(Sin(A)) + Y1
//X2 = Sqrt((H^2)-(Y2^2)) + X1
pointX1 = 100;
pointY1 = 90;

pointY2 = s3 * (Math.sin(angle1*Math.PI/180)) + pointY1;
pointX2 = Math.sqrt((s3 * s3) - (pointY2 * pointY2)) + pointX1;

alert("X2 = " + pointX2 + "\n Y2 = " + pointY2)
}

but X2 ends up becoming NaN because it is a negative value that it is trying to square root.

Edit Thanks to Cbroe and Jing3142 for helping me with Y2

Это было полезно?

Решение

well if you know valid triangle sides lengths (l1,l2,l3) and their angles (a,b,c) ...

  • then it is quite simple with vector math ...

triangle

// compute directions
a1=0;
a2=180-b;
a3=a2+180-c;
a3=-b-c;
a3=-a;
// convert them from [deg] to [rad]
a1*=Math.pi/180.0;
a2*=Math.pi/180.0;
a3*=Math.pi/180.0;
// compute points
A=(x0,y0); // your start point is known
B=A+l1*dir(a0)=(x0+l1*Math.cos(a0),y0+l1*Math.sin(a0));
B=A+l1*dir( 0)=(x0+l1               ,y0                 );     // a0 is always zero
C=A-l3*dir(a3)=(x0-l3*Math.cos(a3),y0-l3*Math.sin(a3));    // C from A point
C=B+l2*dir(a2)=(x0+l1+l2*Math.cos(a2),y0+l2*Math.sin(a2)); // C from B point

[notes]

  • as you can see there are more alternatives for some variables choose one you like
  • do not forget to check if l1+l2>l3 and l1+l3>l2 and l2+l3>l1
  • if not then your lengths are not valid triangle sides
  • also a+b+c = 180
  • if not then your angle computation is wrong
  • if you want to over-check the triangle then compute C from A and from B point
  • if they are not the same (their distance > some accuracy constant like 0.001)
  • then it is not a valid triangle
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top