find me on
 
twitter del.icio.us
linkedin flickr
goodreads flixster
facebook aim
dopplr stackoverflow
tweets
 
  • @drewtoothpaste you probably hear this a lot, but i wish your tshirts were on american apparel. oh well, bought a rando piece of art. 3 hrs ago
  • sxsw is as full of douchebags as ever, but I am so sad I am missing this- BrooklynVegan Party: GZA, Japandroids, Fucked Up, Titus Andronicus 4 hrs ago
  • @kpich @ben8128 why won't you just love me? can't you see all i want is love? 7 hrs ago
  • @kpich what a psuedo-intellectual faux paw 7 hrs ago
  • straight up tanning on the beach. 16 hrs ago
  • More updates...
del.icio.us
 
recent posts
 
history
 
June 2009
M T W T F S S
« May   Jul »
1234567
891011121314
15161718192021
22232425262728
2930  
 
jason.prado (@) gmail.com real tangible
 
More Convex Hull in Scala
 

Rewrote this algorithm to be O(nlogn), and it’s oh-so-functional. The tail recursion and case statements were perfectly natural. I’m a convert– I think my next webapp backend will be in Scala.

    def fastConvexHull(points: List[Point2D]) = {
        def ensureConvex(hull: List[Point2D]): List[Point2D] = {
            if (hull.length > 2) {
                val lastThreePoints: List[Point2D] = Utility2D.lastThree(hull);
                if (Utility2D.isRightTurn(lastThreePoints(0), lastThreePoints(1), lastThreePoints(2)))
                    return ensureConvex(Utility2D.dropNextToLast(hull))
            }
            return hull;
        }

        def nextIter(remainingPoints: List[Point2D], hull: List[Point2D]): List[Point2D] = {
            remainingPoints match {
                case point :: rest => nextIter(rest, ensureConvex(hull ::: point :: Nil))
                case Nil => hull
            }
        }

        def hullHalf(points: List[Point2D]): List[Point2D] = {
            nextIter(points.tail.tail, points(0) :: points(1) :: Nil);
        }

        drawLine(hullHalf(points))
        drawLine(hullHalf(points.reverse))
    }
You can leave a response, or trackback from your own site.

Leave a Reply