Encircling Circles

Time limit1sMemory limit128 MB

Summary
Given n circles and a radius r, find the length of the boundary of the union of all radius-r circles that enclose every given circle.
Level

Medium7 of 10

Topics
Geometry, Math, Implementation, Brute force
Solved
No attempts yet

Problem

You are given a set CC of circles with various radii placed at various positions on the plane; the circles may overlap one another. If a circle of radius rr is placed at a suitable position and rr is large enough, that circle can completely enclose every circle in CC.

There may be more than one position at which a circle of radius rr encloses all circles of CC. Let UU be the union of the areas covered by the enclosing circles over all such positions. In other words, for every point in UU there exists at least one circle of radius rr that encloses both that point and every circle of CC. Your task is to compute the length of the boundary of region UU.

Figure I.1 shows an example of the set CC and the region UU. The three solid circles belong to CC, the dashed circles show some of the possible positions of the enclosing circle, and the thick dashed closed curve bounds the region UU.

Figure I.1: Example of the Circle Set

Input

The input is a sequence of datasets. The number of datasets is less than 100100. Each dataset has the following format.

n r
x1 y1 r1
x2 y2 r2
...
xn yn rn

The first line of a dataset contains two positive integers nn and rr separated by a single space. nn is the number of circles in CC and does not exceed 100100; rr is the radius of the enclosing circle and does not exceed 10001000.

Each of the following nn lines contains three integers separated by single spaces. (xi,yi)(x_i, y_i) is the center of the ii-th circle of CC and rir_i is its radius. You may assume −500≤xi≤500-500 \le x_i \le 500, −500≤yi≤500-500 \le y_i \le 500, and 1≤ri≤5001 \le r_i \le 500.

The end of the input is indicated by a line containing two zeros separated by a single space.

Output

For each dataset, output on one line the length of the boundary of region UU, rounded to exactly two digits after the decimal point (for example, 81.68). If rr is too small to enclose every circle in CC (that is, no valid position exists), output a line containing only 0.00. Output nothing else.

The inputs are chosen so that the rounded value is unambiguous.

Hint

Figure I.2: An illustration of the last dataset

Examples2

  1. Example 1

    Input
    1 10
    5 5 7
    2 12
    5 5 7
    8 6 3
    3 10
    3 11 2
    2 1 1
    2 16 3
    3 15
    -5 2 5
    9 2 9
    5 8 6
    3 38
    -25 -10 8
    30 5 7
    -3 35 11
    3 39
    -25 -10 8
    30 5 7
    -3 35 11
    3 800
    -400 400 2
    300 300 1
    300 302 1
    3 800
    400 -400 2
    300 300 1
    307 300 3
    8 147
    130 80 12
    130 -40 12
    -110 80 12
    -110 -40 12
    70 140 12
    70 -100 12
    -50 140 12
    -50 -100 12
    3 493
    345 154 10
    291 111 75
    -275 -301 46
    4 55
    54 0 1
    40 30 5
    27 36 10
    0 48 7
    3 30
    0 3 3
    -3 0 4
    400 0 3
    3 7
    2 3 2
    -5 -4 2
    -4 3 2
    3 10
    -5 -4 5
    2 3 5
    -4 3 5
    4 6
    4 6 1
    5 5 1
    1 7 1
    0 1 1
    3 493
    345 154 10
    291 111 75
    -275 -301 46
    5 20
    -9 12 5
    0 15 5
    3 -3 3
    12 9 5
    -12 9 5
    0 0
    
    Expected output
    81.68
    106.81
    74.11
    108.92
    0.00
    254.86
    8576.94
    8569.46
    929.20
    4181.12
    505.09
    0.00
    46.82
    65.67
    50.99
    4181.12
    158.88
    
  2. Example 2

    Input
    1 20
    0 0 5
    0 0
    
    Expected output
    219.91