Commit 2d74cdb5 authored by h2b's avatar h2b

Grid layout improved. Iterator fixed. Tests added.

parent dade450b
......@@ -47,35 +47,31 @@ import Floating._
* @since 1.1.0
* @author h2b
*/
class GridLayout private (val rows: Int, val cols: Int, val cell: Cell, val floating: Floating) extends Iterable[Pixel] {
class GridLayout private (val rows: Int, val cols: Int, val cell: Cell, val floating: Floating,
p0: Pixel) extends Iterable[Pixel] {
def iterator = floating match {
case Horizontal rowIterator
case Vertical colIterator
}
/**
* A copy of this grid layout at a new origin.
*/
def at (origin: Pixel): GridLayout = new GridLayout(rows, cols, cell, floating, origin)
private def rowIterator = new Iterator[Pixel] {
private var i, j = 0
def hasNext = i < rows
def next = {
if (!hasNext) throw new NoSuchElementException("iterator overflow")
val result = Pixel(i*cell.width, j*cell.height)
j += 1
if (j == cols) { i += 1; j = 0 }
result
}
def iterator = floating match {
case Horizontal floatingIterator(rows, cols, (i, j) (i, j))
case Vertical floatingIterator(cols, rows, (i, j) (j, i))
}
private def colIterator = new Iterator[Pixel] {
private var i, j = 0
def hasNext = j < cols
def next = {
if (!hasNext) throw new NoSuchElementException("iterator overflow")
val result = Pixel(i*cell.width, j*cell.height)
i += 1
if (i == rows) { j += 1; i = 0 }
result
}
private def floatingIterator (dim1: Int, dim2: Int, index: (Int, Int) (Int, Int)) =
new Iterator[Pixel] {
private var i, j = 0
def hasNext = i < dim1
def next = {
if (!hasNext) throw new NoSuchElementException("iterator overflow")
val (k, l) = index(i, j)
val result = p0 + Pixel(l*cell.width, k*cell.height)
j += 1
if (j == dim2) { i += 1; j = 0 }
result
}
}
}
......@@ -85,6 +81,9 @@ object GridLayout {
/** Floating from left to right. */
val defaultFloating = Floating.Horizontal
/** Zero point. */
val defaultOrigin = Pixel(0, 0)
/**
* Constructs a new grid layout of the specified number of rows and columns
* using cells of dimensions of the given cell.
......@@ -93,8 +92,9 @@ object GridLayout {
* horizontal (first left to right, then top to bottom) or vertical (vice
* versa) order (defaults to horizontal)
*/
def apply (rows: Int, cols: Int, cell: Cell, floating: Floating = defaultFloating): GridLayout =
new GridLayout(rows, cols, cell, floating)
def apply (rows: Int, cols: Int, cell: Cell, floating: Floating = defaultFloating,
origin: Pixel = defaultOrigin): GridLayout =
new GridLayout(rows, cols, cell, floating, origin)
/**
* Constructs a new grid layout of the specified number of rows and columns
......@@ -107,9 +107,9 @@ object GridLayout {
* @throws ArithmeticException if `rows` or `cols` is zero
*/
def onRectangle (rows: Int, cols: Int, width: Int, height: Int,
floating: Floating = defaultFloating): GridLayout = {
floating: Floating = defaultFloating, origin: Pixel = defaultOrigin): GridLayout = {
val cell = Cell(width/cols, height/rows)
new GridLayout(rows, cols, cell, floating)
new GridLayout(rows, cols, cell, floating, origin)
}
/**
......@@ -122,8 +122,9 @@ object GridLayout {
*
* @throws ArithmeticException if `rows` or `cols` is zero
*/
def onScreen (rows: Int, cols: Int, floating: Floating = defaultFloating): GridLayout = {
onRectangle(rows, cols, Screen.width, Screen.height, floating)
def onScreen (rows: Int, cols: Int, floating: Floating = defaultFloating,
origin: Pixel = defaultOrigin): GridLayout = {
onRectangle(rows, cols, Screen.width, Screen.height, floating, origin)
}
}
......@@ -21,9 +21,10 @@ import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import de.h2b.scala.lib.simgraf.Pixel
import Cell.HorizontalOrientation._
import Cell.VerticalOrientation._
import de.h2b.scala.lib.simgraf.Pixel
@RunWith(classOf[JUnitRunner])
class CellTest extends FunSuite {
......
/*
SimGraf - A Simple Scala Graphics Library
Copyright 2016-2017 Hans-Hermann Bode
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package de.h2b.scala.lib.simgraf.layout
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import de.h2b.scala.lib.simgraf.layout.Floating._
import de.h2b.scala.lib.simgraf.Pixel
@RunWith(classOf[JUnitRunner])
class GridLayoutTest extends FunSuite {
val hlayout = GridLayout.onRectangle(2, 3, 300, 200, origin=Pixel(50, 150))
val vlayout = GridLayout.onRectangle(2, 3, 300, 200, floating=Vertical) at Pixel(50, 150)
test("horizontal iterator") {
val expected = Seq(
Pixel(50,150), Pixel(150,150), Pixel(250,150),
Pixel(50,250), Pixel(150,250), Pixel(250,250))
var i = 0
for (p hlayout) {
assertResult(expected(i))(p)
i += 1
}
}
test("vertical iterator") {
val expected = Seq(
Pixel( 50,150), Pixel( 50,250),
Pixel(150,150), Pixel(150,250),
Pixel(250,150), Pixel(250,250))
var i = 0
for (p vlayout) {
assertResult(expected(i))(p)
i += 1
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment