I’m using sylvester.js for a math framework, but seem to have either forgotten geometry (likely), or am not using it correct. I’m trying:
var angle = 45;
var line1 = $L([1,1],[Math.tan(angle * Math.PI/180),Math.tan(angle * Math.PI/180)]); //Should be a 45-degree angle
angle = -45;
var line2 = $L([0,0],[Math.tan(angle * Math.PI/180),Math.tan(angle * Math.PI/180)]); //Should be a -45-degree angle
line1.intersects(line2);
//returns false, should be true, I would think?
This should create a line through 0,0 at -45 degrees, and another through 1,1 at 45 degrees that intersect, right? Any suggestions? (Note: I usually use a deg2rad var instead of Math.PI/180, this is purely for example sake)
Recall that the tangent of an angle is equal to the length of the opposite leg over the adjacent leg. In other words, rise over run. Thus, the tangent gives you the slope of the line, not the vector along the line.
To convert an angle to a vector, use sine and cosine:
You see this in the formula to convert from polar to Cartesian coordinates:
Since the magnitude doesn’t matter (even if you had a magnitude,
$Lnormalizes the vector), leave the “r” out of the equation and you get the equation for m⃑ above.Note that for each direction vector in your code, the two components are equal, so the vectors lie on the line y=x; thus they are parallel. You can also show the vectors are parallel because the vectors are scalar multiples of each other. More specifically, since the vectors have opposite direction (the scalar is negative), they are antiparallel. The reference points (0,0) and (1,1) lie on both lines, so the lines spanned by each vector overlap. Sylvester.js’
Line.intersectsreturns true only if there’s a unique intersection point. Since the two lines intersect everywhere, there’s no unique point, and the function returns false.