Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to calculate distance between nodes in NS2?

Description

Dynamic topology (random x and y coordinate) is created. Distance between the nodes is calculated by applying the node’s (x,y) coordinates in Pythagoras theorem. Distance output is printed in distance.tr file.

Sample Code

#Filename: sample11.tcl
#***************DISTANCE CALCULATION ***************************#
set r [open distance.tr w]
#Location fixing for a single node
for {set i 0} {$i < 10} {incr i} {

set xx($i) [expr rand()*$val(x)]
set yy($i) [expr rand()*$val(y)]

$node_($i) set X_ $xx($i)
$node_($i) set Y_ $yy($i)
$node_($i) set Z_ 0

}

#*******************DISTANCE CALCULATION********************#

for {set i 0} {$i < $val(nn) } { incr i } {
puts "\n"
puts $r "\n"

for {set j 0} {$j < $val(nn) } { incr j } {

set dx [expr $xx($i) – $xx($j)]
set dy [expr $yy($i) – $yy($j)]

set dx2 [expr $dx * $dx]
set dy2 [expr $dy * $dy]

set h2 [expr $dx2 + $dy2]

set h($i-$j) [expr pow($h2, 0.5)]
puts "distance of node($i) from node($j) = $h($i-$j)"
puts $r "distance of node($i) from node($j) h($i-$j) = $h($i-$j)"

}
}

Screenshots

calculate distance between nodes in NS2
distance calculation