-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
9,683 additions
and
6 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
sandbox/src/main/scala/geometry/GeometryFundamentals.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package geometry | ||
|
||
trait GeometryFundamentals { | ||
|
||
def sq(x: Int): Int = x * x | ||
|
||
def distance(d1: Int, d2: Int, d3: Int): Double = math.sqrt(sq(d1) + sq(d2) + sq(d3)) | ||
|
||
def isNat(x: Double): Option[Int] = Option(x.toInt).filter(_.toDouble == x) | ||
|
||
def distanceIsNat(d1: Int, d2: Int, d3: Int): Option[Int] = isNat(distance(d1, d2, d3)) | ||
|
||
} | ||
|
||
object GeometryFundamentals extends GeometryFundamentals |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package geometry | ||
|
||
import common.Base | ||
|
||
class NaturalDistances extends Base with GeometryFundamentals { | ||
|
||
def range(min: Int, count: Int) = LazyList.from(min).take(count) | ||
|
||
/** triplets with natural distances */ | ||
def genNatTriplets(min: Int = 0, count: Int = 100) = { | ||
val r = range(min, count) | ||
r.flatMap { d1 => | ||
r.flatMap { d2 => | ||
r.flatMap { d3 => | ||
distanceIsNat(d1, d2, d3) | ||
.filter(_ < 30) // filter distance | ||
.map(d => (d1, d2, d3) -> d) | ||
} | ||
} | ||
} | ||
} | ||
|
||
test("triplets") { | ||
genNatTriplets() | ||
.foreach(x => println(x)) | ||
} | ||
|
||
test("isNat") { | ||
Seq(3.123, 3.987, 3.0) | ||
.foreach(x => pprint.log(isNat(x))) | ||
} | ||
|
||
} |
Oops, something went wrong.