Okay, here's some code I wrote...
I might mention a few potential problems.
First of all, this uses a mathematical formula, so the display may look different to you than to the formula, therefore creating visual problems, two of which I have noticed:
1. horizontal and vertical lines don't work. I made my code so that no horizontal or vertical lines are generated. But if you use your own lines that or horizontal or vertical, there may be problems, I'm not sure.
2. lines very close to another line may not intersect, but they may have one pixel right next to another, so the lines may touch. No lines will cross, though.
Another note for those who don't want to read through the source code - the code works by storing every drawn line into an array. Therefore, if you want it to work with existing lines, you will have to store the coordinates of those lines into the array variable. I would recommend writing a function that draws lines with the LINE command, and also stores the coordinates for the LINE command into a DIM SHARED array.
Anyone interested in the actual formula for line intersection can find it in the source code. The x intersection coordinate is top1/bottom1 and the y intersection coordinate is top2/bottom2.
'Intersecting Lines
'By Martin Hunt
'The screenmode variable can be changed between 12 and 13
'12 = high res, low color
'13 = low res, high color
'--------------
screenmode = 12
'--------------
'The screenmode variable sets the maxX, maxY, minX, minY variables
'along with a maxC variable, standing for the maximum color number
SCREEN screenmode
IF screenmode = 12 THEN
maxX = 640: maxY = 480
minX = 0: minY = 0
maxC = 16
END IF
IF screenmode = 13 THEN
maxX = 320: maxY = 200
minX = 0: minY = 0
maxC = 256
END IF
RANDOMIZE TIMER
CLS
TYPE linearray
x AS INTEGER
y AS INTEGER
x2 AS INTEGER
y2 AS INTEGER
END TYPE
maxlines = 1
DIM lines(maxlines) AS linearray
'this holds the coordinates of every line drawn
'create an initial line segment
DO
lines(1).x = INT(RND * (maxX - minX) + minX)
lines(1).x2 = INT(RND * (maxX - minX) + minX)
lines(1).y = INT(RND * (maxY - minY) + minY)
lines(1).y2 = INT(RND * (maxY - minY) + minY)
LOOP WHILE lines(1).x = lines(1).x2 OR lines(1).y = lines(1).y2
'the formula does not work for vertical or horizontal lines
'so loop until the line is not vertical or horizontal
'Draw the first line
LINE (lines(1).x, lines(1).y)-(lines(1).x2, lines(1).y2), INT(RND * maxC)
DO
DO
'Generate the coordinates of a random line
DO
x1 = INT(RND * (maxX - minX) + minX)
x2 = INT(RND * (maxX - minX) + minX)
y1 = INT(RND * (maxY - minY) + minY)
y2 = INT(RND * (maxY - minY) + minY)
LOOP WHILE x1 = x2 OR y1 = y2
'make sure the line is not vertical or horizontal
intersect = 0
i = 1
DO
'test random line against every
'other line generated so far
'copy a line from the array into x3, y3 and x4, y4
x3 = lines(i).x: x4 = lines(i).x2
y3 = lines(i).y: y4 = lines(i).y2
top1 = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)
bottom1 = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
top2 = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)
'cannot divide by 0, so check if bottom1 <> 0
IF bottom1 <> 0 THEN
i1 = top1 / bottom1
j1 = top2 / bottom1
'x and y coordinates of intersection
xMax = x3: xMin = x4: IF x4 > x3 THEN xMax = x4: xMin = x3
yMax = y3: yMin = y4: IF y4 > y3 THEN yMax = y4: yMin = y3
xMax2 = x1: xMin2 = x2: IF x2 > x1 THEN xMax2 = x2: xMin2 = x1
yMax2 = y1: yMin2 = y2: IF y2 > y1 THEN yMax2 = y2: yMin2 = y1
'find max and min of the two line segments
IF i1 >= xMax OR i1 <= xMin OR j1 >= yMax OR j1 <= yMin OR i1 >= xMax2 OR i1 <= xMin2 OR j1 >= yMax2 OR j1 <= yMin2 THEN bottom1 = 0
'determine if the intersection coordinates are
'on both line segments based on max and min
'of both line segments
'If the interesection coordinates are outside of the
'line segments, then the two lines do not intersect
END IF
IF bottom1 <> 0 THEN intersect = 1
i = i + 1
'increase i and loop to test the new line
'for intersection with another existing line
LOOP WHILE i <= maxlines AND intersect <> 1
'break the loop of testing if the line intersects
'or if there are no more lines to test it against
LOOP WHILE intersect = 1
'If one of the lines intersected
'then it loops again, generating a new random line
'If the new line does not intersect anywhere, draw it and store it:
LINE (x1, y1)-(x2, y2), INT(RND * maxC)
maxlines = maxlines + 1
REDIM PRESERVE lines(maxlines) AS linearray
'increase size of line array by one
lines(maxlines).x = x1
lines(maxlines).y = y1
lines(maxlines).x2 = x2
lines(maxlines).y2 = y2
'the mathematically correct values are stored for comparisons
LOOP WHILE INKEY$ <> CHR$(27)
'loop until escape is pressed
SYSTEM