Before tackling this one, take a look at
this one.
+-------------D
/| /|
/ | / |
/ | / |
/ | / |
C-------------+ |
| | | |
| B--------|----+
| / | /
| / | /
| / | /
|/ |/
+-------------A
A, B, C and D are non-adjacent vertices of a cube. There is a point P in space such that PA=3, PB=5, PC=7, and PD=6. Find the distance from P to the other four vertices and find the length of the edge of the cube.
There are two answers, one with P outside the cube and one with P inside the cube.
Using a modified version of the program, the distances to the corners of the cube (including the unlabeled ones) appear below the x,y,z coordinates of the point and the edge length of the cube:
External:
x= 1.7877458 y= 3.8912615 z=-2.5810945; size 3.0900649;
3.0000000 5.0000000 7.0000000 6.0000000
4.8476798 5.8736701 7.1063352 3.2403704
Internal:
x= 2.8806627 y= 4.0650647 z= 0.4207511; size 5.4880020;
3.0000000 5.0000000 7.0000000 6.0000000
4.8476799 5.8736701 7.1063352 3.2403703
DEFDBL A-Z
size = 6
x = 5
y = 3
z = 1
aGoal = 3: bGoal = 5: cGoal = 7: dGoal = 6
x = RND(1) * 8
y = RND(1) * 4
z = RND(1) * 1
size = RND(1) * 5
delta = .1
CLS
GOSUB evalPos
aOff = da - 3
bOff = db - 5
cOff = dc - 7
dOff = dd - 11
DO
FOR itr = 1 TO 48
leastTot = 9999
FOR dx = -delta TO delta STEP delta
xTr = x + dx
FOR dy = -delta TO delta STEP delta
yTr = y + dy
FOR dz = -delta TO delta STEP delta
zTr = z + dz
FOR dS = -delta TO delta STEP delta
sizeTr = size + dS
IF dx <> 0 OR dy <> 0 OR dz <> 0 OR dS <> 0 THEN
GOSUB evalTrPos
IF totTr < leastTot THEN
dxP = dx: dyP = dy: dzP = dz: dSP = dS
leastTot = totTr
END IF
END IF
NEXT dS
NEXT dz
NEXT dy
NEXT dx
xTr = x + 2 * dxP: yTr = y + 2 * dyP: zTr = z + 2 * dzP
sizeTr = size + 2 * dSP
GOSUB evalTrPos
'IF totTr > leastTot THEN delta = delta * .8
x = x + dxP: y = y + dyP: z = z + dzP: size = size + dSP
GOSUB evalPos
PRINT USING "x=##.####### y=##.####### z=##.#######; size ##.#######;"; x; y; z; size
PRINT USING " ##.####### ##.####### ##.####### ##.#######"; da; db; dc; dd
NEXT itr
DO: a$ = INKEY$: LOOP UNTIL a$ > ""
IF a$ = "-" THEN delta = delta / 2
LOOP UNTIL a$ = CHR$(27)
GOSUB evalOthPos
PRINT USING " ##.####### ##.####### ##.####### ##.#######"; de; df; dg; dh
END
evalTrPos:
daTr = SQR((xTr - sizeTr) ^ 2 + (yTr - sizeTr) ^ 2 + zTr ^ 2)
dbTr = SQR(xTr ^ 2 + yTr ^ 2 + zTr ^ 2)
dcTr = SQR((xTr - sizeTr) ^ 2 + (zTr - sizeTr) ^ 2 + yTr ^ 2)
ddTr = SQR((yTr - sizeTr) ^ 2 + (zTr - sizeTr) ^ 2 + xTr ^ 2)
totTr = ABS(daTr - aGoal) + ABS(dbTr - bGoal) + ABS(dcTr - cGoal) + ABS(ddTr - dGoal)
RETURN
evalPos:
da = SQR((x - size) ^ 2 + (y - size) ^ 2 + z ^ 2)
db = SQR(x ^ 2 + y ^ 2 + z ^ 2)
dc = SQR((x - size) ^ 2 + (z - size) ^ 2 + y ^ 2)
dd = SQR((y - size) ^ 2 + (z - size) ^ 2 + x ^ 2)
totTr = ABS(da - aGoal) + ABS(db - bGoal) + ABS(dc - cGoal) + ABS(dd - dGoal)
RETURN
evalOthPos:
de = SQR((x - size) ^ 2 + (y) ^ 2 + z ^ 2)
df = SQR((x - size) ^ 2 + (y - size) ^ 2 + (z - size) ^ 2)
dg = SQR(x ^ 2 + (z - size) ^ 2 + y ^ 2)
dh = SQR((y - size) ^ 2 + z ^ 2 + x ^ 2)
RETURN
|
Posted by Charlie
on 2004-07-20 08:45:54 |