From 2f076d5defa8ef4706a0014e015253a80cbd99d8 Mon Sep 17 00:00:00 2001 From: Ken Power Date: Fri, 26 Mar 2021 20:15:22 +0000 Subject: [PATCH 1/4] scala jarvis march initial --- .../code/scala/jarvis_,march.scala | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 contents/jarvis_march/code/scala/jarvis_,march.scala diff --git a/contents/jarvis_march/code/scala/jarvis_,march.scala b/contents/jarvis_march/code/scala/jarvis_,march.scala new file mode 100644 index 000000000..ada90be0c --- /dev/null +++ b/contents/jarvis_march/code/scala/jarvis_,march.scala @@ -0,0 +1,63 @@ +object GiftWrap { + + case class Point(x: Int, y: Int) + + def jarvis_march(gift: List[Point]): List[Point] = { + + def is_clockwise(p1: Point, p2: Point, p3: Point): Boolean = + (p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x) + + def most_clockwise(apex: Point)(a: Point, b: Point): Point = + if (!is_clockwise(apex, a, b)) a else b + + def next_point_on_hull(current: Point): Point = + gift + .filter(_ != current) //ignore current point + .reduce(most_clockwise(current)) + + def build_hull(hull: List[Point]): List[Point] = + next_point_on_hull(hull.last) match { + case back_to_start if back_to_start == hull.head => hull + case new_point => build_hull(hull :+ new_point) + } + + def leftmost(points: List[Point]): Point = + points.reduce((a, b) => if (a.x < b.x) a else b) + + // leftmost point guaranteed to be in hull + build_hull(List(leftmost(gift))) + + } + + + def main(args: Array[String]): Unit = { + + val test_gift = List( + (-5, 2), + (-13, 100), + (5, 7), + (-6, -12), + (-14, -14), + (9, 9), + (-1, -1), + (100, 100), + (-10, 11), + (-13, 100), + (-6, 15), + (-6, -8), + (15, -9), + (7, -7), + (-2, -9), + (100, -100), + (6, -5), + (0, 14), + (2, 8) + ).map({ case (x, y) => Point(x, y) }) + + val hull = jarvis_march(test_gift) + + println("The points in the wrapping are:") + hull.foreach(println) + + } +} \ No newline at end of file From c6e1d0e95b7ec2b7f7d4bb9e1255a5bdcbc6fbe0 Mon Sep 17 00:00:00 2001 From: Ken Power Date: Sun, 28 Mar 2021 14:40:37 +0100 Subject: [PATCH 2/4] refactoring and renaming --- .../jarvis_march/code/scala/jarvisMarch.scala | 60 ++++++++++++++++++ .../code/scala/jarvis_,march.scala | 63 ------------------- 2 files changed, 60 insertions(+), 63 deletions(-) create mode 100644 contents/jarvis_march/code/scala/jarvisMarch.scala delete mode 100644 contents/jarvis_march/code/scala/jarvis_,march.scala diff --git a/contents/jarvis_march/code/scala/jarvisMarch.scala b/contents/jarvis_march/code/scala/jarvisMarch.scala new file mode 100644 index 000000000..f5dac8452 --- /dev/null +++ b/contents/jarvis_march/code/scala/jarvisMarch.scala @@ -0,0 +1,60 @@ +object GiftWrap { + + case class Point(x: Int, y: Int) + + def jarvisMarch(gift: List[Point]): List[Point] = { + + def isClockwise(p1: Point, p2: Point, p3: Point): Boolean = + (p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x) + + def leastClockwise(apex: Point)(a: Point, b: Point): Point = + if (!isClockwise(apex, a, b)) a else b + + def nextPointOnHull(current: Point): Point = + gift + .filter(_ != current) //ignore current point + .reduce(leastClockwise(current)) + + def leftmost(points: List[Point]): Point = + points.reduce((a, b) => if (a.x < b.x) a else b) + + def buildHull(hull: List[Point]): List[Point] = + nextPointOnHull(hull.last) match { + case backToStart if backToStart == hull.head => hull + case nextPoint => buildHull(hull :+ nextPoint) + } + + buildHull(List(leftmost(gift))) // leftmost point guaranteed to be in hull + + } + + def main(args: Array[String]): Unit = { + + val testGift = List( + (-5, 2), + (-13, 100), + (5, 7), + (-6, -12), + (-14, -14), + (9, 9), + (-1, -1), + (100, 100), + (-10, 11), + (-13, 100), + (-6, 15), + (-6, -8), + (15, -9), + (7, -7), + (-2, -9), + (100, -100), + (6, -5), + (0, 14), + (2, 8) + ).map({ case (x, y) => Point(x, y) }) + + val hull = jarvisMarch(testGift) + + println("The points in the wrapping are:") + hull.foreach(println) + } +} diff --git a/contents/jarvis_march/code/scala/jarvis_,march.scala b/contents/jarvis_march/code/scala/jarvis_,march.scala deleted file mode 100644 index ada90be0c..000000000 --- a/contents/jarvis_march/code/scala/jarvis_,march.scala +++ /dev/null @@ -1,63 +0,0 @@ -object GiftWrap { - - case class Point(x: Int, y: Int) - - def jarvis_march(gift: List[Point]): List[Point] = { - - def is_clockwise(p1: Point, p2: Point, p3: Point): Boolean = - (p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x) - - def most_clockwise(apex: Point)(a: Point, b: Point): Point = - if (!is_clockwise(apex, a, b)) a else b - - def next_point_on_hull(current: Point): Point = - gift - .filter(_ != current) //ignore current point - .reduce(most_clockwise(current)) - - def build_hull(hull: List[Point]): List[Point] = - next_point_on_hull(hull.last) match { - case back_to_start if back_to_start == hull.head => hull - case new_point => build_hull(hull :+ new_point) - } - - def leftmost(points: List[Point]): Point = - points.reduce((a, b) => if (a.x < b.x) a else b) - - // leftmost point guaranteed to be in hull - build_hull(List(leftmost(gift))) - - } - - - def main(args: Array[String]): Unit = { - - val test_gift = List( - (-5, 2), - (-13, 100), - (5, 7), - (-6, -12), - (-14, -14), - (9, 9), - (-1, -1), - (100, 100), - (-10, 11), - (-13, 100), - (-6, 15), - (-6, -8), - (15, -9), - (7, -7), - (-2, -9), - (100, -100), - (6, -5), - (0, 14), - (2, 8) - ).map({ case (x, y) => Point(x, y) }) - - val hull = jarvis_march(test_gift) - - println("The points in the wrapping are:") - hull.foreach(println) - - } -} \ No newline at end of file From 469b295ce7fd9899fb459d0b814b246b2bd5ddad Mon Sep 17 00:00:00 2001 From: Ken Power Date: Wed, 12 May 2021 09:44:48 +0100 Subject: [PATCH 3/4] fix points --- contents/jarvis_march/code/scala/jarvisMarch.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/jarvis_march/code/scala/jarvisMarch.scala b/contents/jarvis_march/code/scala/jarvisMarch.scala index f5dac8452..8f80fd0d8 100644 --- a/contents/jarvis_march/code/scala/jarvisMarch.scala +++ b/contents/jarvis_march/code/scala/jarvisMarch.scala @@ -38,15 +38,15 @@ object GiftWrap { (-14, -14), (9, 9), (-1, -1), - (100, 100), + (20, 20), (-10, 11), - (-13, 100), + (-13, 10), (-6, 15), (-6, -8), (15, -9), (7, -7), (-2, -9), - (100, -100), + (15, -15), (6, -5), (0, 14), (2, 8) From f59be84b88b9161c9cd268f6286884e043f5f697 Mon Sep 17 00:00:00 2001 From: Ken Power Date: Wed, 12 May 2021 09:48:23 +0100 Subject: [PATCH 4/4] add scala to md file --- contents/jarvis_march/jarvis_march.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/jarvis_march/jarvis_march.md b/contents/jarvis_march/jarvis_march.md index 0a48dbd85..f76b0e88f 100644 --- a/contents/jarvis_march/jarvis_march.md +++ b/contents/jarvis_march/jarvis_march.md @@ -50,6 +50,8 @@ Since this algorithm, there have been many other algorithms that have advanced t [import, lang:"v"](code/v/jarvis.v) {% sample lang="rust" %} [import, lang:"rust"](code/rust/jarvis_march.rs) +{% sample lang="scala" %} +[import, lang:"scala"](code/scala/jarvisMarch.scala) {% endmethod %}