@Karyudo: If I had published a page that includes this processing, I would have linked to it. For line intersection, Fred gives a link to the maths. Implementing that in a script depends on the script language, and how the data is organised, and what error-checking is needed.

For example, lineIntersect.bat (a Windows BAT script) finds the intersection of two lines:

Code: Select all

```
rem Given %1 and %2 are two lines
rem returns coordinates of the intersection (if any) of the lines in %3.
rem All may be floating-point.
rem %3 environment variable for output.
rem %4 Fractional threshold for BAD. [0.0 or above, eg 0.25]
rem Larger numbers are more tolerant.
rem If either line needs to be extended by proportional fraction %4 to intersect,
rem it is marked as bad.
rem %4 can be zero, meaning all intersections that need extending are marked bad.
rem If blank or ".", no intersections are marked bad.
@rem This assumes "enabledelayedexpansion" is in effect.
set FRAC_THRESH=%4
if "%FRAC_THRESH%"=="." set FRAC_THRESH=
call :vect AB %1.A %1.B
call :vect CD %2.A %2.B
call :vect CA %2.A %1.A
call :perp Dp AB CD
if %Dp%==0 exit /B 1
call :perp sp CD CA
call :perp tp AB CA
call :doCalc sI %sp%/(%Dp%)
call :doCalc tI %tp%/(%Dp%)
call :doCalc %3.x !%1.A.x!+%sI%*%AB.x%
if ERRORLEVEL 1 exit /B 1
call :doCalc %3.y !%1.A.y!+%sI%*%AB.y%
if ERRORLEVEL 1 exit /B 1
call :doCalc %3.liNeedExtend "(%sI%<0||%sI%>1||%tI%<0||%tI%>1)?1:0"
if ERRORLEVEL 1 exit /B 1
set %3.sI=%sI%
set %3.tI=%tI%
if not "%FRAC_THRESH%"=="" if !%3.liNeedExtend!==1 (
call :doCalc siBAD "(%sI%<(-%FRAC_THRESH%)||%sI%>(1+%FRAC_THRESH%))?1:0"
if ERRORLEVEL 1 exit /B 1
call :doCalc tiBAD "(%tI%<(-%FRAC_THRESH%)||%tI%>(1+%FRAC_THRESH%))?1:0"
if ERRORLEVEL 1 exit /B 1
if !siBAD!==1 set %3.BAD=1
if !tiBAD!==1 set %3.BAD=1
)
echo %0: sI=%sI% tI=%tI% %3.liNeedExtend=!%3.liNeedExtend!
exit /B 0
rem -----------------------------------------------------
rem Subroutines
:vect
call :doCalc %1.x !%3.x!-!%2.x!
call :doCalc %1.y !%3.y!-!%2.y!
exit /B 0
:perp
call :doCalc %1 !%2.x!*!%3.y!-!%2.y!*!%3.x!
exit /B 0
:doCalc
set %1=
for /F "usebackq" %%L in (`%IM%identify -precision 19 -format "%1=%%[fx:%~2]" xc:`) do set %%L
rem echo %1 is !%1!
if "!%1!"=="" exit /B 1
exit /B 0
```

It can be called like this:

Code: Select all

```
set Line1.A.x=0
set Line1.A.y=0
set Line1.B.x=10
set Line1.B.y=0
set Line2.A.x=4.5
set Line2.A.y=-1
set Line2.B.x=7.5
set Line2.B.y=1
call %PICTBAT%lineIntersect Line1 Line2 OutLine
if ERRORLEVEL 1 echo Bad result
set OutLine
```

Code: Select all

```
OutLine.liNeedExtend=0
OutLine.sI=0.59999999999999998
OutLine.tI=0.5
OutLine.x=6
OutLine.y=0
```

This tells us the intersection is at (6,0), which is 0.5999 along the first line and 0.5 along the second line.