zoukankan      html  css  js  c++  java
  • 10 绘制螺旋示例

    1.Element类和伴生对象

    import Element.elem
    
    abstract class Element {
      def contents: Array[String]
    
      def height: Int = contents.length
    
      def  Int = if (height == 0) 0 else contents(0).length
    
      def above(that: Element): Element = {
        val this1 = this widen that.width
        val that1 = that widen this.width
        elem(this1.contents ++ that1.contents)
      }
    
      def beside(that: Element): Element = {
        val this1 = this heighten that.height
        val that1 = that heighten this.height
        val contents = new Array[String](this.contents.length)
        elem(for ((line1, line2) <- this1.contents zip that1.contents) yield line1 + line2)
      }
    
      def widen(w: Int): Element = {
        if (w <= width) this
        else {
          val left = elem(' ', (w - width) / 2, height)
          val right = elem(' ', w - width - left.width, height)
          left beside this beside right
        }
      }
    
      def heighten(h: Int): Element = {
        if (h <= height) this
        else {
          val top = elem(' ', width, (h - height) / 2)
          val bot = elem(' ', width, h - height - top.height)
          top above this above bot
        }
      }
    
      override def toString: String = contents mkString "
    "
    
    }
    
    object Element{
    
      class ArrayElement(val contents: Array[String]) extends Element
    
      class LineElement(s: String) extends Element {
        override def contents: Array[String] = Array(s)
    
        override val  Int = s.length
    
        override val height: Int = 1
      }
    
      class UniformElement(ch: Char,override val  Int, override val height: Int) extends Element {
    
        private val line: String = ch.toString * width
    
        override val contents: Array[String] = Array.fill(height)(line)
    
      }
    
      def elem(contents: Array[String]): Element = new ArrayElement(contents)
    
      def elem(chr: Char,  Int, height: Int): Element = new UniformElement(chr, width, height)
    
      def elem(line: String):Element = new LineElement(line)
    
    }
    

    2.Spiral对象

    import Element.elem
    
    object Spiral {
    
      val space = elem(" ")
      val corner = elem("+")
    
      def spiral(nEdges: Int, direction: Int): Element = {
    
        if (nEdges == 1)
          elem("+")
        else {
          val sp = spiral(nEdges - 1, (direction + 3) % 4)
          def verticalBar = elem('|', 1, sp.height)
          def horizontalBar = elem('-', sp.width, 1)
          if (direction == 0)
            (corner beside horizontalBar) above (sp beside space)
          else if (direction == 1)
            (sp above space) beside (corner above verticalBar)
          else if (direction == 2)
            (space beside sp) above (horizontalBar beside corner)
          else
            (verticalBar above corner) beside (space above sp)
        }
      }
    
      def main(args: Array[String]): Unit = {
        val nSides = 11
        println(spiral(nSides, 0))
      }
      
    }
    

    3.打印示例

    +----------
    |          
    | +------+ 
    | |      | 
    | | +--+ | 
    | | |  | | 
    | | ++ | | 
    | |    | | 
    | +----+ | 
    |        | 
    +--------+ 
  • 相关阅读:
    HTML5之Canvas绘图——使用Canvas绘制图形的基本教程
    搭建EF6.0+MVC4搭建框架——之路由配置
    搭建EF6.0+MVC4搭建框架遇到的问题及解决方案
    Sql2008 全文索引应用(错误7625)
    Excel 导入并导出结果集
    SqlParameter设定的value值为0时、调用的存储过程获取到的值却为null解决方法
    JavaScript中知而不全的this (转)
    JavaScript 类定义常用方法(转)
    NPOI大数据量多个sheet导出源码(原)
    网页右下角固定图片(不随滚动条滑动,始终在固定位置)
  • 原文地址:https://www.cnblogs.com/noyouth/p/14082299.html
Copyright © 2011-2022 走看看