commit
c6eb92d923
1 changed files with 8 additions and 13 deletions
|
@ -8,7 +8,7 @@ type
|
||||||
at*: Vec2
|
at*: Vec2
|
||||||
to*: Vec2
|
to*: Vec2
|
||||||
|
|
||||||
proc segment*(at, to: Vec2): Segment =
|
proc segment*(at, to: Vec2): Segment {.inline.} =
|
||||||
result.at = at
|
result.at = at
|
||||||
result.to = to
|
result.to = to
|
||||||
|
|
||||||
|
@ -16,23 +16,18 @@ proc intersects*(a, b: Segment, at: var Vec2): bool {.inline.} =
|
||||||
## 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
|
||||||
let
|
let
|
||||||
s1x = a.to.x - a.at.x
|
s1 = a.to - a.at
|
||||||
s1y = a.to.y - a.at.y
|
s2 = b.to - b.at
|
||||||
s2x = b.to.x - b.at.x
|
denominator = (-s2.x * s1.y + s1.x * s2.y)
|
||||||
s2y = b.to.y - b.at.y
|
s = (-s1.y * (a.at.x - b.at.x) + s1.x * (a.at.y - b.at.y)) / denominator
|
||||||
|
t = (s2.x * (a.at.y - b.at.y) - s2.y * (a.at.x - b.at.x)) / denominator
|
||||||
let
|
|
||||||
denominator = (-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)) / denominator
|
|
||||||
|
|
||||||
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 = a.at + (t * s1)
|
||||||
at.y = a.at.y + (t * s1y)
|
|
||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
|
|
||||||
proc fractional*(v: float32): float32 =
|
proc fractional*(v: float32): float32 {.inline.} =
|
||||||
## Returns unsigned fraction part of the float.
|
## Returns unsigned fraction part of the float.
|
||||||
## -13.7868723 -> 0.7868723
|
## -13.7868723 -> 0.7868723
|
||||||
result = abs(v)
|
result = abs(v)
|
||||||
|
|
Loading…
Reference in a new issue