Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jarvis march implementation scala #812

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions contents/jarvis_march/code/scala/jarvisMarch.scala
Original file line number Diff line number Diff line change
@@ -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),
(20, 20),
(-10, 11),
(-13, 10),
(-6, 15),
(-6, -8),
(15, -9),
(7, -7),
(-2, -9),
(15, -15),
(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)
}
}
2 changes: 2 additions & 0 deletions contents/jarvis_march/jarvis_march.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

<script>
Expand Down