Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The basic algorithm would go like this:</p> <pre><code>shapes.tail.foldLeft(boundingBox(shapes.head)) { case (box, shape) if box contains shape =&gt; box case (box, shape) if shape contains box =&gt; shape case (box, shape) =&gt; boxBounding(box, shape) } </code></pre> <p>Now you have to write <code>contains</code> and <code>boxBounding</code>, which is a pure algorithms problem more than a language problem.</p> <p>If the shapes all had the same center, implementing <code>contains</code> would be easier. It would go like this:</p> <pre><code>abstract class Shape { def contains(s: Shape): Boolean } case class Rectangle(width: Int, height: Int) extends Shape { def contains(s: Shape): Boolean = s match { case Rectangle(w2, h2) =&gt; width &gt;= w2 &amp;&amp; height &gt;= h2 case Location(x, y, s) =&gt; // not the same center case Circle(radius) =&gt; width &gt;= radius &amp;&amp; height &gt;= radius case Group(shapes @ _*) =&gt; shapes.forall(this.contains(_)) } } case class Location(x: Int, y: Int, shape: Shape) extends Shape { def contains(s: Shape): Boolean = // not the same center } case class Circle(radius: Int) extends Shape { def contains(s: Shape): Boolean = s match { case Rectangle(width, height) =&gt; radius &gt;= width &amp;&amp; radius &gt;= height case Location(x, y) =&gt; // not the same center case Circle(r2) =&gt; radius &gt;= r2 case Group(shapes @ _*) =&gt; shapes.forall(this.contains(_)) } } case class Group(shapes: Shape*) extends Shape { def contains(s: Shape): Boolean = shapes.exists(_ contains s) } </code></pre> <p>As for <code>boxBounding</code>, which takes two shapes and combine them, it will usually be a rectangle, but can be a circle under certain circunstances. Anyway, it is pretty straight-forward, once you have the algorithm figured out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload