Created
November 30, 2011 20:30
-
-
Save corruptmemory/1410684 to your computer and use it in GitHub Desktop.
Outline of overlapping shape tester...
This file contains hidden or 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
case class Point(x:Double,y:Double) | |
case class Rectangle(p1:Point,p2:Point) { | |
def overlaps(that:Rectangle):Boolean = | |
// Rectangle overlap test | |
} | |
trait Overlapable { | |
def boundingRectangle:Rectangle | |
def convexHulls:Seq[Seq[Point]] | |
def overlaps(that:Seq[Seq[Point]]):Boolean | |
} | |
object Rectangle { | |
implicit def rectangleOverlapable(r:Rectangle):Overlapable = new Overlapable { | |
def boundingRectangle:Rectangle = r | |
def convexHulls:Seq[Seq[Point]] = Seq(Seq(Point(r.p1.x,r.p1.y), | |
Point(r.p1.x,r.p2.y), | |
Point(r.p2.x,r.p2.y), | |
Point(r.p2.x,r.p1.y))) | |
def overlaps(that:Seq[Seq[Point]]):Boolean = | |
that.foldLeft(False) { | |
(s,hull) => hull.foldLeft((s,hull(0))) { | |
case ((r,prevPt),pt) => { | |
// psuedo code | |
if (lineIntersects(prevPt,pt,r)) (r || True,pt) | |
else (r || False,pt) | |
} | |
} | |
} | |
} | |
} | |
object Test { | |
def overlaps(o1:Overlapable,o2:Overlapable):Boolean = | |
if (o1.boundingRectangle.overlaps(o2.boundingRectangle)) o1.overlaps(o2) | |
else false | |
} | |
object Main { | |
def main(args:Array[String]):Unit { | |
val r1 = Rectangle(Point(<x1>,<y1>),Point(<x2>,<y2>)) | |
val r2 = Rectangle(Point(<x3>,<y3>),Point(<x4>,<y4>)) | |
println(Test.overlaps(r1,r2)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment