# How to create triangle based sensor deployment using NS2?

#### Source Code

The network area is formed as a triangle shape. This region act as a cluster that contain sensor nodes. The formula for a region is
Area = (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2.0

TCL part :
proc area {a1 b1 a2 b2 a3 b3} {
set ax [expr \$b2 – \$b3]
set ay [expr \$b3 – \$b1]
set az [expr \$b1 – \$b2]
set axx [expr \$ax * \$a1]
set ayy [expr \$ay * \$a2]
set azz [expr \$az * \$a3]
set hx [expr \$axx + \$ayy + \$azz]
set ar [expr \$hx / 2.0]
set arr [expr abs(\$ar)]
puts “Distance between x1 and x2 = \$arr”
return \$arr
}
proc isinside {c1 d1 c2 d2 c3 d3 c d g} {
global now ns
set inside 0
set A [area \$c1 \$d1 \$c2 \$d2 \$c3 \$d3]
set A1 [area \$c \$d \$c2 \$d2 \$c3 \$d3]
set A2 [area \$c1 \$d1 \$c \$d \$c3 \$d3]
set A3 [area \$c1 \$d1 \$c2 \$d2 \$c \$d]
set AA [expr \$A1 + \$A2 + \$A3]
set diff [expr \$AA – \$A]
puts “the A and \$A yyyyyyyy AA and \$AA and \$diff”
if {\$A == \$AA || \$diff < 10} {
set inside [expr \$inside + 1]
puts “Nodes inside the triangle \$g and \$inside ”
}
return \$inside
}
for {set i 0} {\$i < \$val(nn) } {incr i } {
set now [\$ns now]
if {\$xx(\$i) > 0 && \$xx(\$i) < 200} {
if {\$yy(\$i) < 200} {
set xx1 0
set yy1 0
set xx2 200
set yy2 0
set xx3 100
set yy3 200
set ins [isinside \$xx1 \$yy1 \$xx2 \$yy2 \$xx3 \$yy3 \$xx(\$i) \$yy(\$i) \$i]
if {\$ins == 1} {
\$ns at [expr \$now + 0.1] “\$node_(\$i) color darkcyan”
puts “the color of node \$i”
}
}
}
}