I have a problem where I draw SVG lines on each click, what I need to do is draw only horizontal/vertical lines (90deg) or 45deg. line. The horizontal/vertical problem I already solved, where I'm stuck is drawing 45deg. line if I know the following information: startCoordX, startCoordY, endCoordX, endCoordY, angle(either positive 45deg or negative -45deg. Basically I need to just tweak the endCoordinates to make them form a +-45deg. angle line with the starting coordinates. So far I am calculating the angle between the two points like this:
angle(startx, starty, endx, endy) {
var dy = endy - starty;
var dx = endx - startx;
var theta = Math.atan2(dy, dx); // range (-PI, PI]
theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
//if (theta < 0) theta = 360 + theta; // range [0, 360)
return Math.abs(theta) > 90 ? theta % 90 : theta;
}
Any ideas how can I achieve this? I need another function that will return me the ending X and Y coordinates in order to draw the line...