It should be simple enough to add a dot to your draw function:
if(intersectionX && intersectionY) { svg.append("circle") .attr("class", "intersection") .attr("fill", "red") .attr("cx", intersectionX) .attr("cy", intersectionY); } else { svg.selectAll(".intersection").remove(); }
The next task is to calculate the intersection of your two points, which are actually a mathematical problem, not a coding problem. I’ll point you to a couple of reading links, since StackOverflow is not intended for formatting math, but the main steps are:
- Find the equation of both lines
- Set
x to the same value in both equations - Solve the equation for
y - for example. both equations must be equal to each other
Calculate the intersection of two lines
Calculate the equation of lines
JSFiddle example
Ian
source share