Problem:
Problem E: Legend of Storia

In Storia Kingdom, there is an artifact called "Perfect Sphere" made by a great meister Es. This is handed down for generations in the royal palace as a treasure. Es left a memo and filled mysterious values in it. Within achievements of researches, it has been revealed that the values denote coordinate values. However it is still unknown the definition of the coordinate system.

By the way, you are a person who lived in age of Es lived. The values which meister Es left are defined by following. Es set an moving object in the artifact sphere. Surprisingly, that is rigid body and perpetual organization. The object rotates inside the sphere and it is always touching the inside surface of the sphere. This value is a coordinate of the tangent point by the object and the sphere, but it is not a three dimensional value. The object is a plate and this is a simple polygon. Here, "simple" means "non self-intersected". This rotates while touching a great circle of the sphere. Here, assume that the great circle is a circle O centered in the origin in the xy plane. (It is two-dimensional Cartesian coordinate system, the direction of positive x-value is right and the direction of positive y-value is up). This plate touches the circle O with a single point at the beginning of the movement. And it rotates in counterclockwise as the current touching point is the rotation fulcrum. When another point X of the plate touches the circle O newly, the plate continue to rotate as described above, but in the next rotation, the rotation fulcrum point is changed. This will be the point X. If two or more points can be interpreted as the point X, the point X is a most distant point from the currently touching point X'. Here, that distance is the length of the arc of the circle O from the point X' to the point X in clockwise.

Es asked you, an excellent assistant, to write a program that enumerates Q tangent points (fulcrum points while the movement) by the plate and the circle O while the movement of the plate described above in the chronological order. Of course, one and the previous one must be distinct points. Following figures show you examples of the movement of the plate inside the circle O.

1

2

3

4

Input

Input consists of multiple datasets.

A dataset is given in a following format.

N R Q
x1 y1
x2 y2
...
xN yN

N, R, Q are integers that represent the number of points of the plate, the radius of the circle, and the number of points Es needs respectively.

Following N lines contain 2N integers that represent coordinates of the plate's points ( xk , yk ) in the circle.(1≤k≤N)

You can assume following conditions.

Points of the plate are given in counterclockwise.

No three points are co-linear.

N-1 points of the plate are given inside of the circle (distances between the center of the circle and these points are less than R). And 1 point is on the circle edge.

N=R=Q=0 shows the end of input.

Constraints

The number of datasets is less than or equal to 400.

3≤N≤25

1≤R≤1000

0≤Q≤100

Output

For each dataset, output (Px Py:real number) in Q in the following format.

Px1 Px1
Px2 Py2
...
PxN+1 PyN+1

These answers must satisfy |Txi-Pxi|≤0.0001 and |Tyi-Pyi|≤0.0001.

Here, Pxi, Pyi(1≤i≤Q) are your output and Txi,Tyi(1≤i≤Q) are judge's output.

Sample Input

4 10 8
0 -10
3 -7
0 -4
-3 -7
0 0 0

Output for the Sample Input

-4.146082488326 -9.100000000000
-7.545870128753 -6.562000000000
-9.587401146004 -2.842840000000
-9.903199956975 1.388031200000
-8.436422775690 5.369056784000
-5.451089494781 8.383652146880
-1.484560104812 9.889190123322
2.749190104024 9.614673877565

Solution in Scala (judge verdict: Accepted)

object Main extends App {
  import scala.io.StdIn._

  val efs = 0.0000001


  solve
  def solve: Unit ={
    val Array(n, r, q) = readLine.trim.split(' ').map(_.toInt)
    if (n == 0) return
    val circle = Circle(Point(0, 0), r)
    val convex = Array.tabulate(n){_ ⇒
      val Array(x, y) = readLine.trim.split(' ').map(_.toInt)
      Point(x, y)
    }
    var current = convex.indices.find(p ⇒ math.abs(convex(p).distanceFrom(circle.center) - circle.radius) < efs).get
    var angle = Vector(circle.center, convex(current)).angle
    for (_ ← 0 until q){
      val next = findNext(convex, circle, current)
      angle -= circle.angleByChord(convex(current).distanceFrom(convex(next)))
      current = next
      println(s"${angle.cos * r} ${angle.sin * r}")
    }
    solve
  }
  def findNext(convex: Array[Point], circle: Circle, current: Int): Int = {
    //convex(current)がcircleの円周上にある場合に時計回りで次に接するconvex上の点
    //circle上の点で、ここにconvex(current)があるとする
    val basePosition = Vector(convex(current), circle.center).let(v ⇒ circle.center + Vector(v.x / v.norm * circle.radius, v.y / v.norm * circle.radius))
    //basePositionから見たcircle.centerの角度
    val baseAngle = Vector(basePosition, circle.center).angle
    //convex上の点をconvex(current)がbasePointに来るようにシフトしたもの
    val shifted = convex.map(c ⇒ c - convex(current) + basePosition)
    //求める点の候補
    var suspect = (current + 1) % convex.length
    var fromAngle = Vector(basePosition, shifted(suspect)).angle
    //Edge(basePoint, shifted(suspects)) がcircle上の時計回りの弦であるときの basePointから見たconvex(suspect)の角度。toAngle - fromAngleが回転すべき角度
    var toAngle = Angle.byCos(basePosition.distanceFrom(shifted(suspect)) / 2 / circle.radius) + baseAngle
    while (!shifted.map(c ⇒ c.rotateWith(basePosition, toAngle - fromAngle)).forall(p ⇒ circle.contains(p))){
      suspect = (suspect + 1) % convex.length
      fromAngle = Vector(basePosition, shifted(suspect)).angle
      toAngle = Angle.byCos(basePosition.distanceFrom(shifted(suspect)) / 2 / circle.radius) + baseAngle
    }
    suspect
  }

  object Vector{
    def apply(from: Point, to: Point): Vector = (to - from).let(p ⇒ Vector(p.x, p.y))
  }
  case class Vector(x: Double, y: Double) {
    def norm: Double = math.sqrt(x * x + y * y)
    def angle: Angle = Angle(this)
    def rotate(angle: Angle): Vector = Vector(x * angle.cos - y * angle.sin, x * angle.sin + y * angle.cos)
  }
  case class Point(x: Double, y: Double) {
    def distanceFrom(that: Point): Double = math.sqrt((x - that.x) * (x - that.x) + (y - that.y) * (y - that.y))
    def +(that: Point): Point = Point(x + that.x, y + that.y)
    def +(vector: Vector): Point = Point(x + vector.x, y + vector.y)
    def -(that: Point): Point = Point(x - that.x, y - that.y)
    def rotateWith(origin: Point, angle: Angle): Point = origin + Vector(origin, this).rotate(angle)
  }
  object Angle {
    def apply(vector: Vector): Angle = {
      new Angle(vector.y / vector.norm, vector.x / vector.norm)
    }
    def byCos(cos: Double): Angle = {
      new Angle(math.sqrt(1 - cos * cos), cos)
    }
    def bySin(sin: Double): Angle = {
      new Angle(sin, math.sqrt(1 - sin * sin))
    }
  }
  class Angle private (val sin: Double, val cos: Double) {
    def +(that: Angle): Angle = new Angle(sin * that.cos + cos * that.sin, cos * that.cos - sin * that.sin)
    def -(that: Angle): Angle = new Angle(sin * that.cos - cos * that.sin, cos * that.cos + sin * that.sin)
    def complementaryAngle: Angle = new Angle(cos, sin)
  }
  case class Edge(from: Point, to: Point) {
    def length: Double = from.distanceFrom(to)
  }
  case class Circle(center: Point, radius: Double) {
    def contains(point: Point): Boolean = center.distanceFrom(point) <= radius + efs
    def angleByChord(chord: Double): Angle = {
      Angle.bySin(chord / 2 / radius).let(a ⇒ a + a)
    }
  }
  implicit class Scope[T](val value: T) extends AnyVal{
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](func: T ⇒ A): A = func(value)
  }
}