Skip to content

Commit

Permalink
Fixed broken negative coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Aug 25, 2024
1 parent 3331a93 commit fd91642
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/tripaint/coords/GlobalPixCoords.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ case class GlobalPixCoords(value: Long) extends AnyVal {
object GlobalPixCoords {
private val sqrt3: Double = math.sqrt(3)

inline def apply(x: Int, y: Int): GlobalPixCoords = new GlobalPixCoords(x.toLong << 32 | y)
inline def apply(x: Int, y: Int): GlobalPixCoords = new GlobalPixCoords(
x.toLong << 32 | (y.toLong & 0xffffffffL)
)
}
2 changes: 1 addition & 1 deletion core/src/main/scala/tripaint/coords/GridCoords.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ case class GridCoords(value: Int) extends AnyVal {

object GridCoords {
inline def apply(x: Int, y: Int): GridCoords = {
new GridCoords(x << 16 | y)
new GridCoords(x << 16 | (y & 0xffff))
}
}
4 changes: 2 additions & 2 deletions core/src/main/scala/tripaint/coords/PixelCoords.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ object PixelCoords {
if upsideDown then {
val image = GridCoords(ix + 1, iy)
val pix = TriangleCoords(2 * imageSize - 1 - px1, imageSize - 1 - py1)
new PixelCoords(image.value.toLong << 32 | pix.value.toLong)
new PixelCoords(image.value.toLong << 32 | (pix.value.toLong & 0xffffffffL))
} else {
val image = GridCoords(ix, iy)
val pix = TriangleCoords(px1, py1)
new PixelCoords(image.value.toLong << 32 | pix.value.toLong)
new PixelCoords(image.value.toLong << 32 | (pix.value.toLong & 0xffffffffL))
}
}
}
7 changes: 4 additions & 3 deletions core/src/main/scala/tripaint/coords/TriangleCoords.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ case class TriangleCoords(value: Int) extends AnyVal {

object TriangleCoords {
inline def apply(x: Int, y: Int): TriangleCoords = {
assert(x >= 0)
assert(x <= 2 * y)
assert(y < 0x1000)
new TriangleCoords(x << 12 | y)
}

def fromInt(repr: Int): TriangleCoords = {
if repr == -1 then {
throw new IllegalArgumentException()
}
assert(repr != -1)
TriangleCoords(repr >>> 12, repr & 0xfff)
}
}
6 changes: 3 additions & 3 deletions core/src/test/scala/tripaint/coords/TriangleCoordsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TriangleCoordsTest extends FunSuite {
testToIntSuccess(0xfff * 2, 0xfff)(0x1ffefff)
}
test("fromInt should throw an exception for -1") {
intercept[IllegalArgumentException](TriangleCoords.fromInt(-1))
intercept[AssertionError](TriangleCoords.fromInt(-1))
}
test("fromInt should throw an exception for invalid input") {
testFromIntFail(0x001000)
Expand All @@ -49,13 +49,13 @@ class TriangleCoordsTest extends FunSuite {
}

private def testApplyFail(x: Int, y: Int): Unit =
intercept[IllegalArgumentException](TriangleCoords(x, y))
intercept[AssertionError](TriangleCoords(x, y))

private def testToIntSuccess(x: Int, y: Int)(repr: Int): Unit =
assertEquals(TriangleCoords(x, y).toInt, repr)

private def testFromIntFail(repr: Int): Unit =
intercept[IllegalArgumentException](TriangleCoords.fromInt(repr))
intercept[AssertionError](TriangleCoords.fromInt(repr))

private def testFromIntSuccess(repr: Int)(x: Int, y: Int): Unit =
assertEquals(TriangleCoords.fromInt(repr), TriangleCoords(x, y))
Expand Down

0 comments on commit fd91642

Please sign in to comment.