Skip to content

Instantly share code, notes, and snippets.

@abhin4v
Created September 22, 2010 20:08
Show Gist options
  • Select an option

  • Save abhin4v/592475 to your computer and use it in GitHub Desktop.

Select an option

Save abhin4v/592475 to your computer and use it in GitHub Desktop.
import scala.math.{sin, cos, abs, round, ceil, Pi}
import scala.collection.mutable.Buffer
import scala.util.Random.nextPrintableChar
case class Point(x: Int, y: Int)
class Star(val spikes: Int, val kind: Int, val diameter: Int) {
lazy val vertices: Seq[Point] = {
val radius = diameter/2.0
for {
i <- 0 to (spikes - 1)
angle = i / spikes.toFloat * 2 * Pi
} yield Point(round(radius * (1 + cos(angle))).toInt,
round(radius * (1 + sin(angle))).toInt)
}
private def line(p0: Point, p1: Point): Seq[Point] = {
val (x0, y0) = Point.unapply(p0).get
val (x1, y1) = Point.unapply(p1).get
val steep = abs(y1 - y0) > abs(x1 - x0)
val (x0A, y0A, x1A, y1A) =
if (steep) (y0, x0, y1, x1) else (x0, y0, x1, y1)
val (x0P, y0P, x1P, y1P) =
if (x0A > x1A) (x1A, y1A, x0A, y0A) else (x0A, y0A, x1A, y1A)
val dx = x1P - x0P
val dy = abs(y1P - y0P)
val ys = if (y0P < y1P) 1 else -1
var err = dx/2
var y = y0P
val points = Buffer[Point]()
for (x <- x0P to x1P) {
points += (if (steep) Point(y, x) else Point(x, y))
err = err - dy
if (err < 0) {
y = y + ys
err = err + dx
}
}
points.toList
}
lazy val lines: Seq[Seq[Point]] =
(vertices, (vertices ++ vertices).drop(kind).take(spikes)).zipped.map(line)
def plot(lineSym: String, spaceSym: String) {
val size = diameter + 1
val grid = Array.ofDim[String](size, size)
for (i <- Range(0, size); j <- Range(0, size)) {
grid(i)(j) = spaceSym
}
for (line <- lines; point <- line) {
grid(point.y)(point.x) = lineSym
}
println(grid map { _.mkString } mkString "\n")
}
def plot {
val size = diameter + 1
val grid = Array.ofDim[String](size, size)
for (i <- Range(0, size); j <- Range(0, size)) {
grid(i)(j) = " "
}
for (line <- lines; point <- line) {
grid(point.y)(point.x) = nextPrintableChar.toString +
nextPrintableChar.toString
}
println(grid map { _.mkString } mkString "\n")
}
}
object Star {
def apply(spikes: Int, kind: Int, diameter: Int) =
new Star(spikes, kind, diameter)
}
scala> Star(5, 3, 35).plot(">>", " ")
>>
>>>>
>>>>
>> >>
>> >>
>> >>
>>>> >> >>
>>>>>>>> >> >>
>> >>>>>> >> >>
>> >>>>>> >> >>
>> >>>>>> >>
>> >> >>>>>> >>
>> >> >>>> >>
>> >> >>>>>>
>> >> >> >>>>>>
>> >> >> >>>>>>
>>>> >> >>>>>>
>>>> >> >>>>
>> >> >> >>>>>>
>> >> >> >>>>>>
>> >> >>>>>>>>>>
>> >> >>>>>>
>> >> >>>>>> >>
>> >>>>>> >>
>> >>>>>> >> >>
>> >>>>>>>> >> >>
>>>>>>>> >> >>
>>>> >> >>
>> >>
>> >>
>> >>
>>>>
>>>>
>>
scala> Star(5, 3, 35).plot
0>
|1L%
6@Tf
55 Gm
c_ Rb
p% }>
#5Pn ]> 8#
pvXlG"TM K+ e{
CT fI,mhB ,d cz
9q 1+?mEr U, Na
.O 1LOEnO |q
Fw D0 I*a*+$ Ia
nw [; qA2e |L
1c [V &edCQ9
Ew #[ G' fnz=_:
pR ,Z oQ NGn{Tf
f|?V }i tx*No)
-\5d )4 m9]G
oK xg 6p Ld>z!U
@> Gx kr S[=p|{
hS (} *+E<QO$YKw
'+ 0r !2C2bn
og dc @.%K!{ tI
9A UiM0|t ].
<( @$L&SS VR Gr
An ^OrUJ]Q{ b< 8-
}&HZ*s"M Ru zS
3Zs> QS YY
cm A(
sj 7?
o' 0h
Z=J2
X(fr
B7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment