Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Created October 26, 2018 08:06
Show Gist options
  • Save shadowmint/20f483bcb21f538dc0b2e48419227e91 to your computer and use it in GitHub Desktop.
Save shadowmint/20f483bcb21f538dc0b2e48419227e91 to your computer and use it in GitHub Desktop.
package example
import org.apache.commons.math3.geometry.euclidean.twod._
import org.apache.commons.math3.geometry.partitioning.{BoundaryProjection, RegionFactory}
import org.apache.commons.math3.geometry.partitioning.Region.Location
import org.scalatest._
import scala.collection.JavaConverters._
class GeomTest extends FlatSpec with Matchers {
"basic rect" should "have rect properties" in {
val rect = new PolygonsSet(0, 1, 0, 1, 0.0001)
assert(rect.checkPoint(new Vector2D(0.5, 0.5)) == Location.INSIDE)
assert(rect.checkPoint(new Vector2D(0, 0)) == Location.BOUNDARY)
}
"basic rect from verts" should "have rect properties" in {
val rect = new PolygonsSet(0.0001,
new Vector2D(0, 0),
new Vector2D(1, 0),
new Vector2D(1, 1),
new Vector2D(0, 1))
assert(rect.checkPoint(new Vector2D(0.5, 0.5)) == Location.INSIDE)
assert(rect.checkPoint(new Vector2D(0, 0)) == Location.BOUNDARY)
}
"combine shapes" should "work" in {
val rect1 = new PolygonsSet(0, 1, 0, 1, 1.0e-10)
val rect2 = new PolygonsSet(0.5, 1.5, 0.5, 1.5, 1.0e-10)
val shape = new RegionFactory().union(rect1, rect2)
assert(shape.checkPoint(new Vector2D(0.5, 0.5)) == Location.INSIDE)
assert(shape.checkPoint(new Vector2D(0, 1.5)) == Location.OUTSIDE)
}
"raycast" should "work" in {
val rect1 = new PolygonsSet(0, 1, 0, 1, 1.0e-10)
val rect2 = new PolygonsSet(0.5, 1.5, 0.5, 1.5, 1.0e-10)
val shape = new RegionFactory().union(rect1, rect2)
val ray = new SubLine(new Vector2D(5, 5), new Vector2D(-1, -1), 1.0e-10)
val intersection: SubLine = shape.intersection(ray).asInstanceOf[SubLine]
println(intersection.isEmpty)
println(intersection.getSize)
for (k <- intersection.getSegments.toArray()) {
val start = k.asInstanceOf[Segment].getStart
val end = k.asInstanceOf[Segment].getEnd
println(s"$start -> $end")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment