A Simple Discrete-Event Simulation: Part 89

Direct link for mobile devices.

I was walking a few miles in the nice weather this evening when two possible solutions popped into my head for the DisplayGroup drag problem I had in the simulation app (where the pointer arrow from the edge of the DisplayGroup object to the edge of the DisplayItem object does not get drawn if the intersection tests generate false negatives). As long as the idea was fresh I figured I’d go ahead and check it out. It turned out that I already do one of things that occurred to me so that quickly proved to be a dead end. The other idea appears to solve the problem for all the cases I’ve tested.

The problem I found with the method I originally implemented solved for the intersection of two lines using the slope and y-intercept formula. The problem with that is that some values for the y-intercept can be quite high which can lead to rounding errors at the intersection point. If I write formulas for the lines in terms of one of the endpoints of each line then the formulas get slightly more complex but the accuracy of calculating the intersection point should increase.

The point-slope formula for a line is:

    yp = y1 + m12 * (xp – x1)


    xp, yp = location of arbitrary point on line
    m12 = slope of line, (y2 – y1) / (x2 – x1)
    x1, y1 = location of line endpoint 1
    x2, y2 = location of line endpoint 2

Substituting for the yp we look to solve for xp for the intersection of lines 12 and 34

    y1 + m12 * (xp – x1) = y3 + m34 * (xp – x3)

    y1 + m12 * xp – m12 * x1 = y3 + m34 * xp – m34 * x3

    m12 * xp – m34 * xp = y3 – y1 + m12 * x1 – m34 * x3

    xp * (m12 – m34) = y3 – y1 + m12 * x1 – m34 * x3

    xp = (y3 – y1 + m12 * x1 – m34 * x3) / (m12 – m34)

Once you have the x-coordinate then you get the y-coordinate by reinvoking the first formula. The code has to do some testing for special cases involving vertical and horizontal lines as shown in the new intersection function here:

Click or tap on any of the non-path objects to make the associated DisplayGroup object visible, then drag that object around the screen with the mouse or by touch. The pointer appears to render smoothly in all cases.

This entry was posted in Software and tagged , , , , , . Bookmark the permalink.

Leave a Reply