x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 588

# Trilateration Computation

Hello eveybody;

I need program to compute three point assuming x1=0, y1=0, B=PI=180 degree =200 grades) in VB 6.0

by entering three distance like: Dist3,Dist2,Dist1

computing poits : point 2, Point 3

result like : p1(0,0) , p2(?,?), p3(?,?) .. Using Trilateration Comptation Method!

Thanks alot.
0
LoveBii
• 3
• 2
1 Solution

RetiredCommented:
Not sure what this bit means :  B=PI=180 degree =200 grades

I suppose that one point is at the origin and that one liies on the X-Axis. You may be trying to say something different.

********* In a module :

Option Explicit

Type XY
x As Single
y As Single
End Type

********* In a Form:

Option Explicit

Private Sub Command1_Click()
Dim a As Single
Dim b As Single
Dim c As Single

Dim Points(2) As XY

a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)

Select Case True
Case a > b + c, b > a + c, c > a + b
MsgBox "Invalid triangle"
Exit Sub
End Select

'Point 0 is at origin (given)
Points(0).x = 0
Points(0).y = 0

'Point 1 is on X-Axis (given)
Points(1).x = c
Points(1).y = 0

Points(2).x = (a ^ 2 + c ^ 2 - b ^ 2) / (2 * c)
Points(2).y = Sqr(a ^ 2 - Points(2).x ^ 2)

End Sub

0

Author Commented:
Dear GrahamSka;

thanks alot for your interest with my question, but this is too much far from what i want.

i'll give you some hints in trilateration computation:

for point 2 you have to calculate:
dx=sin(b)*dist3
dy=cos(b)*dist3

for point 3 you have to calculate:
generate dx1,dy1 from x2-x1,y2-y1
TAN2(DX1,DY1) * (2*PI)
D3=SQRT(DX1^2+DY1^2)
D4=DIST1+DIST2
IF (D3.LT.D4) ERROR
B=(D3^2+DIST1^2-DIST2^2)/(2*D3*DIST1)
BETA=ACOS(B)
G=(DIST2^2+D3^2-DIST1^2)/(2*DIST2*D3)
GAMA=ACOS(G)
V13=V12-BETA
V23=V12-PI+GAMA
DX13=DIST1*SIN(V13)
DY13=DIST1*COS(V13)
X13=X1+DX13
Y13=Y1+DY13
DX23=DIST2*SIN(V23)
DY23=DIST2*COS(V23)

-------------------- USE THESE FORMULA'S AS SHOULD BE IN VB, AND MAKE SURE FROM THE SIN & COS VALUE PLUS TAN2 (FUNCTION) .. MAKE SURE THAT U R USING RADIUS!!! AND NOT NEGATIVE VALUES

IF YOU GOT ANYTHING PLEASE SEND ME! ... THANKS ALOT.

Regards;
0

RetiredCommented:
I now see that you do not accept that the co-ordinates be drawn to allow the second point to lie on the X-axis. My solution would therefore require that points 2 and 3 be rotated through the angle b.

However, here is a translation of code that you supplied. I have not analysed it in any great depth.

Note that I don't understand your first line, so I have commented it out. Also, be aware that VB uses radians in its trigonometric functions.

You'll notice that I have provided an ArcCos function (called Acos to match the name in your code), because VB does not have one built in.

Option Explicit

Function Acos(Theta As Single) As Single
Acos = Atn(-Theta / Sqr(-Theta * Theta + 1)) + 2 * Atn(1)
End Function

Private Sub Command1_Click()
'TAN2(DX1,DY1) * (2*PI)
D3 = Sqr(DX1 ^ 2 + DY1 ^ 2)
D4 = DIST1 + DIST2
If D3 < D4 Then
MsgBox "Distances Error"
Exit Sub
End If
B = (D3 ^ 2 + DIST1 ^ 2 - DIST2 ^ 2) / (2 * D3 * DIST1)
BETA = Acos(B)
G = (DIST2 ^ 2 + D3 ^ 2 - DIST1 ^ 2) / (2 * DIST2 * D3)
GAMA = Acos(G)
V13 = V12 - BETA
V23 = V12 - PI + GAMA
DX13 = DIST1 * Sin(V13)
DY13 = DIST1 * Cos(V13)
X13 = X1 + DX13
Y13 = Y1 + DY13
DX23 = DIST2 * Sin(V23)
DY23 = DIST2 * Cos(V23)
End Sub

0

Author Commented:
Hello every body;

I got something ,,, meanningfull and very close to what i want,,, in fact its what i want?

here's the code .... fully ... but i have doubt about resulting values ... i think there's a negative values plus sometime division by zero ... i'll increase the point's of challenge!! who will get it first!! ... Reply now!?

--------------------------------------- The Code -------------------------------------------
Option Explicit
Const iX1 As Integer = 0
Const iY1 As Integer = 0

Private Sub Command1_Click()
Dim DX As Double ' double
Dim DY As Double
Dim iB As Single
Const iPI As Single = 3.14159265

If DIST3.Text = "" Or DIST2.Text = "" Or DIST1.Text = "" Then
MsgBox "All distance values must be enterd!", vbCritical, Form1.Caption
DIST3.SetFocus
Exit Sub
Else
X1.Text = iX1
Y1.Text = iY1
iB = 180 'degree
X2.Text = Format(Val(X1.Text) + DX, "###.###")
Y2.Text = Format(Val(Y1.Text) + DY, "###.###")
Dim DX1, DY1, V12, V13, D3, D4, B, BETA, G, GAMA, V23, DX13, DY13, X13, Y13, DX23, DY23, X23, Y23 As Double
DX1 = Val(X2.Text) - Val(X1.Text)
DY1 = Val(Y2.Text) - Val(Y1.Text)
'V12 = DATAN2D(DX1, DY1) ' ----
V12 = atan2(DY1, DX1)
V12 = V12 + (2 * iPI)
D3 = Sqr((DX1 ^ 2) + (DY1 ^ 2))
D4 = Val(DIST2.Text) + Val(DIST1.Text)
If (D4 < D3) Then
MsgBox "Distances Error", vbExclamation, Form1.Caption
Exit Sub
Else
B = ((D3 ^ 2) + (Val(DIST1.Text) ^ 2) - (Val(DIST2.Text) ^ 2)) / (2 * D3 * Val(DIST1.Text))
BETA = ArcCos(B)
G = ((Val(DIST2.Text) ^ 2) + (D3 ^ 2) - (Val(DIST1.Text) ^ 2)) / (2 * Val(DIST2.Text) * D3)
GAMA = ArcCos(G)
V13 = V12 - BETA
V23 = (V12 - iPI) + GAMA
DX13 = Val(DIST1.Text) * Sin(V13)
DY13 = Val(DIST1.Text) * Cos(V13)
X13 = Val(X1.Text) + DX13
Y13 = Val(Y1.Text) + DY13
DX23 = Val(DIST2.Text) * Sin(V23)
DY23 = Val(DIST2.Text) * Cos(V23)
X23 = Val(X2.Text) + DX23
Y23 = Val(Y2.Text) + DY23
X3.Text = Format((X23 + X13) / 2, "###.###")
Y3.Text = Format((Y23 + Y13) / 2, "###.###")
End If

T_D1.Text = Sqr(((Val(X3.Text) - Val(X1.Text)) ^ 2) + ((Val(Y3.Text) - Val(Y1.Text)) ^ 2))
T_D2.Text = Sqr(((Val(X3.Text) - Val(X2.Text)) ^ 2) + ((Val(Y3.Text) - Val(Y2.Text)) ^ 2))
T_D3.Text = Sqr(((Val(X2.Text) - Val(X1.Text)) ^ 2) + ((Val(Y2.Text) - Val(Y1.Text)) ^ 2))

End If
End Sub

Private Sub Command2_Click()
End Sub

Private Sub Command3_Click()
DIST3.Text = ""
DIST2.Text = ""
DIST1.Text = ""
X1.Text = ""
Y1.Text = ""
X2.Text = ""
Y2.Text = ""
X3.Text = ""
Y3.Text = ""
T_D1.Text = ""
T_D2.Text = ""
T_D3.Text = ""
DIST3.SetFocus
End Sub

Private Sub Form_Activate()
DIST3.SetFocus
End Sub

Public Function ArcCos(ByVal X As Double) As Double
ArcCos = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
End Function
End Function

Public Function DATAN2D(ByVal X As Double, ByVal Y As Double) As Double
Dim localTan As Double
Dim Angle As Double

If X <> 0 Then
localTan = Y / X
Angle = Atn(localTan) * 180 / 3.1415926
Else
Angle = 0# 'Angle = 0#
End If
If X < 0 Then
If Y > 0 Then
Angle = 90 - Angle
Else
Angle = 180 + Angle
End If
Else
If Y < 0 Then
Angle = 360 + Angle
End If

End If
DATAN2D = Angle
End Function

Function atan2(ys, xs)
' Given y and x coords returns atan2
Dim theta, pi
pi = 3.14159265358979
If xs <> 0 Then
theta = Atn(ys / xs)
If xs < 0 Then
theta = theta + pi
End If
Else
If ys < 0 Then
theta = 3 * pi / 2 '90
Else
theta = pi / 2 '270
End If
End If
atan2 = theta
End Function

---------------------- waiting to hear from you ---------------------- Good Luck ------------------- ;)
0

Author Commented:
TIPs:
* Make Sure there's no negative values in (x2,y2) , (x3,y3).
* No division by zero or error's appear's inside code.
* The result of your entering distance should be the same to T_D1.TEXT,T_D2.TEXT,T_D3.TEXT, THIS IS A CHECK BACK!!
if its true ... then go ahead.
* get back to me soon to give you the piont's.
0

Commented:
PAQed with points refunded (250)

modulo
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.