Skip to content

Commit

Permalink
-- geometry fundamentals
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed May 25, 2024
1 parent ee4d299 commit c54efc7
Show file tree
Hide file tree
Showing 6 changed files with 9,683 additions and 6 deletions.
15 changes: 15 additions & 0 deletions sandbox/src/main/scala/geometry/GeometryFundamentals.scala
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import cats.implicits.catsSyntaxTuple3Semigroupal
import common.Base
import org.scalacheck.Gen

class GeoPlayground extends Base {
class GeometryPlayground extends Base with GeometryFundamentals {

/** int generator 0 to 10 */
val intG: Gen[Int] = Gen.choose[Int](0, 9)

case class Pt(x: Int, y: Int, z: Int)
Expand All @@ -17,17 +16,14 @@ class GeoPlayground extends Base {
override implicit val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(20)

def sq(x: Int): Int = x * x

def distance(pt1: Pt, pt2: Pt): Double =
math.sqrt(sq(pt1.x - pt2.x) + sq(pt1.y - pt2.y) + sq(pt1.z - pt2.z))
distance(pt1.x - pt2.x, pt1.y - pt2.y, pt1.z - pt2.z)

def next(pt: Pt, distance: Int): Option[Pt] = ???

test("1") {

forAll { (pt1: Pt, pt2: Pt) =>

println(pt1 -> pt2 -> distance(pt1, pt2))
}

Expand Down
33 changes: 33 additions & 0 deletions sandbox/src/main/scala/geometry/NaturalDistances.scala
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)))
}

}
Loading

0 comments on commit c54efc7

Please sign in to comment.