One that should help is that distance should be:

1
| float distance = (float) Math.sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ); |

To calculate the distance needed, try this:

1 2 3 4 5
| float width = Math.abs(p1.x - p2.x) + PADDING; float camx = (float) ( (p1.x + p2.x) * .5); float camy = (float) ( (p1.y + p2.y) * .5);
float camz = (float) (.5 * width * Math.tan(.5 * fov)); |

The only things I've added are PADDING (which would be a constant that just affects how much space between the edges of the screen you want), and the variable fov. fov is the field of view angle that you're using when rendering. If you're using JOGL, it's one of the values used in gluPerspective(), probably 60 degrees.