diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-12-16 18:29:42 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-12-16 18:29:42 +0000 |
commit | df50e05006b43b007c2587549030d24b5c154398 (patch) | |
tree | 9edfb1fb5b8c04350a00c163cfcdb1fccd79e3aa /test-nsc/files/run/Course-2002-06.scala | |
parent | 17e2b1c2a6f69ba74e79c30d1e44195fe732e3e3 (diff) | |
download | scala-df50e05006b43b007c2587549030d24b5c154398.tar.gz scala-df50e05006b43b007c2587549030d24b5c154398.tar.bz2 scala-df50e05006b43b007c2587549030d24b5c154398.zip |
'test-nsc' has been moved to 'test'.
Diffstat (limited to 'test-nsc/files/run/Course-2002-06.scala')
-rw-r--r-- | test-nsc/files/run/Course-2002-06.scala | 262 |
1 files changed, 0 insertions, 262 deletions
diff --git a/test-nsc/files/run/Course-2002-06.scala b/test-nsc/files/run/Course-2002-06.scala deleted file mode 100644 index df73daa08f..0000000000 --- a/test-nsc/files/run/Course-2002-06.scala +++ /dev/null @@ -1,262 +0,0 @@ -//############################################################################ -// Programmation IV - 2002 - Week 06 -//############################################################################ -// $Id$ - -/** Two-dimensional vector. */ -class Vector (_x: Double, _y: Double) { - def x: Double = _x; - def y: Double = _y; - def +(that: Vector): Vector = new Vector(x + that.x, y + that.y); - def *(scalar: Double): Vector = new Vector(x * scalar, y * scalar); - def -(that: Vector): Vector = new Vector(x - that.x, y - that.y); - def /(scalar: Double): Vector = new Vector(x / scalar, y / scalar); - def norm: Double = scala.runtime.compat.Math.sqrt(x * x + y * y); -} - -//############################################################################ - -/** Frame. */ -class Frame (_origin: Vector, _edgeX: Vector, _edgeY: Vector) { - def origin: Vector = _origin; - def edgeX: Vector = _edgeX; - def edgeY: Vector = _edgeY; - /** The vector v in the absolute (drawing) coordinate system */ - def coordMap(v: Vector): Vector = origin + (edgeX * v.x) + (edgeY * v.y); -} - -//############################################################################ - -/** Space on which we can draw lines. */ -abstract class Graphics(_width: Double, _height: Double) { - /** Width of the picture.*/ - def width: Double = _width; - - /** Height of the picture.*/ - def height: Double = _height; - - /** Frame that represents the drawable area of the output device*/ - val frame: Frame; - - /** Draw a line in device coordinates*/ - def plotLine(x1: Double, y1: Double, x2: Double, y2: Double): Unit; - - /** Draw a line in logical coordinates*/ - def drawLine(v1: Vector, v2: Vector): Unit = { - val _v1 = frame.coordMap(v1); - val _v2 = frame.coordMap(v2); - plotLine(_v1.x, _v1.y, _v2.x, _v2.y); - } - - /** Draw a segment of the picture.*/ - def drawSegment(frm: Frame)(v1: Vector, v2: Vector): Unit = { - val _v1 = frm.coordMap(v1); - val _v2 = frm.coordMap(v2); - drawLine(_v1, _v2); - } - - /** Draw a list of segments on the picture.*/ - def drawSegments(frm: Frame)(segments: List[Pair[Vector, Vector]]): Unit = - if (segments.isEmpty) () - else { - drawSegment(frm)(segments.head._1, segments.head._2); - drawSegments(frm)(segments.tail) - } - - /** Draw a list of continuous segments on the picture.*/ - def drawPolySegment(frm: Frame)(points: List[Vector]) : Unit = - if (!points.tail.isEmpty) { - drawSegment(frm)(points.head, points.tail.head); - drawPolySegment(frm)(points.tail); - } - - /** updates the contents of the output device*/ - def repaint = (); - - /** Add the last touch to the picture.*/ - def close : Unit; -} - -//############################################################################ - -/** Provides PostScript output. The name of the file is the first parameter - * of the constructor. The width and height determine the aspect ratio - */ -class PostScript (filename: String, _width: Double, _height: Double) - extends Graphics(_width, _height) { - /** Convert mm into 72th of inch.*/ - def mm2ps(x: Double) : Double = round(x * 72.0 / 25.4); - - def round(x: Double): Double = - scala.runtime.compat.Math.floor(x * 100.0 + 0.5) / 100.0; - - def scaleAndCenter(frm: Frame, ratio:Double): Frame = { - val currentRatio = frm.edgeX.norm / frm.edgeY.norm; - if (currentRatio < ratio) { - val newEdgeX = frm.edgeX; - val newEdgeY = frm.edgeY * (currentRatio /ratio); - val newOrigin = frm.origin + ((frm.edgeY - newEdgeY) / 2); - new Frame(newOrigin, newEdgeX, newEdgeY) - } - else { - val newEdgeX = frm.edgeX * (ratio / currentRatio); - val newEdgeY = frm.edgeY; - val newOrigin = frm.origin + ((frm.edgeX - newEdgeX) / 2); - new Frame(newOrigin, newEdgeX, newEdgeY) - } - } - - /** Line thickness in millimeters.*/ - val line_thickness : Double = 0.05; - - /** Width, height, left and right margins in mm.*/ - val psWidth: Double = 210.0; - val psHeight: Double = 297.0; - val psWidthMargin: Double = 15.0; - val psHeightMargin: Double = 15.0; - - val frame: Frame = { - val origin = new Vector(mm2ps(psWidthMargin), mm2ps(psHeightMargin)); - val edgeX = new Vector(mm2ps(psWidth) - 2 * mm2ps(psWidthMargin), 0); - val edgeY = new Vector(0, mm2ps(psHeight) - 2 * mm2ps(psHeightMargin)); - scaleAndCenter(new Frame(origin, edgeX, edgeY), width / height) - } - - def plotLine(x1: Double, y1: Double, x2: Double, y2: Double): Unit = { - Console.println(round(x1) + " " + round(y1) + " m " + - round(x2) + " " + round(y2) + " l"); - } - - /** Print the PS header.*/ - Console.println("%!PS-Adobe-3.0 EPSF-3.0\n%%Title: ProgrammationIV"); - Console.println("%%Creator: LAMP"); - Console.println("%%BoundingBox: 0 0 " + mm2ps(psWidth) + " " + mm2ps(psHeight)); - Console.println("%%EndComments\n"); - Console.println("/m {moveto} bind def\n/l {lineto} bind def\n"); - Console.println(mm2ps(line_thickness) + " setlinewidth\nnewpath"); - - /** Terminate the PS document and close the file stream. */ - def close : Unit = { - Console.println("stroke\nshowpage\n%%EOF"); - Console.flush; - } -} - -//############################################################################ - -object M0 { - - /** Define the type of a painter as a function that takes a frame, - * draws itself onto it and returns nothing - */ - type Painter = (Frame) => Unit; - - - /** Transform the frame in which the painter is to be drawn, hence - * changing the appearance of the painter - */ - def transformPainter(origin: Vector, newX: Vector, newY: Vector)(painter: Painter): Painter = { - frame: Frame => { - val newOrigin = frame.coordMap(origin); - val newFrame = new Frame(newOrigin, - frame.coordMap(newX) - newOrigin, - frame.coordMap(newY) - newOrigin); - painter(newFrame) - } - } - - - /** Flip the painter vertically - */ - def flipVert: Painter => Painter = - transformPainter(new Vector(0.0, 1.0), - new Vector(1.0, 1.0), - new Vector(0.0, 0.0)); - - /** Flip the painter horizontally - */ - def flipHoriz: Painter => Painter = - transformPainter(new Vector(1.0, 0.0), - new Vector(0.0, 0.0), - new Vector(1.0, 1.0)); - - /** Compose a painter that draws p1 on the left of p2 - */ - def beside(p1: Painter, p2: Painter) : Painter = { - frame: Frame => { - transformPainter(new Vector(0.0, 0.0), - new Vector(0.5, 0.0), - new Vector(0.0, 1.0))(p1)(frame); - transformPainter(new Vector(0.5, 0.0), - new Vector(1.0, 0.0), - new Vector(0.5, 1.0))(p2)(frame) - } - } - - /** Compose a painter that draws p1 below p2 - */ - def below(p1: Painter, p2: Painter): Painter = { - frame: Frame => { - transformPainter(new Vector(0.0, 0.0), - new Vector(1.0, 0.0), - new Vector(0.0, 0.5))(p1)(frame); - transformPainter(new Vector(0.0, 0.5), - new Vector(1.0, 0.5), - new Vector(0.0, 1.0))(p2)(frame) - } - } - - def rightSplit(painter: Painter, n: Int): Painter = { - if (n == 0) painter - else { - val smaller = rightSplit(painter, n-1); - beside(painter, below(smaller, smaller)) - } - } - - // A small test painter. - def house(canvas: Graphics)(frame: Frame): Unit = { - canvas.drawPolySegment(frame)(List(new Vector(0.0, 0.0), - new Vector(1.0, 0.0), - new Vector(1.0, 2.0/3.0), - new Vector(0.0, 2.0/3.0), - new Vector(0.5, 1.0), - new Vector(1.0, 2.0/3.0), - new Vector(0.0, 0.0), - new Vector(0.0, 2.0/3.0), - new Vector(1.0, 0.0))); - canvas.repaint - } - - def test = { - val psfile = "-"; - val canvas: Graphics = new PostScript(psfile, 2, 2); - - // the identity frame - val identFrame = new Frame(new Vector(0.0,0.0), - new Vector(1.0,0.0), - new Vector(0.0,1.0)); - - // Create a basic painter... - val p: Painter = house(canvas); - // ...then compose it with itself. - val threeHouses = beside(p, beside(p, flipVert(p))); - - // Use the painter to draw the final image. - threeHouses(identFrame); - - // Don't forget to close the canvas! - canvas.close - } -} - -//############################################################################ - -object Test { - def main(args: Array[String]): Unit = { - M0.test; - () - } -} - -//############################################################################ |