So i check of the from/to x/y values are equal and add a small amount to one if they are
// Project: intersect
// Created: 2019-03-25
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "intersect" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
type point
x as float
y as float
endtype
type line
f as point
t as point
endtype
l1 as line
l2 as line
l1.f.x = GetWindowWidth() * 0.1
l1.t.x = GetWindowWidth() * 0.9
l1.f.y = GetWindowHeight() * 0.5
l1.t.y = GetWindowHeight() * 0.5
l2.f.x = GetWindowWidth() * 0.5
l2.t.x = GetWindowWidth() * 0.5
l2.f.y = GetWindowHeight() * 0.1
l2.t.y = GetWindowHeight() * 0.9
i as point
id = LoadImage("dot.png")
l1f = CreateSprite(id)
SetSpritePositionByOffset(l1f, GetWindowWidth() * 0.1, GetWindowHeight() * 0.5)
SetSpriteColor(l1f, 0, 0xff, 0, 0xff)
l1t = CreateSprite(id)
SetSpritePositionByOffset(l1t, GetWindowWidth() * 0.9, GetWindowHeight() * 0.5)
SetSpriteColor(l1t, 0, 0xff, 0, 0xff)
id = LoadImage("dot.png")
l2f = CreateSprite(id)
SetSpritePositionByOffset(l2f, GetWindowWidth() * 0.5, GetWindowHeight() * 0.1)
SetSpriteColor(l2f, 0, 0xff, 0, 0xff)
l2t = CreateSprite(id)
SetSpritePositionByOffset(l2t, GetWindowWidth() * 0.5, GetWindowHeight() * 0.9)
SetSpriteColor(l2t, 0, 0xff, 0, 0xff)
global DragSprite as integer
do
CheckHandles()
l1.f.x = GetSpriteXByOffset(l1f) : l1.f.y = GetSpriteYByOffset(l1f)
l1.t.x = GetSpriteXByOffset(l1t) : l1.t.y = GetSpriteYByOffset(l1t)
l2.f.x = GetSpriteXByOffset(l2f) : l2.f.y = GetSpriteYByOffset(l2f)
l2.t.x = GetSpriteXByOffset(l2t) : l2.t.y = GetSpriteYByOffset(l2t)
drawline(l1.f.x, l1.f.y, l1.t.x, l1.t.y, 0x808080, 0x808080)
drawline(l2.f.x, l2.f.y, l2.t.x, l2.t.y, 0x808080, 0x808080)
if intersection(l1, l2, i)
DrawEllipse(i.x, i.y, 8, 8, 0x0000ff, 0x0000ff, 1)
else
print("No intersect")
endif
Print( ScreenFPS() )
Sync()
loop
function CheckHandles()
id as integer
if GetPointerPressed()
DragSprite = GetSpriteHit(GetPointerX(), GetPointerY())
endif
if GetPointerReleased()
DragSprite = 0
endif
if GetPointerState()
if DragSprite
SetSpritePositionByOffset(DragSprite, GetPointerX(), GetPointerY())
endif
endif
endfunction
function intersection(l1 as line, l2 as line, p ref as point)
b as float : a as float
a2 as float : b2 as float
xi as float : yi as float
t as float
if l1.f.x = l1.t.x
inc l1.t.x, 0.0001
endif
if l1.f.y = l1.t.y
inc l1.t.y, 0.0001
endif
if l2.f.x = l2.t.x
inc l2.t.x, 0.0001
endif
if l2.f.y = l2.t.y
inc l2.t.y, 0.0001
endif
if l1.f.x>l1.t.x
t=l1.t.x : l1.t.x=l1.f.x : l1.f.x=t
t=l1.t.y : l1.t.y=l1.f.y : l1.f.y=t
endif
if l2.f.x>l2.t.x
t=l2.t.x : l2.t.x=l2.f.x : l2.f.x=t
t=l2.t.y : l2.t.y=l2.f.y : l2.f.y=t
endif
intersect = 1
b = (l1.t.y - l1.f.y) / (l1.t.x - l1.f.x)
a = l1.f.y - ( b * l1.f.x )
b2 = (l2.t.y - l2.f.y) / (l2.t.x - l2.f.x)
a2 = l2.t.y - ( b2 * l2.t.x )
xi = -(a - a2) / (b - b2)
yi = a + b * xi
p.x = xi
p.y = yi
if xi>l1.t.x
exitfunction 0
endif
if xi>l2.t.x
exitfunction 0
endif
if xi<l1.f.x
exitfunction 0
endif
if xi<l2.f.x
exitfunction 0
endif
endfunction intersect
This generally goes hand in hand so here is some code to scale a line up or down
function ScaleLine(fx as float, fy as float, tx as float, ty as float, magnitude as float)
l as float
slope as point
line as line
slope.x = tx - fx
slope.y = ty - fy
l = sqrt(slope.x * slope.x + slope.y * slope.y)
line.f.x = fx - slope.x / l * magnitude
line.f.y = fy - slope.y / l * magnitude
line.t.x = tx + slope.x / l * magnitude
line.t.y = ty + slope.y / l * magnitude
endfunction line