0

I need to draw a line at 90° to an existing line, I know the 2 points of the line and that the new line should be the same length as the existing (essentially a 45-45-90 triangle) How do I work this out in a JavaScript context?

45-45-90 Triangle

I know the coordinates of A and B, then length of a (and b as it is the same as a) and need to find C I know there is going to be 2 different place this point could be, but for my purpose it does not matter which, as long as it makes a valid 45-45-90 from the base line.

for example: A=5,2 B=3,7, what is C?

For the life of me I cannot remember the trigonometry I learned in high school (this is the first time I have needed it, 15 years later) and all of the math sites I have visited have just confused me more. most assume you want to know the length of the long side.

mike16889
  • 322
  • 3
  • 11
  • If you just need some hints with code to work it out, [How to create a curved SVG path between two points?](https://stackoverflow.com/a/49286885/1115360) has the bits you need. – Andrew Morton Aug 13 '20 at 15:38
  • @AndrewMorton thanks for the response. I was about to respond that i don't see the relavence, but I think I may be able to use it. Will have to try in the morning as my brain is about fried tonight. – mike16889 Aug 13 '20 at 15:49

1 Answers1

1

It is in fact not a complicated formula: the difference in x you have between A and B, becomes the difference in y between A and C. The same happens with the difference in y. Just pay attention to the signs:

let a=[5,2];
let b=[3,7];

// The vector from A to B:
let dist = [b[0]-a[0], b[1]-a[1]];

// Using that to get two variants of C:
let c1 = [a[0]+dist[1], a[1]-dist[0]];
let c2 = [a[0]-dist[1], a[1]+dist[0]];

console.log(c1);
console.log(c2);
trincot
  • 317,000
  • 35
  • 244
  • 286