### dist(ance);

Friday, February 29th, 2008

run the sketchâ€”view the code

**How do you use it?**

Click and drag.

**What is it?**

A drawing program. The circle diameter is based on the speed of the mouse. It turned into an ironic programming lesson; I spent a bunch of time working out the algebra and logic for determining the distance between two points, then discoveredâ€”by chanceâ€”that there was already a function that did the same thing. See below…

**Why is it cool?**

It turns out that all of this…

`if(pmouseY>mouseY){`

if(pmouseX>mouseX){

` `

` `

`dX=pmouseX-mouseX;`

` `

` `

`dY=pmouseY-mouseY;`

`}`

` `

`if(mouseX>pmouseX){`

` `

` `

`dX=mouseX-pmouseX;`

` `

` `

`dY=pmouseY-mouseY;`

` `

`}`

}

if(mouseY>pmouseY){

` `

`if(pmouseX>mouseX){`

` `

` `

` dX=pmouseX-mouseX;`

` `

` `

` dY=mouseY-pmouseY;`

` `

` }`

` `

` if(mouseX>pmouseX){`

` `

` `

` dX=mouseX-pmouseX;`

` `

` `

` dY=mouseY-pmouseY;`

` `

` }`

}

distance=sqrt(sq(dX)+sq(dY));

Can be done with just this…

`distance=dist(pmouseX, pmouseY, mouseX, mouseY); `