Skip to content

Commit

Permalink
Removed while loops
Browse files Browse the repository at this point in the history
  • Loading branch information
VOSID8 committed Apr 21, 2024
1 parent 71c5399 commit 87bfe02
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
23 changes: 14 additions & 9 deletions core/shared/src/main/scala/doodle/core/Transform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package doodle
package core

import scala.annotation.tailrec

/** Representation of an affine transformation as an augmented matrix. */
final case class Transform(elements: Array[Double]) {
def apply(point: Point): Point = {
Expand Down Expand Up @@ -70,17 +72,20 @@ final case class Transform(elements: Array[Double]) {
}

override def equals(that: Any): Boolean = {
@scala.annotation.tailrec
def checkEquality(
thisElements: Array[Double],
otherElements: Array[Double],
index: Int
): Boolean = {
if (index >= thisElements.length) true
else if (thisElements(index) != otherElements(index)) false
else checkEquality(thisElements, otherElements, index + 1)
}

that.isInstanceOf[Transform] && {
val other = that.asInstanceOf[Transform]
var i = 0
var isEqual = true
while (i < elements.length) {
if (this.elements(i) != other.elements(i))
isEqual = false

i = i + 1
}
isEqual
checkEquality(this.elements, other.elements, 0)
}
}

Expand Down
26 changes: 14 additions & 12 deletions golden/src/test/scala/doodle/golden/Golden.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,21 @@ trait Golden { self: FunSuite =>
}

def pixelAbsoluteError(a: Int, b: Int): Int = {
var error = 0
var i = 0
while (i < 4) {
val shift = i * 8
val mask = 0x000000ff << shift
val aValue = (a & mask) >> shift
val bValue = (b & mask) >> shift

error = error + Math.abs(aValue - bValue)

i = i + 1
@scala.annotation.tailrec
def calculateError(i: Int, acc: Int): Int = {
if (i >= 4) acc
else {
val shift = i * 8
val mask = 0x000000ff << shift
val aValue = (a & mask) >> shift
val bValue = (b & mask) >> shift

val newError = acc + Math.abs(aValue - bValue)
calculateError(i + 1, newError)
}
}
error

calculateError(0, 0)
}

def absoluteError(
Expand Down
7 changes: 2 additions & 5 deletions java2d/src/main/scala/doodle/java2d/effect/Java2DPanel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ final class Java2DPanel(frame: Frame)(implicit runtime: IORuntime)
Java2d.render(gc, reified, tx)

// Draw remaining images, redrawing *before* each image
var i = 0
while (i < pictures.size) {
pictures.foreach { case (bb, reified) =>
frame.redraw match {
case Redraw.ClearToBackground =>
frame.background.foreach { c =>
Expand All @@ -140,7 +139,6 @@ final class Java2DPanel(frame: Frame)(implicit runtime: IORuntime)
gc.fillRect(0, 0, getWidth(), getHeight())
}

val (bb, reified) = pictures(i)
val tx = Java2d.transform(
bb,
getWidth.toDouble,
Expand All @@ -149,9 +147,8 @@ final class Java2DPanel(frame: Frame)(implicit runtime: IORuntime)
)

Java2d.render(gc, reified, tx)

i = i + 1
}

}
}

Expand Down

0 comments on commit 87bfe02

Please sign in to comment.