Under construction

Still being built. Things may change

Autocorrelation Function

Interview

Time limit8sMemory limit512 MB

Summary
A piecewise linear function is given by its endpoints; compute the integral of f(x)f(x+r) over the whole line for a given shift r.
Level

Medium5 of 10

Topics
Math, Geometry, Implementation, Simulation
Solved
No attempts yet

Problem

Autocorrelation measures how much a signal matches a shifted copy of itself. For a continuous function f(x)f(x) defined on the whole real line, the autocorrelation function Rf(r)R_f(r) is

Rf(r)=∫−∞∞f(x) f(x+r) dxR_f(r) = \int_{-\infty}^{\infty} f(x)\,f(x + r)\,dx

where rr is a real number.

Every function in this problem is piecewise linear and continuous. It is given by nn endpoints (x1,y1)(x_1, y_1) through (xn,yn)(x_n, y_n) with distinct xx-coordinates, and consecutive endpoints are joined by a straight segment. The first and the last endpoint have yy-coordinate 0, and ff is 0 outside the interval [x1,xn][x_1, x_n]. The figure below shows one such function.

An example of a given function

Given ff and rr, compute Rf(r)R_f(r).

Input

The input holds several data sets.

The first line of each data set has an integer nn (3≤n≤1003 \le n \le 100) and a real number rr (−100≤r≤100-100 \le r \le 100). Here nn is the number of endpoints that form ff, and rr is the parameter of the autocorrelation function. Each of the next nn lines has two integers xix_i (−100≤xi≤100-100 \le x_i \le 100) and yiy_i (−50≤yi≤50-50 \le y_i \le 50), the coordinates of the ii-th endpoint. The endpoints come in increasing order of xx-coordinate, and no two endpoints share an xx-coordinate. The first and the last endpoint always have yy-coordinate 0.

A line with nn and rr both equal to 0 ends the input. That line is not a data set, so do not process it.

Output

For each data set, print the value of Rf(r)R_f(r) on its own line. Round the value at the seventh digit after the decimal point and print exactly six digits after the decimal point. If the value is 0, print 0.000000 and not -0.000000.

Examples2

  1. Example 1

    Input
    3 1
    0 0
    1 1
    2 0
    11 1.5
    0 0
    2 7
    5 3
    7 8
    10 -5
    13 4
    15 -1
    17 3
    20 -7
    23 9
    24 0
    0 0
    
    Expected output
    0.166667
    165.213542
    
  2. Example 2

    Input
    3 0
    0 0
    1 1
    2 0
    0 0
    
    Expected output
    0.666667