pseudo-code
note:
C : Current node.
D : Destination node.
set_C : Set of neighbor nodes of C.
M,N:the neighbor of C.
flag : neighbor node is available or not
d_(neighbor→D ): The distance from neighbor to D.
θ_neighbor : The corresponding angle of neighbor.
Q : The allowed communication area.
greedy_forward
Caculate min = d_(C->D);
Initialize nexthop;
while(N in set_C)//对当前节点的所有邻居节点
Caculate d_(N->D)
if d_(N->D)<min then
min = d_(N->D);
nexthop <- N;
end if
end while
return nexthop;
rng_planarize
while(N in set_C) do //对当前节点的所有邻居节点
Caculate d_(N->C);
while(M in set_C) do
Caculate d_(M->C);
Caculate d_(M->N);
if d_(M->C)<d(N->C) && d_(M->N)<d(N->C) then
N <- unavailable;
//remove node N from the list
break; //舍弃节点N
end if
end while
if(N is available) then
add the node into newset;
end if
end while
return newset;
gg_planarize
while(N in set_C) do
Caculate d_(C->middle);
while(M in set_C) do
if(N != M) then
Caculate d_(M->middle);
if(d(M->caculate) < d(C->middle)) then
N <- unavailable;
//remove node N from the list
break; //舍弃节点N
end if
end if
end while
if(N is available) then
add the node into newset;
end if
end while
return nweset;
angle
exist two nodes R1(x1, y1) and R2(x2, y2)
caculate the relative angle of line R1->R2
Caculate d_(R1->R2);
if d_(R1->R2) then
R1 and R2 are the same;
else
Caculate sin = (y2-y1) / d_(R1->R2);
Caculate cos = (x2-x1) / d_(R1->R2);
Caculate theta = arccos(cos);
if sin < 0 then
//R2 在 R1 的负方向
theta = 2*PI - theta;
end if
end if
return theta;
intersect
在这篇博客中介绍了判断线段是否相交的方法,感觉讲的也比较详细。
current a(x1, y1) nexthop b(x2,y2)
source c(x3, y3) destination d(x4, y4)
if 'the rectangle of ab' and 'the rectangle of cd' have public areas then
Caculate u = (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
Caculate v = (d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y)
Caculate w = (a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
Caculate z = (b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
if u*v <= 0 && w*z <= 0 then
is crossed;
else
is uncrossed;
end if
else
is uncrossed;
end if
perimeter_forward
//平面化
if(choose RNG planarization)
neighborset = rng_planarize();
else
neighborset = gg_planarize();
end if
//周边转发
if exist last hop M then
Caculate alpha = θ_M; //计算当前节点与上一跳节点连线ml的夹角
else
Caculate alpha = θ_D; //计算当前节点与目的节点连线md的夹角
end if
while(N in neighborset && N is not equivalent to M) //遍历平面化后的邻居列表
Caculate delta = θ_N;
//选取逆时针方向的节点
//且该节点与当前节点的连线与ml或md的夹角是最小的
delta = delt-alpha;
if delta < 0.0 then //两条线的逆时针夹角
delt = 2*PI + delta;
end if
if delt < minangle then
minangle = delt;
choose N as the next hop;
end if
end while
if the line 'from me to the next hop' and the line 'from source and destination' is intersecting each other then
repeat perimeter_forward with choosing the next hop as the next hop;
end if