Skip to content

Commit

Permalink
Bug fix for polygon intersects polygon where polygon contains polygon (
Browse files Browse the repository at this point in the history
…#179)

* fix space

* Fix insects bug where a line is fully inside polygon, without crossing an edge

* added PolyLine intersects Polygon function

* added PolyLine tests

* Fix header

* fix space

* Fix insects bug where a line is fully inside polygon, without crossing an edge

* added PolyLine intersects Polygon function

* added PolyLine tests

* added polyline polygon insects function

* added polygon polyline insection function

* fix wrong merge

* fix second merge error

* debug and add test getRingPolygon

* resolve conflicts

* added tests polygon contains polygon

* fix bug where polygon contains polygon

* get vertexes from multipolygons

* added test for getVertexes function

* added test where a polygon intersects itself

* make private getVertexes function
  • Loading branch information
Charmatzis authored and harsha2010 committed Jan 13, 2018
1 parent 62910c3 commit 58ebcd9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/main/scala/magellan/Polygon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ class Polygon extends Shape {
* @return
*/
private [magellan] def intersects(polygon: Polygon): Boolean = {
polygon.loops.exists(otherLoop => this.loops.exists(_.intersects(otherLoop)))
var intersects = false
if(polygon.getVertexes().exists(other => this.contains(other))
|| this.getVertexes().exists(vertex => polygon.contains(vertex))){
intersects = true
}
else{
intersects = polygon.loops.exists(otherLoop => this.loops.exists(_.intersects(otherLoop)))
}
intersects
}

private [magellan] def contains(box: BoundingBox): Boolean = {
Expand Down Expand Up @@ -209,6 +217,21 @@ class Polygon extends Shape {

def getRing(index: Int): Int = indices(index)

private def getVertexes():Array[Point]={
var ring = 0
val vertexes = new ArrayBuffer[Point]()
while(ring < getNumRings()) {
var i = 0
var ringPolygon = getRingPolygon(ring)
while (i < ringPolygon.length() - 1) {
vertexes += ringPolygon.getVertex(i)
i += 1
}
ring += 1
}
vertexes.toArray
}

def getRingPolygon(index: Int): Polygon = {
var startindex = getRing(index)
if(indices.length==1){
Expand Down
35 changes: 35 additions & 0 deletions src/test/scala/magellan/PolygonSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,41 @@ class PolygonSuite extends FunSuite {

assert(!polygon1.intersects(polygon5))

/**
* +---------+ 1,1
* + +----+ +
* + + + +
* + +----+ +
* +---------+
*
*/

val ring6 = Array(Point(0.5, 0.5), Point(0.5, -0.5),
Point(-0.5, -0.5), Point(-0.5, 0.5), Point(0.5, 0.5))
val polygon6 = Polygon(Array(0), ring6)

assert(polygon1.intersects(polygon6))


/**
* +---------+ 1.5,1.5
* + +----+ +
* + + + +
* + +----+ +
* +---------+
*
*/

val ring7 = Array(Point(1.5, 1.5), Point(1.5, -1.5),
Point(-1.5, -1.5), Point(-1.5, 1.5), Point(1.5, 1.5))
val polygon7 = Polygon(Array(0), ring7)

assert(polygon1.intersects(polygon7))



//polygon intesects itself
assert(polygon1.intersects(polygon1))
}

test("get ring as Polygon") {
Expand Down

0 comments on commit 58ebcd9

Please sign in to comment.