Dystans pomiedzy dwoma odcinkami (wyliczenie punktow)

0

mam taki oto kod:

const float SPECIAL_FEAUTRE = 0.00000001;  

t3dpoint __fastcall vectors_add(t3dpoint v1,t3dpoint v2)
{
t3dpoint result;
result.x = v1.x+v2.x;
result.y = v1.y+v2.y;
result.z = v1.z+v2.z;
return result;
}

t3dpoint __fastcall vectorAB(t3dpoint A,t3dpoint B)
{
t3dpoint  result;
result.x = B.x-A.x;
result.y = B.y-A.y;
result.z = B.z-A.z;
return result;
}

t3dpoint __fastcall vectors_substract_v1minusv2(t3dpoint v1,t3dpoint v2)
{
	t3dpoint result;
result.x = v1.x-v2.x;
result.y = v1.y-v2.y;
result.z = v1.z-v2.z;
return result;
}


float __fastcall Dot(t3dpoint vVector1,t3dpoint  vVector2)
{
return ( (vVector1.x * vVector2.x) + (vVector1.y * vVector2.y) + (vVector1.z * vVector2.z) );
}

float __fastcall VectorLength(t3dpoint vec)
{
    return    sqrt(Dot(vec,vec)); 
}

//-------------------------------------------

float dist3D_Segment_to_Segment( t3dpoint S1p0, t3dpoint S1p1, t3dpoint S2p0, t3dpoint S2p1)
{
	t3dpoint   u = vectorAB(S1p0,S1p1);//   S1.P1 - S1.P0; vectorAB equals to B - A
	t3dpoint   v = vectorAB(S2p0,S2p1);//S2.P1 - S2.P0;
	t3dpoint   w = vectorAB(S2p0,S1p0);//S1.P0 - S2.P0;
	float    a = Dot(u,u);        // always >= 0
	float    b = Dot(u,v);                               //jak duzo u jest na V
	float    c = Dot(v,v);        // always >= 0
	float    d = Dot(u,w);
	float    e = Dot(v,w);
	float    D = a*c - b*b;       // always >= 0
	float    sc, sN, sD = D;      // sc = sN / sD, default sD = D >= 0
	float    tc, tN, tD = D;      // tc = tN / tD, default tD = D >= 0

	// compute the line parameters of the two closest points
	if (D < SPECIAL_FEAUTRE) { // the lines are almost parallel
		sN = 0.0;        // force using point P0 on segment S1
		sD = 1.0;        // to prevent possible division by 0.0 later
		tN = e;
		tD = c;
	}
	else {                // get the closest points on the infinite lines
		sN = (b*e - c*d);
        tN = (a*e - b*d);
		if (sN < 0.0) {       // sc < 0 => the s=0 edge is visible
            sN = 0.0;
            tN = e;
			tD = c;
		}
		else if (sN > sD) {  // sc > 1 => the s=1 edge is visible
			sN = sD;
            tN = e + b;
            tD = c;
        }
	}

	if (tN < 0.0) {           // tc < 0 => the t=0 edge is visible
        tN = 0.0;
        // recompute sc for this edge
        if (-d < 0.0)
			sN = 0.0;
        else if (-d > a)
            sN = sD;
        else {
            sN = -d;
			sD = a;
        }
    }
	else if (tN > tD) {      // tc > 1 => the t=1 edge is visible
        tN = tD;
		// recompute sc for this edge
		if ((-d + b) < 0.0)
            sN = 0;
        else if ((-d + b) > a)
            sN = sD;
		else {
            sN = (-d + b);
            sD = a;
        }
	}
    // finally do the division to get sc and tc
	sc = (abs(sN) < SPECIAL_FEAUTRE ? 0.0 : sN / sD);
    tc = (abs(tN) < SPECIAL_FEAUTRE ? 0.0 : tN / tD);

    // get the difference of the two closest points
	t3dpoint   dP = vectors_add( w, vectors_substract_v1minusv2(vector_multiple(u,sc),vector_multiple(v,tc)));  // = S1(sc) - S2(tc)

	return VectorLength(dP);   // return the closest distance
}

chodzi mi o funkcje
dist3D_Segment_to_Segment

tam są dziwne rozne przeksztalcenia ale jedno jest pewne ta funkcja dziala poprawnie (oprzynajniej jak wstawialem wartosci i wiedzialem jakiego mam sie wyniku spodziewac to dzialalo porpawnie)

ale jest jeszcze jedno musze znalezc te dwa punkty, ktore reprezentują ten dystans pomiedzy dwoma odcinkami, niestety wymiekkam

nie rozumiem kilku floatow sc tc i sn tn

dodatkowo co mi daje dodanie wektora w do odjetych wektoow (przemnozonego u i przemnozonego v)

kod jest podpisany

    // get the difference of the two closest points
	t3dpoint   dP = vectors_add( w, vectors_substract_v1minusv2(vector_multiple(u,sc),vector_multiple(v,tc)));  // = S1(sc) - S2(tc)

// get the difference of the two closest points

ale kaj te dwa punkty

wzailem sobie to wsadzilem do tstringlist i wyprodukowal mi takie dane, ktore i tak mi nic nie podpowiedzialy

S1p0 = 0.3.6
S1p1 = 112.3.6
S2p0 = 4.-10.4
S2p1 = 4.10.4
u = 112.0.0
v = 0.20.0
w = -4.13.2
a = 12544
b = 0
c = 400
d = -448
e = 260
D = 5017600
2 points managed
sc = 0,0357142873108387
tc = 0,649999976158142
dP = 112.0.0
REURNED = 2

w ogole co mi daje Dot(s1p0,s2p0) czyli dot produkt dwoch poczatkowych punktow (tych dwoch linii)

nic nie wiem :X

0

Słyszałeś o czymś takim jak iloczyn skalarny?
http://pl.wikipedia.org/wiki/Iloczyn_skalarny

Gdzie widzisz "Dot(s1p0,s2p0)"? Iloczyn skalarny wyznacz się pomiędzy wektorami, a nie punktami.

1 użytkowników online, w tym zalogowanych: 0, gości: 1