blob: 31a639f591ed35c000f2e137c7c1ec858e85f780 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import types/vector
type
Line* = object
p0*: Vector2D
p1*: Vector2D
LineSegment* = object
line: Line
miny*: float64
maxy*: float64
minyx*: float64
islope*: float64
func minx*(line: Line): float64 =
return min(line.p0.x, line.p1.x)
func maxx*(line: Line): float64 =
return max(line.p0.x, line.p1.x)
func minyx*(line: Line): float64 =
if line.p0.y < line.p1.y:
return line.p0.x
return line.p1.x
func maxyx*(line: Line): float64 =
if line.p0.y > line.p1.y:
return line.p0.x
return line.p1.x
func miny*(line: Line): float64 =
return min(line.p0.y, line.p1.y)
func maxy*(line: Line): float64 =
return max(line.p0.y, line.p1.y)
func slope*(line: Line): float64 =
let xdiff = (line.p0.x - line.p1.x)
if xdiff == 0:
return 0
return (line.p0.y - line.p1.y) / xdiff
# inverse slope
func islope*(line: Line): float64 =
let ydiff = (line.p0.y - line.p1.y)
if ydiff == 0:
return 0
return (line.p0.x - line.p1.x) / ydiff
proc cmpLineSegmentY*(l1, l2: LineSegment): int =
return cmp(l1.miny, l2.miny)
proc cmpLineSegmentX*(l1, l2: LineSegment): int =
return cmp(l1.minyx, l2.minyx)
func p0*(ls: LineSegment): Vector2D {.inline.} = ls.line.p0
func p1*(ls: LineSegment): Vector2D {.inline.} = ls.line.p1
converter toLineSegment*(line: Line): LineSegment =
LineSegment(
line: line,
miny: line.miny,
maxy: line.maxy,
minyx: line.minyx,
islope: line.islope
)
|