This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ccfa88b
Showing
11 changed files
with
773 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language: go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (c) 2016 Josh Baker | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
Contact GitHub API Training Shop Blog About | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Poly | ||
==== | ||
[![Build Status](https://travis-ci.org/tidwall/poly.svg?branch=master)](https://travis-ci.org/tidwall/poly) | ||
[![GoDoc](https://godoc.org/github.com/tidwall/poly?status.svg)](https://godoc.org/github.com/tidwall/poly) | ||
|
||
Polygon detection methods for Go. | ||
|
||
Contact | ||
------- | ||
Josh Baker [@tidwall](http://twitter.com/tidwall) | ||
|
||
License | ||
------- | ||
Poly source code is available under the MIT [License](/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package poly | ||
|
||
// Inside returns true if point is inside of exterior and not in a hole. | ||
// The validity of the exterior and holes must be done elsewhere and are assumed valid. | ||
// A valid exterior is a near-linear ring. | ||
// A valid hole is one that is full contained inside the exterior. | ||
// A valid hole may not share the same segment line as the exterior. | ||
func (p Point) Inside(exterior Polygon, holes []Polygon) bool { | ||
if !insideshpext(p, exterior, true) { | ||
return false | ||
} | ||
for i := 0; i < len(holes); i++ { | ||
if insideshpext(p, holes[i], false) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Inside returns true if shape is inside of exterior and not in a hole. | ||
func (shape Polygon) Inside(exterior Polygon, holes []Polygon) bool { | ||
var ok bool | ||
for _, p := range shape { | ||
ok = p.Inside(exterior, holes) | ||
if !ok { | ||
return false | ||
} | ||
} | ||
ok = true | ||
for _, hole := range holes { | ||
if hole.Inside(shape, nil) { | ||
return false | ||
} | ||
} | ||
return ok | ||
} | ||
|
||
func insideshpext(p Point, shape Polygon, exterior bool) bool { | ||
// if len(shape) < 3 { | ||
// return false | ||
// } | ||
in := false | ||
for i := 0; i < len(shape); i++ { | ||
res := raycast(p, shape[i], shape[(i+1)%len(shape)]) | ||
if res == on { | ||
return exterior | ||
} | ||
if res == left { | ||
in = !in | ||
} | ||
} | ||
return in | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package poly | ||
|
||
import "testing" | ||
|
||
func testRayInside(t *testing.T, p Point, ps []Point, expect bool) { | ||
res := insideshpext(p, ps, true) | ||
if res != expect { | ||
t.Fatalf("{%v,%v} = %t, expect %t", p.X, p.Y, res, expect) | ||
} | ||
} | ||
|
||
func TestRayInside(t *testing.T) { | ||
strange := []Point{P(0, 0), P(0, 3), P(4, -3), P(4, 0), P(0, 0)} | ||
|
||
// on the edge | ||
testRayInside(t, P(0, 0), strange, true) | ||
testRayInside(t, P(0, 3), strange, true) | ||
|
||
testRayInside(t, P(4, -3), strange, true) | ||
testRayInside(t, P(4, -2), strange, true) | ||
testRayInside(t, P(3, 0), strange, true) | ||
testRayInside(t, P(1, 0), strange, true) | ||
|
||
// ouside by just a tad | ||
testRayInside(t, P(-0.1, 0), strange, false) | ||
testRayInside(t, P(-0.1, -0.1), strange, false) | ||
testRayInside(t, P(0, 3.1), strange, false) | ||
testRayInside(t, P(0.1, 3.1), strange, false) | ||
testRayInside(t, P(-0.1, 3), strange, false) | ||
testRayInside(t, P(4, -3.1), strange, false) | ||
testRayInside(t, P(3.9, -3), strange, false) | ||
testRayInside(t, P(4.1, -2), strange, false) | ||
testRayInside(t, P(3, 0.1), strange, false) | ||
testRayInside(t, P(1, -0.1), strange, false) | ||
} | ||
|
||
var texterior = Polygon{ | ||
P(0, 0), | ||
P(0, 6), | ||
P(12, -6), | ||
P(12, 0), | ||
P(0, 0), | ||
} | ||
var tholeA = Polygon{ | ||
P(1, 1), | ||
P(1, 2), | ||
P(2, 2), | ||
P(2, 1), | ||
} | ||
var tholeB = Polygon{ | ||
P(11, -1), | ||
P(11, -3), | ||
P(9, -1), | ||
} | ||
var tholes = []Polygon{tholeA, tholeB} | ||
|
||
func TestRayExteriorHoles(t *testing.T) { | ||
|
||
type point struct { | ||
p Point | ||
ok bool | ||
} | ||
|
||
points := []point{ | ||
{P(.5, 3), true}, | ||
{P(11.5, -4.5), true}, | ||
{P(6, 0), true}, | ||
|
||
{P(3.5, .5), true}, | ||
{P(1.5, 1.5), false}, | ||
{P(10.5, -1.5), false}, | ||
{P(-2, 0), false}, | ||
{P(0, -2), false}, | ||
{P(8, -3), false}, | ||
{P(8, 1), false}, | ||
{P(14, -1), false}, | ||
} | ||
// add the edges, all should be inside | ||
for i := 0; i < len(texterior); i++ { | ||
points = append(points, point{texterior[i], true}) | ||
} | ||
for i := 0; i < len(tholeA); i++ { | ||
points = append(points, point{tholeA[i], true}) | ||
} | ||
for i := 0; i < len(tholeB); i++ { | ||
points = append(points, point{tholeB[i], true}) | ||
} | ||
|
||
for i := 0; i < len(points); i++ { | ||
ok := points[i].p.Inside(texterior, tholes) | ||
if ok != points[i].ok { | ||
t.Fatalf("{%v,%v} = %t, expect %t", points[i].p.X, points[i].p.Y, ok, points[i].ok) | ||
} | ||
} | ||
} | ||
|
||
func TestInsideShapes(t *testing.T) { | ||
if texterior.Inside(texterior, nil) == false { | ||
t.Fatalf("expect true, got false") | ||
} | ||
if texterior.Inside(texterior, tholes) == true { | ||
t.Fatalf("expect false, got true") | ||
} | ||
if tholeA.Inside(texterior, nil) == false { | ||
t.Fatalf("expect true, got false") | ||
} | ||
if tholeB.Inside(texterior, nil) == false { | ||
t.Fatalf("expect true, got false") | ||
} | ||
if tholeA.Inside(tholeB, nil) == true { | ||
t.Fatalf("expect false, got true") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package poly | ||
|
||
// Intersects detects if a point intersects another polygon | ||
func (p Point) Intersects(exterior Polygon, holes []Polygon) bool { | ||
return p.Inside(exterior, holes) | ||
} | ||
|
||
// Intersects detects if a polygon intersects another polygon | ||
func (shape Polygon) Intersects(exterior Polygon, holes []Polygon) bool { | ||
return shape.doesIntersects(false, exterior, holes) | ||
} | ||
|
||
// LineStringIntersects detects if a polygon intersects a linestring | ||
func (shape Polygon) LineStringIntersects(exterior Polygon, holes []Polygon) bool { | ||
return shape.doesIntersects(true, exterior, holes) | ||
} | ||
func (shape Polygon) doesIntersects(isLineString bool, exterior Polygon, holes []Polygon) bool { | ||
switch len(shape) { | ||
case 0: | ||
return false | ||
case 1: | ||
switch len(exterior) { | ||
case 0: | ||
return false | ||
case 1: | ||
return shape[0].X == exterior[0].X && shape[0].Y == shape[0].Y | ||
default: | ||
return shape[0].Inside(exterior, holes) | ||
} | ||
default: | ||
switch len(exterior) { | ||
case 0: | ||
return false | ||
case 1: | ||
return exterior[0].Inside(shape, holes) | ||
} | ||
} | ||
if !shape.Rect().IntersectsRect(exterior.Rect()) { | ||
return false | ||
} | ||
for i := 0; i < len(shape); i++ { | ||
for j := 0; j < len(exterior); j++ { | ||
if lineintersects( | ||
shape[i], shape[(i+1)%len(shape)], | ||
exterior[j], exterior[(j+1)%len(exterior)], | ||
) { | ||
return true | ||
} | ||
} | ||
} | ||
for _, hole := range holes { | ||
if shape.Inside(hole, nil) { | ||
return false | ||
} | ||
} | ||
if shape.Inside(exterior, nil) { | ||
return true | ||
} | ||
if !isLineString { | ||
if exterior.Inside(shape, nil) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func lineintersects( | ||
a, b Point, // segment 1 | ||
c, d Point, // segment 2 | ||
) bool { | ||
// do the bounding boxes intersect? | ||
// the following checks without swapping values. | ||
if a.Y > b.Y { | ||
if c.Y > d.Y { | ||
if b.Y > c.Y || a.Y < d.Y { | ||
return false | ||
} | ||
} else { | ||
if b.Y > d.Y || a.Y < c.Y { | ||
return false | ||
} | ||
} | ||
} else { | ||
if c.Y > d.Y { | ||
if a.Y > c.Y || b.Y < d.Y { | ||
return false | ||
} | ||
} else { | ||
if a.Y > d.Y || b.Y < c.Y { | ||
return false | ||
} | ||
} | ||
} | ||
if a.X > b.X { | ||
if c.X > d.X { | ||
if b.X > c.X || a.X < d.X { | ||
return false | ||
} | ||
} else { | ||
if b.X > d.X || a.X < c.X { | ||
return false | ||
} | ||
} | ||
} else { | ||
if c.X > d.X { | ||
if a.X > c.X || b.X < d.X { | ||
return false | ||
} | ||
} else { | ||
if a.X > d.X || b.X < c.X { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
// the following code is from http://ideone.com/PnPJgb | ||
cmpx, cmpy := c.X-a.X, c.Y-a.Y | ||
rx, ry := b.X-a.X, b.Y-a.Y | ||
cmpxr := cmpx*ry - cmpy*rx | ||
if cmpxr == 0 { | ||
// Lines are collinear, and so intersect if they have any overlap | ||
if !(((c.X-a.X <= 0) != (c.X-b.X <= 0)) || ((c.Y-a.Y <= 0) != (c.Y-b.Y <= 0))) { | ||
return false | ||
} | ||
return true | ||
} | ||
sx, sy := d.X-c.X, d.Y-c.Y | ||
cmpxs := cmpx*sy - cmpy*sx | ||
rxs := rx*sy - ry*sx | ||
if rxs == 0 { | ||
return false // Lines are parallel. | ||
} | ||
rxsr := 1 / rxs | ||
t := cmpxs * rxsr | ||
u := cmpxr * rxsr | ||
if !((t >= 0) && (t <= 1) && (u >= 0) && (u <= 1)) { | ||
return false | ||
} | ||
return true | ||
} |
Oops, something went wrong.