Thanks Dafhi.
I can only imagine Aussie, we've just had a big storm here, the sky is permanently laden grey, the temperature barely above zero, daylight ends about four and starts about 8 the next morning.
I watch Aussie gold hunters and Aussie opal diggers on the telly, only to get a glimpse of the glimmering bush heat and greenery.
So, among triangulating points for a robot, and messing about in his shed, basiccoder2 has the Great Australian bush at his doorstep.
If triangulation (3 points) are used, then a slight code adjustment for basiccoder2's robot.
Bearing in mind that the triangulation uses the average of three positions (as paul doe talked about earlier)
press a key to refresh, escape to end.
Code: Select all
Type Point
As Double x,y
End Type
Type Line
As Point s,f
End Type
Function isleft(L As Line,p As Point) As Long
Return -Sgn((L.s.x-L.f.x)*(p.y-L.f.y)-(p.x-L.f.x)*(L.s.y-L.f.y))
End Function
Function intersects(L1 As Line,L2 As Line) As Long
If isleft(L2,L1.s) = isleft(L2,L1.f) Then Return 0
If isleft(L1,L2.s) = isleft(L1,L2.f) Then Return 0
Return -1
End Function
Function intersections(l1 As Line,l2 As Line,_out As Point) As Long
_out=Type(0,0)
Dim As Point p1=l1.s
Dim As Point p2=l1.f
Dim As Point p3=l2.s
Dim As Point p4=l2.f
Var x12=p1.x-p2.x
Var x34=p3.x-p4.x
Var y12=p1.y-p2.y
Var y34=p3.y-p4.y
Var c=x12 * y34 - y12 * x34
If (Abs(c) < 0.01) Then
Return 0
Else
Var a = p1.x * p2.y - p1.y * p2.x
Var b = p3.x * p4.y - p3.y * p4.x
Var x = (a * x34 - b * x12) / c
Var y = (a * y34 - b * y12) / c
_out=Type(x,y)
End If
Return 1
End Function
Function GetLine(x As Long,y As Long,angle As Double,length As Double)As Line
Dim As Double x2,y2
angle=angle*.0174532925199433
x2=x+length*Cos(angle)
y2=y+length*Sin(angle)
Dim As Line _out=Type<Line>((x,y),(x2,y2))
Return _out
End Function
Function getbearing(p As Point,x As Point) As Double
Const pi=4*Atn(1)
Dim As Line z1=(p,x)
Var a2=(Atan2((z1.s.y-z1.f.y),(z1.s.x-z1.f.x))*180/pi)-90
Return Iif(-a2<0,a2+90,-a2)
End Function
Function getpos(p1 As Point,b1 As Double,p2 As Point,b2 As Double,p3 As Point,b3 As Double) As Point
Var z1=GetLine(p1.x,p1.y,360-(b1-90)+180,200)
Var z2=GetLine(p2.x,p2.y,360-(b2-90)+180,200)
Var z3=GetLine(p3.x,p3.y,360-(b3-90)+180,200)
If intersects(z1,z2) And intersects(z2,z3) And intersects(z3,z1) Then
Dim As Long pp,flag
Dim As Point pt1,pt2,pt3
pp=intersections(z1,z2,pt1)
If pp then flag+=1
pp=intersections(z2,z3,pt2)
If pp Then flag+=1
pp=intersections(z3,z1,pt3)
If pp Then flag+=1
If flag=3 Then
Pset(pt1.x,pt1.y),4'optional
Line -(pt2.x,pt2.y),4'optional
Line -(pt3.x,pt3.y),4'optional
Return Type((pt1.x+pt2.x+pt3.x)/3,(pt1.y+pt2.y+pt3.y)/3)
Else
Return Type(0,0)
End If
End If
End Function
Screen 19
Window (0,0)-(799,599)
Randomize
#define range(f,l) Rnd*((l)-(f))+(f)
#define onscreen(p) p.x>0 and p.x<800 and p.y>0 and p.y<600
Dim As Point mypos,p1,p2,p3,x
Dim As Double bearingp1,bearingp2,bearingp3
Do
Do
Cls
p1=Type(range(100,700),range(100,500))'three fixed beacons
p2=Type(range(100,700),range(100,500))
p3=Type(range(100,700),range(100,500))
x=Type(range(100,700),range(100,500))'a position
bearingp1=getbearing(p1,x)
bearingp2=getbearing(p2,x)
bearingp3=getbearing(p3,x)
mypos=getpos(p1,bearingp1,p2,bearingp2,p3,bearingp3)
Loop Until mypos.x<>0 And mypos.y<>0 And onscreen(mypos)
Circle (p1.x,p1.y),5,2,,,,f
Circle (p2.x,p2.y),5,3,,,,f
Circle (p3.x,p3.y),5,4,,,,f
Circle(mypos.x,mypos.y),1,15,,,,f
Line(mypos.x,mypos.y)-(p1.x,p1.y)
Line(mypos.x,mypos.y)-(p2.x,p2.y)
Line(mypos.x,mypos.y)-(p3.x,p3.y)
Color 2
Print "bearing of green ";bearingp1
Color 3
Print "bearing of blue ";bearingp2
Color 4
Print "bearing of red ";bearingp3
Color 15
Print "true random position "
Print x.x,x.y
Draw String(mypos.x-80,mypos.y),"calculated "+Str((mypos.x))+" , "+Str((mypos.y))
Sleep
Loop Until Inkey=Chr(27)
'do