I have a table element with numbers from 1 to 100, and I want to programmatically add lines between any two cells using CSS. In order to do that, I created an absolute-positioned div. Now I should use the offsets of the cells to calculate the position, the height and the rotation degree for that div.
Let's make s
the "start point" cell's offset and e
the "end point" cell's offset.
- I figured that the div's offset should be like
s
's. - Based on the Pythagorean theorem, I figured that the height should be
Math.sqrt( Math.pow(s.top-e.top, 2) + Math.pow(s.left-e.left, 2) )
. - Using trigonometry, I figured that the rotation degree should be
-Math.atan2(s.top-e.top, s.left-e.left)*180/Math.PI
.
Some (or all) of these are probably wrong, because I tried them and it doesn't seem to produce the correct line. Here's a fiddle for demonstration. Can anyone correct me so that this algorythm will produce the correct line?
Thanks!