exps/Attic/Element.scala
author Chengsong
Sun, 06 Feb 2022 16:44:17 +0000
changeset 417 a2887a9e8539
parent 310 c090baa7059d
permissions -rwxr-xr-x
rdersetc.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
304
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     1
import Element.elem
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     2
abstract class Element{
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     3
  def contents: Array[String]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     4
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     5
  def height: Int = contents.length
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     6
  def width: Int = contents(0).length
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     7
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     8
  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
     9
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    10
  def above(that: Element): Element = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    11
    //new ArrayElement(this.contents ++ that.contents)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    12
    val this1 = this widen that.width
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    13
    val that1 = that widen this.width
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    14
    elem(this1.contents ++ that1.contents)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    15
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    16
  def left_align(that: Element): Element = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    17
    if (this.width == that.width){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    18
      this above that
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    19
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    20
    else if (this.width < that.width) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    21
      (this beside elem(' ', that.width - this.width, this.height)) above that
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    22
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    23
    else {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    24
      this above (that beside elem(' ', this.width - that.width, that.height))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    25
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    26
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    27
  def up_align(that: Element): Element = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    28
    if (this.height == that.height){
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    29
      this beside that
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    30
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    31
    else if (this.height < that.height) {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    32
      (this above elem(' ', this.width, that.height - this.height)) beside that
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    33
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    34
    else {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    35
      this beside (that above elem(' ', that.width, this.height - that.height))
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    36
    }  
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    37
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    38
  def beside(that: Element): Element = {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    39
    val this1 = this heighten that.height
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    40
    val that1 = that heighten this.height
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    41
    elem(
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    42
      for ((line1, line2) <- this1.contents zip that1.contents)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    43
      yield line1 + line2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    44
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    45
  def widen(w: Int): Element = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    46
    if(w <= width) this
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    47
    else {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    48
      val left = Element.elem(' ', (w - width) / 2, height)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    49
      var right = Element.elem(' ', w - width - left.width, height)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    50
      left beside this beside right
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    51
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    52
  def heighten(h: Int): Element = 
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    53
    if (h <= height) this
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    54
    else {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    55
      val top = Element.elem(' ', width, (h - height) / 2)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    56
      val bot = Element.elem(' ', width, h - height - top.height)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    57
      top above this above bot
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    58
    }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    59
  override def toString = contents mkString "\n"
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    60
}
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    61
object Element {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    62
  private class ArrayElement(
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    63
    val contents: Array[String]
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    64
  ) extends Element
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    65
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    66
  private class LineElement(s: String) extends Element {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    67
    val contents = Array(s)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    68
    override def width = s.length
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    69
    override def height = 1
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    70
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    71
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    72
  private class UniformElement(
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    73
    ch: Char,
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    74
    override val width: Int,
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    75
    override val height: Int
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    76
  ) extends Element {
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    77
    private val line = ch.toString * width
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    78
    def contents = Array.fill(height)(line)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    79
  }
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    80
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    81
  def elem(contents: Array[String]): Element =
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    82
    new ArrayElement(contents)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    83
    
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    84
  def elem(chr: Char, width: Int, height: Int): Element =
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    85
    new UniformElement(chr, width, height)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    86
    
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    87
  def elem(line: String): Element =
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    88
      new LineElement(line)
82a99eec5b73 3 files to be compiled together and then run scala Spiral a b
Chengsong
parents:
diff changeset
    89
}