-
-
Save jinuk17/63cb7d9707d1d6840a2e19d75f88304a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object OOWay extends App { | |
object PenState extends Enumeration { | |
type PenState = Value | |
val Up, Down = Value | |
} | |
object PenColor extends Enumeration { | |
type PenColor = Value | |
val Black, Red, Blue = Value | |
} | |
case class Position(x:Double, y:Double) | |
class Log { | |
def log(line:String) = { Console.println(line) } | |
} | |
def dummyDrawLine(log:Log, oldPos:Position, newPos:Position, color:PenColor.Value) = { | |
log.log(f"...Draw line from (${oldPos.x},${oldPos.y}) to (${newPos.x},${newPos.x}) using $color") | |
} | |
def round2(d:Double):Double = { | |
Math.round(d * 100) / 100.0 | |
} | |
def calcNewPosition(distance:Double)(angle:Double)(currentPos:Position) = { | |
val angleInRads = math.toRadians(angle) | |
val x0 = currentPos.x | |
val y0 = currentPos.y | |
val x1 = x0 + (distance * Math.cos(angleInRads)) | |
val y1 = y0 + (distance * Math.sin(angleInRads)) | |
Position(round2(x1), round2(y1)) | |
} | |
class Turtle(log:Log) { | |
var curPosition: Position = Position(0, 0) | |
var currentAngle: Double = 0.0 | |
var curColor: PenColor.Value = PenColor.Black | |
var curState: PenState.Value = PenState.Down | |
def penUp() = { | |
log.log("Pen Up") | |
curState = PenState.Up | |
} | |
def penDown() = { | |
log.log("Pen Down") | |
curState = PenState.Down | |
} | |
def turn(angle: Double) = { | |
log.log(f"Turn $angle") | |
val newAngle = (currentAngle + angle) % 360.0 | |
currentAngle = newAngle | |
} | |
def setColor(penColor: PenColor.Value) = { | |
log.log(String.format("Set Color : %s", penColor.toString)) | |
this.curColor = penColor | |
} | |
def move(distance: Double) = { | |
log.log(f"Move $distance") | |
val newPosition = calcNewPosition(distance)(currentAngle)(curPosition) | |
if (curState == PenState.Down) { | |
dummyDrawLine(log, curPosition, newPosition, curColor) | |
} | |
curPosition = newPosition | |
} | |
} | |
/* let drawPolygon n = | |
let angle = 180.0 - (360.0/float n) | |
let angleDegrees = angle * 1.0<Degrees> | |
let turtle = Turtle(log) | |
// define a function that draws one side | |
let drawOneSide() = | |
turtle.Move 100.0 | |
turtle.Turn angleDegrees | |
// repeat for all sides | |
for i in [1..n] do | |
drawOneSide()*/ | |
def drawPolygon(n: Int):Unit = { | |
def angle = 180.0 - (360.0/n) | |
val log = new Log() | |
val turtle = new Turtle(log) | |
def drawOneSide():Unit = { | |
turtle move 100.0 | |
turtle turn angle | |
} | |
for ( i <- 1 to n) { | |
drawOneSide() | |
} | |
} | |
val log = new Log() | |
val turtle = new Turtle(log) | |
// | |
// turtle move 100.0 | |
// turtle turn 120.0 | |
// turtle move 100.0 | |
// turtle turn 120.0 | |
// turtle move 100.0 | |
// turtle turn 120.0 | |
drawPolygon(8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment