How to create Clusterhead in triangle based Sensor deployment using NS2?

Clusterhead is elected in every cluster that is deployed in triangle structure.
TCL part :

proc cl_selection {m1 v1 m2 v2 w} {
global ns r nd min
set x_m [expr $m2 – $m1]
set y_m [expr $v2 – $v1]
set x_v [expr $x_m * $x_m]
set y_v [expr $y_m * $y_m]
set h_v [expr $x_v + $y_v]
set c_l [expr pow($h_v, 0.5)]
puts $r "distance of node $w from midpoint = $c_l"
if {$c_l < $min} {
set min $c_l
set nd $w
}
puts $r "minimum value $min and node $nd"
return $nd
}
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
}
set min 500
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 add_x [expr $xx1 + $xx2 + $xx3]
set midpoint_x [expr $add_x / 3]
set add_y [expr $yy1 + $yy2 + $yy3]
set midpoint_y [expr $add_y / 3]
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"
set l1 [cl_selection $midpoint_x $midpoint_y $xx($i) $yy($i) $i]
puts "the color of node $i and $l1"
set CH $l1
}
}
}
}

Screenshot

Clusterhead creation in triangle based Sensor deployment using NS2