Skip to content

Commit

Permalink
Rename Line to Segment
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Aug 2, 2021
1 parent d09fae0 commit 8df530d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 76 deletions.
16 changes: 8 additions & 8 deletions internal/exact/line.go → internal/exact/segment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package exact

type Line struct {
type Segment struct {
A, B XY64
}

Expand All @@ -9,17 +9,17 @@ type Intersection struct {
A, B XY64
}

func LineIntersection(lineA, lineB Line) Intersection {
func SegmentIntersection(segA, segB Segment) Intersection {
// Algorithm from https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection

if lineA.A == lineA.B || lineB.A == lineB.B {
panic("invalid line")
if segA.A == segA.B || segB.A == segB.B {
panic("invalid segment")
}

v1 := lineA.A.ToRat()
v2 := lineA.B.ToRat()
v3 := lineB.A.ToRat()
v4 := lineB.B.ToRat()
v1 := segA.A.ToRat()
v2 := segA.B.ToRat()
v3 := segB.A.ToRat()
v4 := segB.B.ToRat()

// d := (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)
sub12 := v1.Sub(v2)
Expand Down
136 changes: 68 additions & 68 deletions internal/exact/line_test.go → internal/exact/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
. "github.com/peterstace/simplefeatures/internal/exact"
)

func TestLineIntersection(t *testing.T) {
func TestSegmentIntersection(t *testing.T) {
var (
p00 = XY64{X: 0, Y: 0}
p10 = XY64{X: 1, Y: 0}
Expand All @@ -22,8 +22,8 @@ func TestLineIntersection(t *testing.T) {
p42 = XY64{X: 4, Y: 2}
p63 = XY64{X: 6, Y: 3}
)
ln := func(a, b XY64) Line {
return Line{A: a, B: b}
ln := func(a, b XY64) Segment {
return Segment{A: a, B: b}
}

e := func(xy XY64) XY64 {
Expand All @@ -37,126 +37,126 @@ func TestLineIntersection(t *testing.T) {
}

for _, tc := range []struct {
description string
lineA, lineB Line
inter Intersection
description string
segA, segB Segment
inter Intersection
}{
{
description: "parallel same length",
lineA: ln(p01, p11),
lineB: ln(p00, p10),
segA: ln(p01, p11),
segB: ln(p00, p10),
inter: Intersection{Empty: true},
},
{
description: "parallel slanted",
lineA: ln(p00, p21),
lineB: ln(p01, p22),
segA: ln(p00, p21),
segB: ln(p01, p22),
inter: Intersection{Empty: true},
},
{
description: "parallel different length",
lineA: ln(p01, p21),
lineB: ln(p00, p10),
segA: ln(p01, p21),
segB: ln(p00, p10),
inter: Intersection{Empty: true},
},
{
description: "cross",
lineA: ln(p00, p22),
lineB: ln(p02, p20),
segA: ln(p00, p22),
segB: ln(p02, p20),
inter: Intersection{A: p11, B: p11},
},
{
description: "would cross if longer",
lineA: ln(p00, p21),
lineB: ln(p02, p11),
segA: ln(p00, p21),
segB: ln(p02, p11),
inter: Intersection{Empty: true},
},
{
description: "just touches in middle",
lineA: ln(p00, p22),
lineB: ln(p02, p11),
segA: ln(p00, p22),
segB: ln(p02, p11),
inter: Intersection{A: p11, B: p11},
},
{
description: "touches at endpoints",
lineA: ln(p00, p20),
lineB: ln(p00, p02),
segA: ln(p00, p20),
segB: ln(p00, p02),
inter: Intersection{A: p00, B: p00},
},

// On same infinite sloping line.
// On same infinite sloping segment.
{
description: "on same sloping inf line but no intersection",
lineA: ln(p00, p21),
lineB: ln(p42, p63),
description: "on same sloping inf segment but no intersection",
segA: ln(p00, p21),
segB: ln(p42, p63),
inter: Intersection{Empty: true},
},
{
description: "on same sloping inf line touching",
lineA: ln(p00, p21),
lineB: ln(p21, p42),
description: "on same sloping inf segment touching",
segA: ln(p00, p21),
segB: ln(p21, p42),
inter: Intersection{A: p21, B: p21},
},
{
description: "on same sloping inf line staggered",
lineA: ln(p00, p42),
lineB: ln(p21, p63),
description: "on same sloping inf segment staggered",
segA: ln(p00, p42),
segB: ln(p21, p63),
inter: Intersection{A: p21, B: p42},
},
{
description: "on same sloping inf line overlapping",
lineA: ln(p00, p63),
lineB: ln(p21, p42),
description: "on same sloping inf segment overlapping",
segA: ln(p00, p63),
segB: ln(p21, p42),
inter: Intersection{A: p21, B: p42},
},
{
description: "on same sloping inf line overlapping with one same endpoint",
lineA: ln(p00, p42),
lineB: ln(p21, p42),
description: "on same sloping inf segment overlapping with one same endpoint",
segA: ln(p00, p42),
segB: ln(p21, p42),
inter: Intersection{A: p21, B: p42},
},
{
description: "on same sloping inf line overlapping with two same endpoints",
lineA: ln(p00, p42),
lineB: ln(p00, p42),
description: "on same sloping inf segment overlapping with two same endpoints",
segA: ln(p00, p42),
segB: ln(p00, p42),
inter: Intersection{A: p00, B: p42},
},

// On same infinite horizontal line.
// On same infinite horizontal segment.
{
description: "on same horizontal inf line but no intersection",
lineA: ln(p00, p20),
lineB: ln(p40, p60),
description: "on same horizontal inf segment but no intersection",
segA: ln(p00, p20),
segB: ln(p40, p60),
inter: Intersection{Empty: true},
},
{
description: "on same horizontal inf line touching",
lineA: ln(p00, p20),
lineB: ln(p20, p40),
description: "on same horizontal inf segment touching",
segA: ln(p00, p20),
segB: ln(p20, p40),
inter: Intersection{A: p20, B: p20},
},
{
description: "on same horizontal inf line staggered",
lineA: ln(p00, p40),
lineB: ln(p20, p60),
description: "on same horizontal inf segment staggered",
segA: ln(p00, p40),
segB: ln(p20, p60),
inter: Intersection{A: p20, B: p40},
},
{
description: "on same horizontal inf line overlapping",
lineA: ln(p00, p60),
lineB: ln(p20, p40),
description: "on same horizontal inf segment overlapping",
segA: ln(p00, p60),
segB: ln(p20, p40),
inter: Intersection{A: p20, B: p40},
},
{
description: "on same horizontal inf line overlapping with one same endpoint",
lineA: ln(p00, p40),
lineB: ln(p20, p40),
description: "on same horizontal inf segment overlapping with one same endpoint",
segA: ln(p00, p40),
segB: ln(p20, p40),
inter: Intersection{A: p20, B: p40},
},
{
description: "on same horizontal inf line overlapping with two same endpoints",
lineA: ln(p00, p40),
lineB: ln(p00, p40),
description: "on same horizontal inf segment overlapping with two same endpoints",
segA: ln(p00, p40),
segB: ln(p00, p40),
inter: Intersection{A: p00, B: p40},
},
} {
Expand All @@ -173,29 +173,29 @@ func TestLineIntersection(t *testing.T) {
{q, p, p, p},
} {
t.Run(fmt.Sprintf("flip_%d_group_%d", flip, groupIdx), func(t *testing.T) {
lineA := tc.lineA
lineB := tc.lineB
segA := tc.segA
segB := tc.segB
if flip&1 != 0 {
lineA, lineB = lineB, lineA
segA, segB = segB, segA
}
if flip&2 != 0 {
lineA.A, lineA.B = lineA.B, lineA.A
segA.A, segA.B = segA.B, segA.A
}
if flip&4 != 0 {
lineB.A, lineB.B = lineB.B, lineB.A
segB.A, segB.B = segB.B, segB.A
}

want := tc.inter
for _, fn := range group {
want.A = fn(want.A)
want.B = fn(want.B)
lineA.A = fn(lineA.A)
lineA.B = fn(lineA.B)
lineB.A = fn(lineB.A)
lineB.B = fn(lineB.B)
segA.A = fn(segA.A)
segA.B = fn(segA.B)
segB.A = fn(segB.A)
segB.B = fn(segB.B)
}

got := LineIntersection(lineA, lineB)
got := SegmentIntersection(segA, segB)
if want.Empty != got.Empty {
t.Fatalf("got_empty:%v want_empty:%v", got.Empty, want.Empty)
}
Expand Down

0 comments on commit 8df530d

Please sign in to comment.