I've been spending weeks trying to pull this off. I have no problems getting the regular distance of a point and an infinite line, regular distance of a point and a segment, and a signed distance of an infinite line. But I tried every conceivable method I got and could not find any approach on getting the signed distance of a segment. The only thing I could do is cheat it just to get the sign of the result of the signed distance of a point and a line, and slap the sign onto the regular distance of the point and the segment. It works, but its not a true mathematical approach. Is there a special trick with vector math to pull this off without cheating it? I tried Googling this for weeks and couldn't find a single way. Here is what I got:

Code:

public float DistanceFromLinef(vertex2d a, vertex2d b){
vector2d ab = new vector2d(a, b);
vector2d pa = SetInitialValues2(a.x - x, a.y - y);
vector2d c = ab.Multiply2(ab.DotProductf(pa) / ab.DotProductf(ab));
vector2d d = SetInitialValues2(pa.x - c.x, pa.y - c.y);
return (float)Math.sqrt(d.DotProductf(d));
}
public float DistanceFromSegmentf(vertex2d a, vertex2d b){
vector2d ab = new vector2d(a, b);
vector2d ap = SetInitialValues2(x - a.x, y - a.y);
float t = ab.DotProductf(ap) / ab.DotProductf(ab);
if (t <= 0.0f)
t = 0.0f;
if (t >= 1.0f)
t = 1.0f;
vector2d c = new vector2d(a.x + t * ab.x, a.y + t * ab.y);
vector2d cp = SetInitialValues2(x - c.x, y - c.y);
return (float)Math.sqrt(cp.DotProductf(cp));
}
float SignedDistanceFromLinef(vertex2d a, vertex2d b){
vector2d ab = new vector2d(a, b);
vector2d normal = ab.Normalize2();
return normal.DotProductf(this) - normal.DotProductf(a);
}
float SignedDistanceFromSegmentf(vertex2d a, vertex2d b){
vector2d ab = new vector2d(a, b);
vector2d ap = SetInitialValues2(x - a.x, y - a.y);
float t = ap.DotProductf(ab) / ab.DotProductf(ab);
if (t <= 0.0f)
t = 0.0f;
if (t >= 1.0f)
t = 1.0f;
vector2d c = new vector2d(a.x + t * ab.x, a.y + t * ab.y);
vector2d cp = SetInitialValues2(x - c.x, y - c.y);
vector2d normal = ab.Normalize2();
float distance = cp.DotProductf(cp);
float sign = normal.DotProductf(this) - normal.DotProductf(a);
if (sign >= 0.0)
return (float)Math.sqrt(distance);
else
return -(float)Math.sqrt(distance);
}