I want to calculate the distance of a point from a line defined by 2 points.

I am using javascript and thats what I came up with using wikipedia: <https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line>

```
function distance(point1, point2, x0, y0) {
    return ((Math.abs((point2.y - point1.y) * x0 - 
                     (point2.x - point1.x) * y0 + 
                     point2.x * point1.y - 
                     point2.y * point1.x)) /
            (Math.pow((Math.pow(point2.y - point1.y, 2) + 
                       Math.pow(point2.x - point1.x, 2)), 
                      0.5)));
}

```

The problem is that It doesn't seem accurate since if I enter these parameters : 

`alert(distance({ x: 1, y: 1 }, { x: 2, y: 2 }, 1, 0));`

It returns `1/sqrt(2)` instead of returning `1` (which is the distance between the point `(1, 0)` and the line at point `(1, 1)`

EDIT : I understand the code above does not do what I wanted it to do. It caulcated from a point to a line represented by 2 point but the line is INFINITE (I wanted something more like a vector which has 2 end-point)

I found the answer [here](https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment)