Commit ba6ce9e6 authored by h2b's avatar h2b

Experimental parallel set-pixels operation added.

parent 76431736
......@@ -17,6 +17,8 @@
*/
package de.h2b.scala.lib.simgraf
import scala.concurrent.Future
import de.h2b.scala.lib.simgraf.driver.{ AwtScreenDriver, ScreenDriver }
/**
......@@ -144,6 +146,36 @@ abstract class Screen (override val p0: Pixel, override val width: Int,
def setPixels (col: Pixel Color): Unit =
setPixels(Pixel(0,0), Pixel(width-1,height-1))(col)
/**
* Sets all pixels in a rectangle with diagonal opposite corners at `p1` and `p2`
* to a computed color using parallel operations.
*
* @param p1
* @param p2
* @param col computing function that maps pixel coordinates to colors
*/
def setPixelsParallel (p1: Pixel, p2: Pixel) (col: Pixel Color): Unit = {
val left = p1.x min p2.x
val right = p1.x max p2.x
val bottom = p1.y min p2.y
val top = p1.y max p2.y
for (y bottom to top) Future {
for (x left to right) {
val p = Pixel(x,y)
setPixel(p, col(p))
}
}
}
/**
* Sets all pixels of the screen to a computed color using parallel
* operations.
*
* @param col computing function that maps pixel coordinates to colors
*/
def setPixelsParallel (col: Pixel Color): Unit =
setPixelsParallel(Pixel(0,0), Pixel(width-1,height-1))(col)
/**
* Sets all pixels in a rectangle with diagonal opposite corners at `p1` and `p2`
* to a specified color.
......
......@@ -17,6 +17,8 @@
*/
package de.h2b.scala.lib
import scala.concurrent.ExecutionContext
/**
This is a Scala library of graphics utilities. It is focused on simple drawing
of shapes and functions without the need of instructing the graphics system by
......@@ -87,4 +89,8 @@ general shape-oriented `draw` and `fill` operations as `World` has to offer,
but only some primitives like `setPixel`, `drawLine`, `drawSquare`,
`fillSquare`, `moveTo` or `drawTo`.
*/
package object simgraf
package object simgraf {
private[simgraf] implicit lazy val packageExecutionContext: ExecutionContext = ExecutionContext.global
}
......@@ -65,13 +65,19 @@ object ScreenDemo extends App {
{
n +=1
val screen = Screen(Pixel(900,100), w, h, s"Screen Demo $n")
val screen = Screen(Pixel(100,400), w, h, s"Screen Demo $n")
screen.setPixels(_ => Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256)))
}
{
n +=1
val screen = Screen(Pixel(500,400), w, h, s"Screen Demo $n")
screen.setPixelsParallel(_ => Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256)))
}
{
n +=1
val screen = Screen(Pixel(900,100), w, h, s"Screen Demo $n")
screen.clear(Color.White)
screen.activeColor = Color.Black
val x1 = 50
......
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