faster
This commit is contained in:
parent
cddfe43315
commit
ca2c925d5f
1 changed files with 9 additions and 10 deletions
|
@ -15,17 +15,16 @@ proc segment*(at, to: Vec2): Segment =
|
||||||
proc intersects*(a, b: Segment, at: var Vec2): bool =
|
proc intersects*(a, b: Segment, at: var Vec2): bool =
|
||||||
## Checks if the a segment intersects b segment.
|
## Checks if the a segment intersects b segment.
|
||||||
## If it returns true, at will have point of intersection
|
## If it returns true, at will have point of intersection
|
||||||
var s1x, s1y, s2x, s2y: float32
|
let
|
||||||
s1x = a.to.x - a.at.x
|
s1x = a.to.x - a.at.x
|
||||||
s1y = a.to.y - a.at.y
|
s1y = a.to.y - a.at.y
|
||||||
s2x = b.to.x - b.at.x
|
s2x = b.to.x - b.at.x
|
||||||
s2y = b.to.y - b.at.y
|
s2y = b.to.y - b.at.y
|
||||||
|
|
||||||
var s, t: float32
|
let
|
||||||
s = (-s1y * (a.at.x - b.at.x) + s1x * (a.at.y - b.at.y)) /
|
denominator = (-s2x * s1y + s1x * s2y)
|
||||||
(-s2x * s1y + s1x * s2y)
|
s = (-s1y * (a.at.x - b.at.x) + s1x * (a.at.y - b.at.y)) / denominator
|
||||||
t = (s2x * (a.at.y - b.at.y) - s2y * (a.at.x - b.at.x)) /
|
t = (s2x * (a.at.y - b.at.y) - s2y * (a.at.x - b.at.x)) / denominator
|
||||||
(-s2x * s1y + s1x * s2y)
|
|
||||||
|
|
||||||
if s >= 0 and s < 1 and t >= 0 and t < 1:
|
if s >= 0 and s < 1 and t >= 0 and t < 1:
|
||||||
at.x = a.at.x + (t * s1x)
|
at.x = a.at.x + (t * s1x)
|
||||||
|
|
Loading…
Reference in a new issue