Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#

Wireless Sensor Network Localization Simulator v1.1

Rate me:
Please Sign up or sign in to vote.
3.70/5 (9 votes)
14 Jul 2011CPOL8 min read 71.8K   9.3K   21   39
Wireless Sensor Network Localization Simulator v1.1

Image 1

Figure 1: WSN Localization Simulator

Introduction

A wireless sensor network (WSN) is a network composed of a large number of sensor nodes, which are deployed in the monitoring field. With the rapid development of WSNs, providing development tools such as simulation environment before deploying real nodes in physical environments is getting more important. A good simulation environment can help developers build their prototype models to know the interactions and the behavior of each node. In addition, most of WSN applications will deploy a large number of nodes in a simulation environment. However, the simulation speed depends on the simulation fidelity and scale.

WSN Localization

WSN localization is the operation of determining the position of sensor nodes; this position is estimated and not accurate. Because GPS have high cost and work outdoors only, there are many localization algorithms that try to be more accurate in determining the position of sensor nodes (i.e. have low localization error). In order to effectively design and develop new localization algorithms targeted towards WSNs, there is a strong need to use an equally effective and reliable simulation environment. Sensor nodes are, by design, tiny and inexpensive, but due to their need to work in large numbers make full-scale testing a fairly expensive process to be carried out using real hardware. Analytical modeling using tools such as MATLAB help in acquiring a quick insight, but they fail to provide anything close to realistic results that can be acquired through proper implementations and good simulation tools.

The Problem and Its Solution

Most of WSNs simulators don't support one or two of the following criteria: easy, extendable and scalable, so that we design and implement an integrated framework used for localization in WSNs and available for the analysis of different types of localization schemes (Range-free, Range-based and Hybrid localization algorithms). The obtained framework is simple and easy to be used to demonstrate the performance evaluation and comparison of different types of localization schemes. It can also be extended to include many localization algorithms by future researchers.

Design Criteria for our Framework

  1. Supports completely large scale networks, e.g. 400 nodes x 400 nodes which are fully interconnected which implies 160,000 network connections. So, our proposed framework is scalable.
  2. The network can be deployed based on a wide range of parameters: network size, sensor nodes communication distance, locator radius (communication range), sector angle (rotation angle) and locator beam width.
  3. Supports multithreading, where the simulated algorithm runs on a separate thread from the thread controlling the developer interface.
  4. Packaging all simulator classes into a referenced DLL file for better modularity design.
  5. Ease of implementing various localization algorithms by encapsulating each one into a separate DLL file. Developers can write their own localization algorithm into a DLL file and reference it into our framework.
  6. Can be extended to implement many different localization algorithms with different categories (range-free, range-based and a hybrid of both).
  7. Suitable developer interface to enable deploying and moving sensor and locator nodes and changing various simulation parameters.

Framework Architecture

Our framework is composed of two main components: simulator and a localization algorithm. A simulator includes two classes; the first one is network DLL file which contains all simulator classes (e.g. sensor class, locator class, packet class, ...etc.) and the second one is developer interface class which contains all control functions (e.g. run and stop simulator, painting method,… etc.). The second component of our framework is the localization algorithm which is written by the developer into a DLL file and referencing this file into our framework, so that the developer has the ability to write his localization algorithm but under certain design specifications as shown in Fig. 2.

C#
Set_Parameters (locator range, beam width …)
{
}
Connect (list of locators, list of sensors …)
{
}
Send (list of sensors)
{
}
Localize (list of sensors, locator range …)
{
}

Image 2

Figure 2: Localization Algorithm Design Specifications

The first method (Set_Parameters) is responsible to get all control data from user interface. The second method is used to test the connectivity between a sensor node and a locator node. The third method (Send), after a valid connection, is used to transmit packets between different nodes. Finally, after getting all necessary packets from locator nodes, a sensor node can estimate its position using the fourth method (Localize).

As shown in Fig. 3, we illustrate the sequence diagram for submitting parameters from the user through simulator and getting the response back from the localization algorithm. First, the user sets the required parameters via user interface and the simulator receives these parameters and applies it using Set_Parameters method which gives the needed argument to the localization algorithm. Second, the algorithm will reply to the simulator. Third, calling Connect method to test the connectivity between different nodes, if there is a connection, fill array list of connections between different nodes. Fourth, calling send method to carry out sending packets between connected nodes by filling array list which contains the sent packets for each sensor node. Finally, calling Localize method to estimate the position of a sensor node and returning the results back to the user into an external text file.

Customizing a Localization Algorithm

A localization algorithm can be customized as follows:

  1. Creating a class library (DLL).
  2. Writing a localization algorithm according to the design specifications mentioned above.
  3. Reference this class library as shown in the snapshot of Fig. 4.

Finally, the proposed framework is easy to use, scalable that supports 400x400 nodes and extendable which allows developers to write their own localization algorithms and referencing it to our framework.

How Does this Framework Help Developers?

This framework allows developers to write their own localization algorithms (DLL file) according to the design specifications mentioned above. Hence, their algorithms can be referenced to our framework as shown in Fig. 3.

How Does the Code Actually Work?

Our framework is composed of two main components: simulator and a localization algorithm. A simulator includes two classes; the first one is network DLL file which contains all simulator classes as shown below:

1- Network Class

Which is used for initialization of user interface parameters.

C#
public WirelessSensorNetwork(int NoSensors, int iMaxX, 
    int iMaxY, int LocatorRadius, int sangle1, int sangle2,
    int sangle3, int sangle4, int sangle5, int sangle6, 
    int sangle7, int sangle8, int Sweepangle, int stepx, 
    int stepy, bool radio1, bool radio2, int combo1, 
    int combo2, int combo3, int BeaconRadius, int noBeacons, 
    bool checkBeacons, int noMobSensors, int noRLocators){    

this.radio1 = radio1;
this.radio2 = radio2;
this.combo1 = combo1;
this.combo2 = combo2;
this.combo3 = combo3;
this.stepx = stepx;
this.stepy = stepy;
this.noBeacons = noBeacons;

BuildNetwork() method allows deploying of various network objects: sensor, locator and beacon nodes as follows:

C#
public void BuildNetwork()
{
    aSensors = new ArrayList();
    aLocators = new ArrayList();        //Array of Locators
    aBeacons = new ArrayList();         //Array of Beacons

    if (radio1 == true && radio2 == false)
    {
        
        if (combo1 == 4)
        {
int m = 0;
while ((aLocators.Count) < 2)
{
    if (m == 0)
    {
    Locator locator = new Locator(m, 20, 520, LocatorRadius, sangle1);
          aLocators.Insert(m, locator);

    }
    else if (m == 1)
    {
   Locator locator = new Locator(m, 520, 520, LocatorRadius, sangle2);
         aLocators.Insert(m, locator);

    }
    m++;
}
int s = 0, x = 70, y = 70;
           
while (aSensors.Count < 25)
{

       WirelessSensor sensor = new WirelessSensor(x, y);
    aSensors.Insert(s, sensor);
    if (x < 470) x += 100;
    else { x = 70; y += 100; }
    s++;
}
        }
        else
        {
int s = 0;
while (aSensors.Count < NoSensors)
{

WirelessSensor sensor = new WirelessSensor(r.Next(iMaxX - 10) + 5,
           r.Next(iMaxY - 10) + 5);
           aSensors.Insert(s, sensor);

    s++;
}
        }        
    }

    //Adding Locators to network
    if (combo1 == 0)
    {
        int m = 0;
        while ((aLocators.Count) < noRLocators)
        {
           Locator locator = new Locator(m, r.Next(iMaxX - 10) + 5, 
           r.Next(iMaxY - 10) + 5, 
           LocatorRadius, r.Next(0, 315));
           aLocators.Insert(m, locator);
           m++;
        }
    }
    else if (combo1 == 1)
    {
        int x = 20, y = 20;
        int m = 0;

        while ((aLocators.Count) < 8)
        {
if (m == 0)
{
    x = 20; y = 20;
   Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
    aLocators.Insert(m, locator);
}
else if (m == 1)
{
    x = 270; y = 20;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
    aLocators.Insert(m, locator);
}
else if (m == 2)
{
    x = 520; y = 20;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
    aLocators.Insert(m, locator);
}
else if (m == 3)
{
    x = 20; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
    aLocators.Insert(m, locator);
}
else if (m == 4)
{
    x = 520; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);
}

else if (m == 5)
{
    x = 20; y = 520;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle6);
    aLocators.Insert(m, locator);
}

else if (m == 6)
{
    x = 270; y = 520;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle7);
    aLocators.Insert(m, locator);
}

else if (m == 7)
{
    x = 520; y = 520;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle8);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    else if (combo1 == 2)
    {
        int x = 20, y = 20;
        int m = 0;

        while ((aLocators.Count) < 5)
        {
if (m == 0)
{
    x = 270; y = 20;
   Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
    aLocators.Insert(m, locator);
}
else if (m == 1)
{
    x = 20; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
    aLocators.Insert(m, locator);
}
else if (m == 2)
{
    x = 270; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
    aLocators.Insert(m, locator);
}
else if (m == 3)
{
    x = 520; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
    aLocators.Insert(m, locator);
}
else if (m == 4)
{
    x = 270; y = 520;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    else if (combo1 == 3)
    {
        int x = 20, y = 20;
        int m = 0;
       
        while ((aLocators.Count) < 6)
        {
if (m == 0)
{
    x = 145; y = 100;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
    aLocators.Insert(m, locator);

}
else if (m == 1)
{
    x = 270; y = 100;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
    aLocators.Insert(m, locator);

}
else if (m == 2)
{
    x = 395; y = 100;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
    aLocators.Insert(m, locator);
}
else if (m == 3)
{
    x = 145; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
    aLocators.Insert(m, locator);

}
else if (m == 4)
{
    x = 270; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);

}
else if (m == 5)
{
    x = 395; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    
    //Deploy Beacons 
    if (checkBeacons == true)
    {
        int b = 0;
        while (aBeacons.Count < noBeacons)
        {
           Beacon beacon = new Beacon(b, r.Next(iMaxX - 10), 
           r.Next(iMaxY - 10), BeaconRadius);
           aBeacons.Insert(b, beacon);
           b++;
        }
    }
}

else if (m == 6)
{
    x = 270; y = 520;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle7);
    aLocators.Insert(m, locator);
}

else if (m == 7)
{
    x = 520; y = 520;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle8);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    else if (combo1 == 2)
    {
        int x = 20, y = 20;
        int m = 0;

        while ((aLocators.Count) < 5)
        {
if (m == 0)
{
    x = 270; y = 20;
   Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
    aLocators.Insert(m, locator);
}
else if (m == 1)
{
    x = 20; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
    aLocators.Insert(m, locator);
}
else if (m == 2)
{
    x = 270; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
    aLocators.Insert(m, locator);
}
else if (m == 3)
{
    x = 520; y = 270;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
    aLocators.Insert(m, locator);
}
else if (m == 4)
{
    x = 270; y = 520;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    else if (combo1 == 3)
    {
        int x = 20, y = 20;
        int m = 0;
       
        while ((aLocators.Count) < 6)
        {
if (m == 0)
{
    x = 145; y = 100;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
    aLocators.Insert(m, locator);

}
else if (m == 1)
{
    x = 270; y = 100;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
    aLocators.Insert(m, locator);

}
else if (m == 2)
{
    x = 395; y = 100;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
    aLocators.Insert(m, locator);
}
else if (m == 3)
{
    x = 145; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
    aLocators.Insert(m, locator);

}
else if (m == 4)
{
    x = 270; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);

}
else if (m == 5)
{
    x = 395; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    
    //Deploy Beacons 
    if (checkBeacons == true)
    {
        int b = 0;
        while (aBeacons.Count < noBeacons)
        {
Beacon beacon = new Beacon(b, r.Next(iMaxX - 10), r.Next(iMaxY - 10), BeaconRadius);
aBeacons.Insert(b, beacon);
b++;
        }
    }
}
        }
    }
    else if (combo1 == 3)
    {
        int x = 20, y = 20;
        int m = 0;
       
        while ((aLocators.Count) < 6)
        {
if (m == 0)
{
    x = 145; y = 100;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle1);
    aLocators.Insert(m, locator);

}
else if (m == 1)
{
    x = 270; y = 100;
  Locator locator = new Locator(m, x, y, LocatorRadius, sangle2);
    aLocators.Insert(m, locator);

}
else if (m == 2)
{
    x = 395; y = 100;
Locator locator = new Locator(m, x, y, LocatorRadius, sangle3);
    aLocators.Insert(m, locator);
}
else if (m == 3)
{
    x = 145; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle4);
    aLocators.Insert(m, locator);

}
else if (m == 4)
{
    x = 270; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);

}
else if (m == 5)
{
    x = 395; y = 400;
 Locator locator = new Locator(m, x, y, LocatorRadius, sangle5);
    aLocators.Insert(m, locator);
}

m++;
        }
    }
    
    //Deploy Beacons 
    if (checkBeacons == true)
    {
        int b = 0;
        while (aBeacons.Count < noBeacons)
        {
Beacon beacon = new Beacon(b, r.Next(iMaxX - 10), r.Next(iMaxY - 10), BeaconRadius);
aBeacons.Insert(b, beacon);
b++;
        }
    }
}
    //Deployment of Beacons 
    if (checkBeacons == true)
    {
        int b = 0;
        while (aBeacons.Count < noBeacons)
        {
Beacon beacon = new Beacon(b, r.Next(iMaxX - 10), r.Next(iMaxY - 10), BeaconRadius);
aBeacons.Insert(b, beacon);
b++;
        }
    }
}

2- Wireless Sensor Node Class

C#
public class WirelessSensor
{                
 //packets between this node and any locator 
 public ArrayList apkts = null;  
 
 //packets between this node and any beacon 
 public ArrayList bpkts = null;  
 public ArrayList Connections;
 
 // the coordinates of the node
 public int x, y, dx, dy;
 
    //constructor for static sensor 
    public WirelessSensor(int x, int y) 
            {
                this.x = x;
                this.y = y;
               
                Connections = new ArrayList();              
             }
  
  //Mobile sensor const.
  public WirelessSensor(int x, int y, int dx, int dy) 
            {
                this.x = x;
                this.y = y;
                this.dx = dx;
                this.dy = dy;
                Connections = new ArrayList();    
            }
}
        }

3- Wireless Sensor Connection Class

If the distance between sensor node and other sensor node or locator node is less than the transmission range, this class will be called.

C#
// This class represents a communications link between 
// two wireless devices.

public class WirelessSensorConnection
{

      public Locator sLocator;   
      public Beacon sBeacon;
      
      // the upstream sensor
      public WirelessSensor sSender;  
      
      // the downstream sensor 
      public WirelessSensor sReceiver; 
      public pkt pkt = null;
      public int Sangle;	//start angle of locator sector
      public short Prlevel;	//Power levels for PTA algorithm

//constructor for directed antenna SeRLoc-HiRLoc     
 public WirelessSensorConnection(Locator sLocator, WirelessSensor
	   Receiver, int Sangle)   
 
            {
                this.sLocator = sLocator;
                this.sReceiver = sReceiver;
                this.Sangle = Sangle;
            }

//constructor for omnidirectional antenna (Centroid algorithm)
public WirelessSensorConnection(Locator sLocator, 
    WirelessSensor sReceiver) 
            {
                this.sLocator = sLocator;
                this.sReceiver = sReceiver;
            }

//constructor for sensder,reciver and power level for PTA algorithm
public WirelessSensorConnection(Locator sLocator, 
                WirelessSensor sReceiver, short Prlevel)            {
                this.sLocator = sLocator;
                this.sReceiver = sReceiver;
                this.Prlevel = Prlevel;
            }

//constructor for beacon,locator and start angle for sector (DIL algorithm)
 public WirelessSensorConnection(Beacon sBeacon, 
     Locator sLocator, WirelessSensor sReceiver, int Sangle) 
            {
                this.sBeacon = sBeacon;
                this.sLocator = sLocator;
                this.sReceiver = sReceiver;
                this.Sangle = Sangle;
            }
}           
        }

4- Mobile Sensor Node Class

C#
// This class represents (and manages) a list of mobile sensors.
public class Sensorlist
        {
    // the array of sensors
    public ArrayList aSensors;  

    // a mutex to control access to the sensor list
    public Mutex mutexSensor;
    
    // the maximum X and Y coordinates of a sensor	
    private int iMaxX, iMaxY;
    
    // a random number generator
    private Random r;

public Sensorlist(int iMaxX, int iMaxY, Random r)
{
    this.iMaxX = iMaxX;
    this.iMaxY = iMaxY;
    this.r = r;
    aSensors = new ArrayList();
    mutexSensor = new Mutex();
}

//Random movement (speed, direction)
// this function creates a new sensor and 
// adds it to the sensor list

     public void RandomMotion(int stepx, int stepy){
          int iBorder = r.Next(4);
          
    // add to left border, traveling right and up or down 
    if (iBorder == 0)  
    aSensors.Add(new WirelessSensor(-5, r.Next(iMaxY), 
        r.Next(stepx + 2) + stepx, r.Next(stepy + 4) - stepy));
        
    // add to top border, traveling down and left or right
    else if (iBorder == 1)  
        aSensors.Add(new WirelessSensor(r.Next(iMaxX), 
        -5, r.Next(stepx + 4) - stepx, r.Next(stepy + 2) + stepy));
        
    // add to right border, traveling left and up or down
    else if (iBorder == 2)  
        aSensors.Add(new WirelessSensor(iMaxX + 5, r.Next(iMaxY), 
        r.Next(stepx) - stepx, r.Next(stepy + 4) - stepy));

     // add to bottom border, traveling up and left or right 
     else if (iBorder == 3) 
         aSensors.Add(new WirelessSensor(r.Next(iMaxX), iMaxY + 5, 
         r.Next(stepx + 4) - stepx, r.Next(stepy) - stepy));
}
//straight movement (directions: right,left,top or down)
//speed: 1 step(dx or dy) = 8.333 m/sec = 30 km/hour,
// 2 step = 16.666 m/sec = //60 km/hour, and so on
public void DirectedMotion1(int stepx, int stepy) 
{   
        int iBorder = r.Next(4);  //four borders
        if (iBorder == 0)  // add to left border, traveling right 
  aSensors.Add(new WirelessSensor(20, r.Next(iMaxY), stepx, 0));
        else if (iBorder == 1)  // add to top border, traveling down 
  aSensors.Add(new WirelessSensor(r.Next(iMaxX), 20, 0, stepy));
         else if (iBorder == 2)  // add to right border, traveling left 
  aSensors.Add(new WirelessSensor(520, r.Next(iMaxY), -stepx, 0));
          else if (iBorder == 3)  // add to bottom border, traveling up 
  aSensors.Add(new WirelessSensor(r.Next(iMaxX), 520, 0, -stepy));
}

//straight movement (directions: right,left,top or down)
public void DirectedMotion2(int stepx, int stepy) 
{   
aSensors.Add(new WirelessSensor(r.Next(20, 520), 
   r.Next(20, 520), stepx, stepy));
}

//straight movement (directions: right,left,top or down)
public void DirectedMotion3(int stepx, int stepy){   
    
aSensors.Add(new WirelessSensor(r.Next(20, 520), 
   r.Next(20, 520), stepx, stepy));
       int iBorder = r.Next(4);  
       if (iBorder == 0) 
 aSensors.Add(new WirelessSensor(r.Next(iMaxX), 
   r.Next(iMaxY), stepx, 0));
       else if (iBorder == 1) 
 aSensors.Add(new WirelessSensor(r.Next(iMaxX), 
   r.Next(iMaxY), 0, stepy));
       else if (iBorder == 2) 
 aSensors.Add(new WirelessSensor(r.Next(iMaxX), 
   r.Next(iMaxY), -stepx, 0));
       else if (iBorder == 3) 
 aSensors.Add(new WirelessSensor(r.Next(iMaxX), 
   r.Next(iMaxY), 0, -stepy));
}

// the following function moves the sensors around, 
// and removes those that have traveled out of bounds
public void Update1()   //directed motion
{
    ArrayList aRemoveSensors = new ArrayList();
    foreach (WirelessSensor sensor in aSensors)
    {
if ((sensor.y == 20 || sensor.y == 80 || sensor.y == 140 || 
    sensor.y == 200 || sensor.y == 260 || sensor.y == 320 || 
    sensor.y == 380 || sensor.y == 440 || sensor.y == 500))
    sensor.x += sensor.dx;
else if ((sensor.y == 50 || sensor.y == 110 || sensor.y == 170 || 
   sensor.y == 230 || sensor.y == 290 || sensor.y == 350 || 
   sensor.y == 410 || sensor.y == 470 || sensor.y == 530))
     sensor.x -= sensor.dx;

f (sensor.x >= 520)
sensor.y += 30;
        else if (sensor.x <= 20)
sensor.y += 30;

if ((sensor.x < -5) || (sensor.y < -5) || (
    sensor.x > iMaxX + 5) || (sensor.y > iMaxY + 5))
         aRemoveSensors.Add(sensor);
    }
foreach (WirelessSensor sensor in aRemoveSensors)
        aSensors.Remove(sensor);
}

// this function moves the sensors around, and removes 
// those that have traveled out of bounds
          
public void Update()    //traditional motion
{
  ArrayList aRemoveSensors = new ArrayList();
    foreach (WirelessSensor sensor in aSensors)
    {
        sensor.x += sensor.dx;
        sensor.y += sensor.dy;

        if ((sensor.x < -5) || (sensor.y < -5) || (
            sensor.x > iMaxX + 5) || (sensor.y > iMaxY + 5))
aRemoveSensors.Add(sensor);
    }
    foreach (WirelessSensor sensor in aRemoveSensors)
        aSensors.Remove(sensor);
}
}

5- Packet Class

C#
public class pkt
        {
            public int x, y, sangle;
            public int ID;
            public short prlevel;

            //Omnidirectional antenna
            public pkt(int ID, int x, int y)    
            {
                this.ID = ID;
                this.x = x;
                this.y = y;
            }
            
   //Directed antenna         
   public pkt(int ID, int x, int y, int sangle)
            {
                this.ID = ID;
                this.x = x;
                this.y = y;
                this.sangle = sangle;
            }
  
  //omin-power levels
  public pkt(int ID, int x, int y, short prlevel) 
            {
                this.prlevel = prlevel;
                this.ID = ID;
                this.x = x;
                this.y = y;
            }
        }

6- Locator Node Class

Locator node is a sensor node but with larger transmission range and known position.

C#
public class Locator
        {
            public int id, x, y, dx, dy, sangle, dsangle;
            public int LocatorRadius;
            public short prlevel;

public Locator(int id, int x, int y, int LocatorRadius, int sangle)
            {
                this.x = x;
                this.y = y;
                this.id = id;
                this.LocatorRadius = LocatorRadius;
                this.sangle = sangle;
            }
        }

7- Beacon Node Class

Beacon node is the same as locator node. Used with DIL algorithm.

C#
public class Beacon
        {
            public int id, x, y;
            public int BeaconRadius;

            public Beacon(int id, int x, int y, int BeaconRadius)
            {
                this.x = x;
                this.y = y;
                this.id = id;
                this.BeaconRadius = BeaconRadius;
            }
        }

The second class of simulator component is developer interface class which contains all control functions (e.g. run and stop simulator, painting method, timer event handler,…etc.).

Paint method works as follows:

C#
 private void picNetwork_Paint(object sender, 
 System.Windows.Forms.PaintEventArgs e){
	
	if (network != null) {
        network.bPainting = true;
        network.bPaint = false;
	}
	
	Font font = new Font("Times New Roman", 7.0f);
	StringFormat format = new StringFormat();
	format.Alignment = StringAlignment.Center;
	Graphics g = e.Graphics;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
	
    // draw background
    g.FillRectangle(System.Drawing.Brushes.FloralWhite, e.ClipRectangle);
	
    // draw network objects (if network has been deployed)
	if (network != null) {

    // draw static sensor nodes
    int j = 0;
    foreach (networkdll.Class1.WirelessSensor sensor in network.aSensors)
      {
          g.DrawEllipse(Pens.Blue, sensor.x - 4, sensor.y - 4, 9, 9);
          g.FillEllipse(Brushes.Azure, sensor.x - 4, sensor.y - 4, 9, 9);
          j++;
      }

    // draw mobile sensor nodes 
    if ((network.bRunningSimulation == true) && (network.sensors != null))
      {
          network.sensors.mutexSensor.WaitOne();
          foreach (networkdll.Class1.WirelessSensor sensor in network.sensors.aSensors)
          {
              g.DrawEllipse(Pens.Blue, sensor.x - 4, sensor.y - 4, 9, 9);
              g.FillEllipse(Brushes.Azure, sensor.x - 4, sensor.y - 4, 9, 9);
          }
          network.sensors.mutexSensor.ReleaseMutex();
      }

      Pen locatorPen = Pens.Black;
      Pen sectorPen1 = Pens.Green;
      Pen sectorPen2 = Pens.Brown;
      Pen sectorPen3 = Pens.Red;
      Pen sectorPen4 = Pens.Orange;
      Pen sectorPen5 = Pens.Olive;

      Brush lbrush = Brushes.Red;
      Brush lBrush = Brushes.CadetBlue;
      Brush Brush = Brushes.Black;
      int prsep = 50; //separation between power levels
      if (network.LocatorRadius > 100)
      {
          if (network.combo3 == 0) prsep = 50;
          else if (network.combo3 == 1) prsep = 20;
      }
      else
      {
if (network.combo3 == 0) prsep = 10;
else if (network.combo3 == 1) prsep = 5;
      }
//draw Locator nodes
         foreach (networkdll.Class1.Locator locator in network.aLocators)
      {
if (int.Parse(textBox10.Text) > 8 && comboBox1.SelectedIndex==0)
{
   if (checkBox6.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius, 
   locator.y - network.LocatorRadius, network.LocatorRadius * 2, 
   network.LocatorRadius * 2, 
   network.sangle1, network.Sweepangle);
    if (checkBox14.Checked)
    {
  g.DrawEllipse(sectorPen2, locator.x - int.Parse(textBox8.Text) + prsep, 
  locator.y - int.Parse(textBox8.Text) + prsep, 
  (int.Parse(textBox8.Text) - prsep) * 2, 
  (int.Parse(textBox8.Text) - prsep) * 2);
        
 g.DrawEllipse(sectorPen3, locator.x - int.Parse(textBox8.Text) + 
 2 * prsep, locator.y - int.Parse(textBox8.Text) + 2 * prsep, 
 (int.Parse(textBox8.Text) - 2 * prsep) * 2, (int.Parse(textBox8.Text) - 2 * prsep) * 2);
        
g.DrawEllipse(sectorPen4, locator.x - int.Parse(textBox8.Text) + 3 * prsep, 
locator.y - int.Parse(textBox8.Text) + 3 * prsep, 
(int.Parse(textBox8.Text) - 3 * prsep) * 2, 
(int.Parse(textBox8.Text) - 3 * prsep) * 2);
       
 g.DrawEllipse(sectorPen5, locator.x - int.Parse(textBox8.Text) + 
 4 * prsep, locator.y - int.Parse(textBox8.Text) + 4 * prsep, 
 (int.Parse(textBox8.Text) - 4 * prsep) * 2, 
 (int.Parse(textBox8.Text) - 4 * prsep) * 2);
    }
   
    g.DrawEllipse(locatorPen, locator.x - 5, locator.y - 5, 10, 10);
    g.FillEllipse(lBrush, locator.x - 5, locator.y - 5, 10, 10);
    g.DrawString(locator.id.ToString(), font, lbrush, 
    locator.x + 5, locator.y + 5);
    g.DrawString("(" + locator.x + " , " + locator.y + 
       ")", font, lbrush, locator.x + 5, locator.y - 15);
}
else
{
    if (locator.id == 0)
    {
 if (checkBox5.Checked) g.DrawPie(sectorPen1, 
 locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
 network.LocatorRadius * 2, network.LocatorRadius * 2, 
 network.sangle1, network.Sweepangle);

if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2);
    if (checkBox14.Checked)
        {

g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius +
 prsep, locator.y - network.LocatorRadius + prsep, 
 (network.LocatorRadius - prsep) * 2, 
 (network.LocatorRadius - prsep) * 2);

g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
(network.LocatorRadius - 2 * prsep) * 2, 
(network.LocatorRadius - 2 * prsep) * 2);
  
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
(network.LocatorRadius - 3 * prsep) * 2, 
(network.LocatorRadius - 3 * prsep) * 2);
  
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
(network.LocatorRadius - 4 * prsep) * 2, 
(network.LocatorRadius - 4 * prsep) * 2);
        }
    }
        if (locator.id == 1)
    {
        
if (checkBox5.Checked) g.DrawPie(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2, 
network.sangle2, network.Sweepangle);
        
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2);
        
        if (checkBox14.Checked)
        {
  g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + 
  prsep, locator.y - network.LocatorRadius + prsep, 
  (network.LocatorRadius - prsep) * 2, (network.LocatorRadius - prsep) * 2);
   g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
   2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
   (network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
 g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
 3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
 (network.LocatorRadius - 3 * prsep) * 2, (network.LocatorRadius - 3 * prsep) * 2);
 g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
 4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
 (network.LocatorRadius - 4 * prsep) * 2, (network.LocatorRadius - 4 * prsep) * 2);
        }
    }
    if (locator.id == 2)
    {
        
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius, 
locator.y - network.LocatorRadius, network.LocatorRadius * 2,
 network.LocatorRadius * 2, network.sangle3, network.Sweepangle);
      
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, locator.x - network.LocatorRadius, 
locator.y - network.LocatorRadius, network.LocatorRadius * 2, network.LocatorRadius * 2);
        if (checkBox14.Checked)
        {
  
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep, 
locator.y - network.LocatorRadius + prsep, (network.LocatorRadius - prsep) * 2,
 (network.LocatorRadius - prsep) * 2);
 
 g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
 2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
 (network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
 
 g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
 3 * prsep, locator.y - network.LocatorRadius + 3 * prsep,
  (network.LocatorRadius - 3 * prsep) * 2, 
  (network.LocatorRadius - 3 * prsep) * 2);
  
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
(network.LocatorRadius - 4 * prsep) * 2, (network.LocatorRadius - 4 * prsep) * 2);
        }
    }
    if (locator.id == 3)
    {
if (checkBox5.Checked) g.DrawPie(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2, network.sangle4, network.Sweepangle);
      
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius,
 network.LocatorRadius * 2, network.LocatorRadius * 2); 
        
        if (checkBox14.Checked)
        {  
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + prsep,
 locator.y - network.LocatorRadius + prsep, 
 (network.LocatorRadius - prsep) * 2, (network.LocatorRadius - prsep) * 2);
  
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
(network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
 
 g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
 3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
 (network.LocatorRadius - 3 * prsep) * 2, (network.LocatorRadius - 3 * prsep) * 2);
  
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
(network.LocatorRadius - 4 * prsep) * 2, (network.LocatorRadius - 4 * prsep) * 2);
        }
    }
    if (locator.id == 4)
    {
        
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius, 
locator.y - network.LocatorRadius, network.LocatorRadius * 2, 
network.LocatorRadius * 2, network.sangle5, network.Sweepangle);
      
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2);
        if (checkBox14.Checked)
        { 
 g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + 
 prsep, locator.y - network.LocatorRadius + prsep, 
 (network.LocatorRadius - prsep) * 2, (network.LocatorRadius - prsep) * 2);
 
 g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
 2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
 (network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
 
 g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
 3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
 (network.LocatorRadius - 3 * prsep) * 2, (network.LocatorRadius - 3 * prsep) * 2);

  g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
  4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
  (network.LocatorRadius - 4 * prsep) * 2, (network.LocatorRadius - 4 * prsep) * 2);
        }
    }
    
  if (locator.id == 5)
    {        
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius, 
locator.y - network.LocatorRadius, network.LocatorRadius * 2, 
network.LocatorRadius * 2, network.sangle6, network.Sweepangle);
      
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2);
        
if (checkBox14.Checked)
        {  
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + 
prsep, locator.y - network.LocatorRadius + prsep, 
(network.LocatorRadius - prsep) * 2, (network.LocatorRadius - prsep) * 2);
  
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
(network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
 
 g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
 3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
 (network.LocatorRadius - 3 * prsep) * 2, (network.LocatorRadius - 3 * prsep) * 2);
 
 g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
 4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
 (network.LocatorRadius - 4 * prsep) * 2, (network.LocatorRadius - 4 * prsep) * 2);
        }
    }
    if (locator.id == 6)
    {        
if (checkBox5.Checked) g.DrawPie(sectorPen1, locator.x - network.LocatorRadius, 
locator.y - network.LocatorRadius, network.LocatorRadius * 2, 
network.LocatorRadius * 2, network.sangle7, network.Sweepangle);
      
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2);
        if (checkBox14.Checked)
        { 
 g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + 
 prsep, locator.y - network.LocatorRadius + prsep, 
 (network.LocatorRadius - prsep) * 2, (network.LocatorRadius - prsep) * 2);
  
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 
2 * prsep, locator.y - network.LocatorRadius + 2 * prsep, 
(network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
  
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
(network.LocatorRadius - 3 * prsep) * 2, (network.LocatorRadius - 3 * prsep) * 2);


g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
(network.LocatorRadius - 4 * prsep) * 2, 
(network.LocatorRadius - 4 * prsep) * 2);
        }
    }
    if (locator.id == 7)
    {
        
if (checkBox5.Checked) g.DrawPie(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2, network.sangle8, network.Sweepangle);
      
if (checkBox6.Checked) g.DrawEllipse(sectorPen1, 
locator.x - network.LocatorRadius, locator.y - network.LocatorRadius, 
network.LocatorRadius * 2, network.LocatorRadius * 2);
        if (checkBox14.Checked)
        {
  
g.DrawEllipse(sectorPen2, locator.x - network.LocatorRadius + 
prsep, locator.y - network.LocatorRadius + prsep, 
(network.LocatorRadius - prsep) * 2, (network.LocatorRadius - prsep) * 2);
  
g.DrawEllipse(sectorPen3, locator.x - network.LocatorRadius + 2 * prsep, 
locator.y - network.LocatorRadius + 2 * prsep, 
(network.LocatorRadius - 2 * prsep) * 2, (network.LocatorRadius - 2 * prsep) * 2);
  
g.DrawEllipse(sectorPen4, locator.x - network.LocatorRadius + 
3 * prsep, locator.y - network.LocatorRadius + 3 * prsep, 
(network.LocatorRadius - 3 * prsep) * 2, (network.LocatorRadius - 3 * prsep) * 2);
  
g.DrawEllipse(sectorPen5, locator.x - network.LocatorRadius + 
4 * prsep, locator.y - network.LocatorRadius + 4 * prsep, 
(network.LocatorRadius - 4 * prsep) * 2, (network.LocatorRadius - 4 * prsep) * 2);
        }
    }

  g.DrawEllipse(locatorPen, locator.x - 5, locator.y - 5, 10, 10);
  g.FillEllipse(lBrush, locator.x - 5, locator.y - 5, 10, 10);
  g.DrawString(locator.id.ToString(), font, lbrush, locator.x + 5, locator.y + 5);
  g.DrawString("(" + locator.x + " , " + locator.y + ")", font, 
		lbrush, locator.x + 5, locator.y - 15);
}
      }
      
//Draw Beacon Nodes
       
         foreach (networkdll.Class1.Beacon beacon in network.aBeacons)
      {
if (beacon.id < network.noBeacons)  
 g.DrawEllipse(sectorPen1, beacon.x - network.BeaconRadius, 
 beacon.y - network.BeaconRadius, network.BeaconRadius * 2, 
 network.BeaconRadius * 2);
         
 g.FillEllipse(lbrush, beacon.x - 5, beacon.y - 5, 10, 10);
 g.DrawString(beacon.id.ToString(), font, Brush, beacon.x + 5, 
 beacon.y + 5);
       }
       
      if (radioButton1.Checked)   //static
      {
if (checkBox1.Checked)   //Estimate Position
{
    if (checkBox11.Checked)
    {
//here developers are allowed to call the constructor of their localization //algorithms
HiRLoc.HiRLoc algorithm3 = new HiRLoc.HiRLoc(sender, e, 
network.aSensors, network.aLocators, checkBox2.Checked, 
checkBox3.Checked, checkBox4.Checked, checkBox7.Checked, 
checkBox8.Checked, checkBox9.Checked, checkBox10.Checked,
network.LocatorRadius, network.Sweepangle, network.sangle1,
network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text);  //HiRLoc Algorithm
    }
    if (checkBox12.Checked)
    {
SeRLoc.SeRLoc algorithm2 = new SeRLoc.SeRLoc(sender, e, 
network.aSensors, network.aLocators, checkBox2.Checked, 
checkBox3.Checked, checkBox4.Checked, checkBox7.Checked, 
checkBox8.Checked, checkBox9.Checked, checkBox10.Checked, 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7, 
network.sangle8, txtOutputFolder.Text);  //SeRLoc Algorithm
    }
    if (checkBox13.Checked)
    {
centroid.centroid algorithm1 = new centroid.centroid(
sender, e, network.aSensors, network.aLocators, 
checkBox2.Checked, checkBox3.Checked, checkBox4.Checked, 
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, 
checkBox10.Checked, network.LocatorRadius, txtOutputFolder.Text);  //Centroid Algorithm
    }
    if (checkBox14.Checked)
    {
PTA.PTA algorithm6 = new PTA.PTA(sender, e, network.aSensors, 
network.aLocators,/* checkBox2.Checked,*/ checkBox14.Checked,
network.LocatorRadius, comboBox3.SelectedIndex, 
txtOutputFolder.Text);  //PTA Algorithm
    }
    if (checkBox15.Checked)
    {
ADLA.ADLA algorithm4 = new ADLA.ADLA(sender, e, 
network.aSensors, network.aLocators, radioButton3.Checked, 
radioButton4.Checked, int.Parse(textBox3.Text), 
int.Parse(textBox4.Text), int.Parse(textBox5.Text), 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, network.sangle5, 
network.sangle6, network.sangle7, network.sangle8, 
txtOutputFolder.Text);  //ADLA Algorithm
    }
  
    if (checkBox16.Checked)
    {
HADLA.HADLA algorithm5 = new HADLA.HADLA(sender, e, 
network.aSensors, network.aLocators, radioButton3.Checked, 
radioButton4.Checked, int.Parse(textBox3.Text), 
int.Parse(textBox4.Text), int.Parse(textBox5.Text), 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7, 
network.sangle8, txtOutputFolder.Text);  //HADLA Algorithm
    }
    if (checkBox17.Checked)
    {
DIL.DIL algorithm7 = new DIL.DIL(sender, e, 
network.aSensors, network.aLocators, network.aBeacons, 
radioButton3.Checked, radioButton4.Checked, 
int.Parse(textBox3.Text), double.Parse(textBox4.Text), 
int.Parse(textBox5.Text), network.LocatorRadius, 
network.BeaconRadius, network.Sweepangle, network.sangle1,
network.sangle2, network.sangle3, network.sangle4,
network.sangle5, network.sangle6, network.sangle7,
network.sangle8, txtOutputFolder.Text);  //DIL Algorithm
    }
    if (checkBox18.Checked)
    {
RAL.RAL algorithm8 = new RAL.RAL(sender, e, network.aSensors, 
network.aLocators, checkBox1.Checked, network.LocatorRadius, 
network.Sweepangle,network.sangle1, network.sangle2, 
network.sangle3, network.sangle4, network.sangle5, network.sangle6, 
network.sangle7, network.sangle8, txtOutputFolder.Text);  //RAL Algorithm
    }
    network.BeaconRadius = trackBar1.Value;
    network.LocatorRadius = trackLocatorRadius.Value;
    network.Sweepangle = trackSweepAngle.Value;
    network.sangle1 = tracksangle1.Value;
    network.sangle2 = tracksangle2.Value;
    network.sangle3 = tracksangle3.Value;
    network.sangle4 = tracksangle4.Value;
    network.sangle5 = tracksangle5.Value;
    network.sangle6 = tracksangle6.Value;
    network.sangle7 = tracksangle7.Value;
    network.sangle8 = tracksangle8.Value;
    network.combo3 = comboBox3.SelectedIndex;
    label1.Text = trackLocatorRadius.Value.ToString();
    label2.Text = trackBar1.Value.ToString();
    label3.Text = trackSweepAngle.Value.ToString();
}
      }
    

      if (radioButton2.Checked)       //Mobile
      {
if (checkBox1.Checked)   //Estimate Position
{    
    network.sensors.mutexSensor.WaitOne();

    if (checkBox11.Checked)
    {
HiRLoc.HiRLoc algorithm3 = new HiRLoc.HiRLoc(sender, e, 
network.sensors.aSensors, network.aLocators, checkBox2.Checked, 
checkBox3.Checked, checkBox4.Checked, checkBox7.Checked, 
checkBox8.Checked, checkBox9.Checked, checkBox10.Checked, 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7, network.sangle8, 
txtOutputFolder.Text);  //HiRLoc Algorithm
    }
    if (checkBox12.Checked)
    {
SeRLoc.SeRLoc algorithm2 = new SeRLoc.SeRLoc(sender, e, 
network.sensors.aSensors, network.aLocators, checkBox2.Checked, 
checkBox3.Checked, checkBox4.Checked, checkBox7.Checked, 
checkBox8.Checked, checkBox9.Checked, checkBox10.Checked, 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4,
 network.sangle5, network.sangle6, network.sangle7, 
 network.sangle8, txtOutputFolder.Text);  //SeRLoc Algorithm
    }
    if (checkBox13.Checked)
    {
centroid.centroid algorithm1 = new centroid.centroid(sender, 
e, network.sensors.aSensors, network.aLocators, 
checkBox2.Checked, checkBox3.Checked, checkBox4.Checked, 
checkBox7.Checked, checkBox8.Checked, checkBox9.Checked, 
checkBox10.Checked, network.LocatorRadius, txtOutputFolder.Text);//Centroid //Algorithm
    }
    if (checkBox14.Checked)
    {
PTA.PTA algorithm6 = new PTA.PTA(sender, e, 
network.sensors.aSensors, network.aLocators, checkBox14.Checked, 
network.LocatorRadius, comboBox3.SelectedIndex, 
txtOutputFolder.Text);  //PTA Algorithm
    }
    if (checkBox15.Checked)
    {
ADLA.ADLA algorithm4 = new ADLA.ADLA(sender, e, 
network.sensors.aSensors, network.aLocators, radioButton3.Checked, 
radioButton4.Checked, int.Parse(textBox3.Text), 
int.Parse(textBox4.Text), int.Parse(textBox5.Text), 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7, 
network.sangle8, txtOutputFolder.Text);  //ADLA Algorithm
    }
    if (checkBox16.Checked)
    {
HADLA.HADLA algorithm5 = new HADLA.HADLA(sender, e, 
network.sensors.aSensors, network.aLocators, radioButton3.Checked, 
radioButton4.Checked, int.Parse(textBox3.Text), 
int.Parse(textBox4.Text), int.Parse(textBox5.Text), 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7, 
network.sangle8, txtOutputFolder.Text);  //HADLA Algorithm
    }
   
    if (checkBox17.Checked)
    {
DIL.DIL algorithm7 = new DIL.DIL(sender, e, network.sensors.aSensors, 
network.aLocators, network.aBeacons, radioButton3.Checked, 
radioButton4.Checked, int.Parse(textBox3.Text), 
int.Parse(textBox4.Text), int.Parse(textBox5.Text), 
network.LocatorRadius, network.BeaconRadius, network.Sweepangle, 
network.sangle1, network.sangle2, network.sangle3, network.sangle4, 
network.sangle5, network.sangle6, network.sangle7, network.sangle8, 
txtOutputFolder.Text);  //DIL Algorithm
    }
    if (checkBox18.Checked)
    {
RAL.RAL algorithm8 = new RAL.RAL(sender, e, 
network.sensors.aSensors, network.aLocators, checkBox1.Checked, 
network.LocatorRadius, network.Sweepangle, network.sangle1, 
network.sangle2, network.sangle3, network.sangle4, network.sangle5, 
network.sangle6, network.sangle7, network.sangle8, txtOutputFolder.Text);
//RAL Algorithm
    }
    network.sensors.mutexSensor.ReleaseMutex();
    
    network.BeaconRadius = trackBar1.Value;
    network.LocatorRadius = trackLocatorRadius.Value;
    network.Sweepangle = trackSweepAngle.Value;
    network.sangle1 = tracksangle1.Value;
    network.sangle2 = tracksangle2.Value;
    network.sangle3 = tracksangle3.Value;
    network.sangle4 = tracksangle4.Value;
    network.sangle5 = tracksangle5.Value;
    network.sangle6 = tracksangle6.Value;
    network.sangle7 = tracksangle7.Value;
    network.sangle8 = tracksangle8.Value;
    network.combo3 = comboBox3.SelectedIndex;
    label1.Text = trackLocatorRadius.Value.ToString();
    label2.Text = trackBar1.Value.ToString();
    label3.Text = trackSweepAngle.Value.ToString();
}
      }
      }
	// finish painting
	if (network != null)
network.bPainting = false;
}

What is Going on Inside the Code Snippets?

The second component of our framework is localization algorithm that is written in a DLL file by a developer, then referencing it to the simulator and running it. We take a sample localization algorithm as an example to show how the code actually works.

High Resolution Localization algorithm (HiRLoc)

Any localization algorithm consists of four methods: set_parameters, connect, send and localize. These methods differ from one another.

Localization Algorithm Constructor

C#
public HiRLoc(object sender, PaintEventArgs e, ArrayList asensor, 
    ArrayList alocator, bool checkBox2, bool checkBox3, 
    bool checkBox4, bool checkBox5, bool checkBox6, bool checkBox7, 
    bool checkBox8, float locatorRadius, float sweepangle, int sangle1, 
    int sangle2, int sangle3, int sangle4, int sangle5, int sangle6, 
    int sangle7, int sangle8, String textbox)
        {

set_parameters(locatorRadius, sweepangle, sangle1, sangle2, 
    sangle3, sangle4, sangle5, sangle6, sangle7, sangle8);
connect(asensor, alocator);
            send(asensor);
localiza(sender, e, asensor, checkBox2, checkBox3, checkBox4,
    checkBox5, checkBox6, checkBox7, checkBox8, locatorRadius, textbox);        

        }

1- Set_parameters method is used to receive initialization parameters from user interface.

C#
public void set_parameters(float locatorRadius, float sweepangle, 
    int sangle1, int sangle2, int sangle3, int sangle4, 
    int sangle5, int sangle6, int sangle7, int sangle8)
        {
            this.locatorRadius = locatorRadius;
            this.sweepangle = sweepangle;
            this.sangle1 = sangle1;
            this.sangle2 = sangle2;
            this.sangle3 = sangle3;
            this.sangle4 = sangle4;
            this.sangle5 = sangle5;
            this.sangle6 = sangle6;
            this.sangle7 = sangle7;
            this.sangle8 = sangle8;

        }

2- Connect method is used to test the connectivity between sensor node and locator node.

C#
public void connect(ArrayList asensor, ArrayList alocator)
{
    for (int j = 0; j < asensor.Count; j++)
    {
       networkdll.Class1.WirelessSensor jSensor = 
           networkdll.Class1.WirelessSensor)asensor[j];
       jSensor.apkts = new ArrayList();
       jSensor.Connections = new ArrayList();

for (int i = 0; i < alocator.Count; i++)
{
    networkdll.Class1.Locator iLocator = (
        networkdll.Class1.Locator)alocator[i];
    if (i == 0) iLocator.sangle = sangle1;
    if (i == 1) iLocator.sangle = sangle2;
    if (i == 2) iLocator.sangle = sangle3;
    if (i == 3) iLocator.sangle = sangle4;
    if (i == 4) iLocator.sangle = sangle5;
    if (i == 5) iLocator.sangle = sangle6;
    if (i == 6) iLocator.sangle = sangle7;
    if (i == 7) iLocator.sangle = sangle8;
     
 float lsRadius = (float)(Math.Sqrt(Math.Pow(
     iLocator.x - jSensor.x, 2) + Math.Pow(iLocator.y - jSensor.y, 2)));
 int theta1 = (int)Math.Floor((Math.Acos(Math.Abs(
     jSensor.x - iLocator.x) / lsRadius) * (180 * 7 / 22)));
 int theta = (int)Math.Floor((Math.Acos(Math.Abs(
     jSensor.y - iLocator.y) / lsRadius) * (180 * 7 / 22)));
  int theta2 = theta + 90;
  int theta3 = theta1 + 180;
  int theta4 = 270 + theta;
  start = System.DateTime.Now.Ticks;

    if (lsRadius <= locatorRadius)   
    {
   int res = 5;    //resolution for rotation
  if (jSensor.x >= iLocator.x && jSensor.y >= iLocator.y)  //Region 1
{
  if (theta1 >= iLocator.sangle && theta1 <= (
     iLocator.sangle + sweepangle))
    {
  if (theta1 == iLocator.sangle && theta1 <= (
     iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
    iLocator, jSensor, (iLocator.sangle - res)));
}

else
{
    for (int k = 1; k < sweepangle; k++)
    {
if (theta1 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
    iLocator, jSensor, (iLocator.sangle - res))); break;//,iLocator.sweep));
}
     else iLocator.sangle = (iLocator.sangle + 1);
    }
}
    }
}
     //Region 2
     else if (jSensor.x < iLocator.x && 
         jSensor.y >= iLocator.y)
{
   if (theta2 >= iLocator.sangle && theta2 <= (
       iLocator.sangle + sweepangle))
    {
    if (theta2 == iLocator.sangle && theta2 <= (
       iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
   iLocator, jSensor, (iLocator.sangle - res)));
}
       else
{
 for (int k = 1; k < sweepangle; k++)
    {
if (theta2 == iLocator.sangle)
{
 jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
    iLocator, jSensor, (iLocator.sangle - res))); break;
}
   else iLocator.sangle = (iLocator.sangle + 1);
    }
}
    }
}
  //Region 3
  else if (jSensor.x <= iLocator.x && jSensor.y < iLocator.y)  
{
  if (theta3 >= iLocator.sangle && theta3 <= (
    iLocator.sangle + sweepangle))
    {
  if (theta3 == iLocator.sangle && theta3 <= (
     iLocator.sangle + sweepangle))
{
 jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
     iLocator, jSensor, (iLocator.sangle - res)));
}
       

else
{
    for (int k = 1; k < sweepangle; k++)
    {
if (theta3 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
   iLocator, jSensor, (iLocator.sangle - res))); break;
}
      else iLocator.sangle = (iLocator.sangle + 1);
    }
}
    }
}
  //Region 4
  else if (jSensor.x > iLocator.x && jSensor.y < iLocator.y)  
{
  if (theta4 >= iLocator.sangle && theta4 <= (
     iLocator.sangle + sweepangle))
    {
 if (theta4 == iLocator.sangle && theta4 <= (
   iLocator.sangle + sweepangle))
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
   iLocator, jSensor, (iLocator.sangle - res)));
}
else
{
     for (int k = 1; k < sweepangle; k++)
    {
if (theta4 == iLocator.sangle)
{
jSensor.Connections.Add(new networkdll.Class1.WirelessSensorConnection(
   iLocator, jSensor, (iLocator.sangle - res))); break;
}
else iLocator.sangle = (iLocator.sangle + 1);
    }
}
    }
}
    }
}
    }
}

3- Send method: When the connection is established between sender and receiver, the sender will carry out sending packets using send method.

C#
public void send(ArrayList asensor)
{
      foreach (networkdll.Class1.WirelessSensor sensor in asensor)
    {
 foreach (networkdll.Class1.WirelessSensorConnection connection in sensor.Connections)
{
    sensor.apkts.Add(new networkdll.Class1.pkt(connection.sLocator.id, 
	connection.sLocator.x, connection.sLocator.y, connection.Sangle));
}
    }
}

4- Localize method is the core of localization algorithm, where all calculations run.

C#
public void localize(object sender, PaintEventArgs e, 
    ArrayList asensor, bool checkBox2, bool checkBox3, 
    bool checkBox4, bool checkBox5, bool checkBox6, 
    bool checkBox7, bool checkBox8, float locatorRadius, 
    String textbox)
{   
    Graphics g = e.Graphics;
    Brush sbrush = Brushes.BlueViolet;
    
    //printing into Excel
    String opc8 = "", opc7 = "", opc6 = "", opc5 = "", 
       opc4 = "", opc3 = "", opc2 = "";   
    String op8 = "", op7 = "", op6 = "", op5 = "", 
       op4 = "", op3 = "", op2 = "";   
    String time = "";   //to calculate algorithm running time
    int d = 0;
    int ersum2 = 0, count2 = 0, ersum3 = 0, count3 = 0, 
        ersum4 = 0, count4 = 0, ersum5 = 0, count5 = 0, 
        ersum6 = 0, count6 = 0, ersum7 = 0, count7 = 0, 
        ersum8 = 0, count8 = 0;
    foreach (networkdll.Class1.WirelessSensor sensor in asensor)
    {
        int[] hLHs = new int[9]; //Locators heard by sensor node
        int[] hax = new int[9];  //x coordinate of locators heard
        int[] hay = new int[9];  //y coordinate of locators heard
        int[] hsa = new int[9];  //start angle of locators heard
        int t2 = 0, Xest, Yest;
foreach (networkdll.Class1.pkt packet in sensor.apkts)   
{
    hLHs[t2] = packet.ID;
    hax[t2] = packet.x;
    hay[t2] = packet.y;
    hsa[t2] = packet.sangle;
    t2++;
    
if (sensor.apkts.Count == 8 && t2 == 8)  //eight sectors
{
    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(Math.Pow(hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
    float lp6Radius = (float)Math.Sqrt(Math.Pow(hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));
    float lp7Radius = (float)Math.Sqrt(Math.Pow(hax[6] - xg, 2) + Math.Pow(hay[6] - yg, 2));
    float lp8Radius = (float)Math.Sqrt(Math.Pow(hax[7] - xg, 2) + Math.Pow(hay[7] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(
    Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(
    Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
    if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(
   Math.Abs(hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(
   Math.Abs(hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
    if (theta1 >= hsa[5] && 
        theta1 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
    if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 10)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
    if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 10)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
    if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 10)) gridscore++;
}
    }
    if (lp7Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[6] - xg) / lp7Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[6] - yg) / lp7Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[6] && yg >= hay[6])  //Region 1
{
    if (theta1 >= hsa[6] && theta1 <= (hsa[6] + 10)) gridscore++;
}
else if (xg <= hax[6] && yg >= hay[6])  //Region 2
{
    if (theta2 >= hsa[6] && theta2 <= (hsa[6] + 10)) gridscore++;

}
else if (xg <= hax[6] && yg <= hay[6])  //Region 3
{
    if (theta3 >= hsa[6] && theta3 <= (hsa[6] + 10)) gridscore++;

}
else if (xg >= hax[6] && yg <= hay[6])  //Region 4
{
    if (theta4 >= hsa[6] && theta4 <= (hsa[6] + 10)) gridscore++;
}
    }
    if (lp8Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
   hax[7] - xg) / lp8Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
   hay[7] - yg) / lp8Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[7] && yg >= hay[7])  //Region 1
{
    if (theta1 >= hsa[7] && theta1 <= (hsa[7] + 10)) gridscore++;
}
else if (xg <= hax[7] && yg >= hay[7])  //Region 2
{
    if (theta2 >= hsa[7] && theta2 <= (hsa[7] + 10)) gridscore++;

}
else if (xg <= hax[7] && yg <= hay[7])  //Region 3
{
    if (theta3 >= hsa[7] && theta3 <= (hsa[7] + 10)) gridscore++;

}
else if (xg >= hax[7] && yg <= hay[7])  //Region 4
{
    if (theta4 >= hsa[7] && theta4 <= (hsa[7] + 10)) gridscore++;
}
    }
    if (gridscore == 8)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count8++;
ersum8 += (int)Math.Sqrt(erx * erx + ery * ery);
op8 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
   "," + sensor.x + "," + sensor.y + "," + erx + "," + ery + "," + 
   (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
 }
 if (count8 != 0) opc8 = "," + count8 + "," + "," + "," + "," + 
    " Average Localization Error =" + (ersum8 / count8) + "\n";
    }

    if (checkBox7)
    {
if (sensor.apkts.Count == 7 && t2 == 7)     //seven sectors
{
    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(
        hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(
        hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(
        hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(Math.Pow(
        hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
    float lp6Radius = (float)Math.Sqrt(Math.Pow(
        hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));
    float lp7Radius = (float)Math.Sqrt(Math.Pow(
        hax[6] - xg, 2) + Math.Pow(hay[6] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
    if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
    if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
    if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 10)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
    if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 10)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
    if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 10)) gridscore++;
}
    }
    if (lp7Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[6] - xg) / lp7Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[6] - yg) / lp7Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[6] && yg >= hay[6])  //Region 1
{
    if (theta1 >= hsa[6] && theta1 <= (hsa[6] + 10)) gridscore++;
}
else if (xg <= hax[6] && yg >= hay[6])  //Region 2
{
    if (theta2 >= hsa[6] && theta2 <= (hsa[6] + 10)) gridscore++;

}
else if (xg <= hax[6] && yg <= hay[6])  //Region 3
{
    if (theta3 >= hsa[6] && theta3 <= (hsa[6] + 10)) gridscore++;

}
else if (xg >= hax[6] && yg <= hay[6])  //Region 4
{
    if (theta4 >= hsa[6] && theta4 <= (hsa[6] + 10)) gridscore++;
}
    }
    if (gridscore == 7)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count7++;
ersum7 += (int)Math.Sqrt(erx * erx + ery * ery);
op7 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
    "," + sensor.x + "," + sensor.y + "," + erx + "," + ery + 
    "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count7 != 0) opc7 = "," + count7 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum7 / count7) + "\n";
    }

    if (checkBox6)
    {
if (sensor.apkts.Count == 6 && t2 == 6)  //six sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(
        Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(
        hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(
        hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(Math.Pow(
        hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
    float lp6Radius = (float)Math.Sqrt(Math.Pow(
        hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && 
        theta1 <= (hsa[0] + 15)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0]) //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 15)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 15)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 15)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && 
       theta1 <= (hsa[1] + 15)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && 
       theta2 <= (hsa[1] + 15)) gridscore++;

}
else if (xg <= hax[1] && 
    yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && 
        theta3 <= (hsa[1] + 15)) gridscore++;

}
else if (xg >= hax[1] && 
    yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && 
       theta4 <= (hsa[1] + 15)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(
   Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(
   Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && 
        theta1 <= (hsa[2] + 15)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && 
        theta2 <= (hsa[2] + 15)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && 
       theta3 <= (hsa[2] + 15)) gridscore++;

}
else if (xg >= hax[2] && 
   yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && 
        theta4 <= (hsa[2] + 15)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
   hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 15)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 15)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 15)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 15)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
   hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 15)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 15)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 15)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 15)) gridscore++;
}
    }
    if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
    if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 15)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
    if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 15)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
    if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 15)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
    if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 15)) gridscore++;
}
    }
    if (gridscore == 6)
    {
MG++; Xest += xg; Yest += yg;
//g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count6++;
ersum6 += (int)Math.Sqrt(erx * erx + ery * ery);
op6 += "," + d + "," + (Xest / MG) + "," +
    (Yest / MG) + "," + sensor.x + "," + sensor.y + 
    "," + erx + "," + ery + "," + (int)Math.Sqrt(
    erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count6 != 0) opc6 = "," + count6 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum6 / count6) + "\n";
    }
    if (checkBox5)
    {
if (sensor.apkts.Count == 5 && t2 == 5)     //five sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(
        Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(
        Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(
        Math.Pow(hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(
        Math.Pow(hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(Math.Pow(
        hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
    if (gridscore == 5)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count5++;
ersum5 += (int)Math.Sqrt(erx * erx + ery * ery);
op5 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + "," + 
   sensor.x + "," + sensor.y + "," + erx + "," + ery + "," + 
   (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count5 != 0) opc5 = "," + count5 + "," + "," + "," + "," + 
    " Average Localization Error =" + (ersum5 / count5) + "\n";
    }

    if (checkBox4)
    {

if (sensor.apkts.Count == 4 && t2 == 4)     //four sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(
        hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(
        hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(
         hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && 
        theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }

    if (gridscore == 4)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count4++;
ersum4 += (int)Math.Sqrt(erx * erx + ery * ery);
op4 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
    "," + sensor.x + "," + sensor.y + "," + erx + "," + ery + 
    "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count4 != 0) opc4 = "," + count4 + "," + "," + "," + "," + 
    " Average Localization Error =" + (ersum4 / count4) + "\n";
    }

    if (checkBox3)
    {

if (sensor.apkts.Count == 3 && t2 == 3)     //three sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;

    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10/*sweepangle*/)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (gridscore == 3)
    {
MG++; Xest += xg; Yest += yg;
//g.DrawString(gridscore.ToString(), font, brush, xg, yg);
    }

    xg += 10;
}

yg += 10;
    }
    int erx, ery;    //error (x,y)
    if (MG != 0)
    {
//int time1 = System.DateTime.Now.Millisecond;
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count3++;
ersum3 += (int)Math.Sqrt(erx * erx + ery * ery);
op3 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + "," + 
    sensor.x + "," + sensor.y + "," + erx + "," + ery + "," + 
    (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count3 != 0) opc3 = "," + count3 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum3 / count3) + "\n";
    }

    if (checkBox2)
    {


if (sensor.apkts.Count == 2 && t2 == 2)     //two sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;

    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(
        hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
   hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] &&  
        theta1 <= (hsa[0] + 10/*sweepangle*/)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && 
        theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && 
        theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && 
       theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && 
        theta4 <= (hsa[1] + 10)) gridscore++;
}
    }

    if (gridscore == 2)
    {
MG++; Xest += xg; Yest += yg;
//g.DrawString(gridscore.ToString(), font, brush, xg, yg);
    }

    xg += 10;
}

yg += 10;
    }
    int erx, ery;    //error (x,y)
    if (MG != 0)
    {

erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count2++;
ersum2 += (int)Math.Sqrt(erx * erx + ery * ery);
op2 += "," + d + "," + (Xest / MG) + "," + 
    (Yest / MG) + "," + sensor.x + "," + sensor.y + 
    "," + erx + "," + ery + "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);

    }
}
if (count2 != 0) opc2 = "," + count2 + "," + "," + 
    "," + "," + " Average Localization Error =" + (ersum2 / count2) + "\n";
    }
}
d++;
    }
    
    end = System.DateTime.Now.Ticks;
    executiontime = end - start;
    time = " " + (double)(executiontime / 10000);
    
    using (FileStream fs = new FileStream("HiRLoc.xls", FileMode.Create))
    {
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
    w.WriteLine("LHs" + "," + "Node" + "," + 
       "X Estimated" + "," + "Y Estimated" + "," + 
       "X Actual" + "," + "Y Actual" + "," + "Ex" + "," + 
       "Ey" + "," + "Error");
    w.WriteLine("Two" + op2 + "No. of Sensors =" + "," + 
        opc2 + "\nThree" + op3 + "No. of Sensors =" + 
        "," + opc3 + "\nFour" + op4 + "No. of Sensors =" + 
        "," + opc4 + "\nFive" + op5 + "No. of Sensors =" + 
        "," + opc5 + "\nSix" + op6 + "No. of Sensors =" + 
        "," + opc6 + "\nSeven" + op7 + "No. of Sensors =" + 
        "," + opc7 + "\nEight" + op8 + "No. of Sensors =" + 
        "," + opc8 + "\n Execution Time = " + time + " ms");
}
    }
}
 
    if (checkBox8)
    {

  if (sensor.apkts.Count == 8 && t2 == 8)     //eight sectors "locators"
{
    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
float lp1Radius = (float)Math.Sqrt(Math.Pow(
    hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
float lp2Radius = (float)Math.Sqrt(Math.Pow(
    hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
float lp3Radius = (float)Math.Sqrt(Math.Pow(
    hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
float lp4Radius = (float)Math.Sqrt(Math.Pow(
    hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
float lp5Radius = (float)Math.Sqrt(Math.Pow(
    hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
float lp6Radius = (float)Math.Sqrt(Math.Pow(
    hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));
float lp7Radius = (float)Math.Sqrt(Math.Pow(
    hax[6] - xg, 2) + Math.Pow(hay[6] - yg, 2));
float lp8Radius = (float)Math.Sqrt(Math.Pow(
    hax[7] - xg, 2) + Math.Pow(hay[7] - yg, 2));
    if (lp1Radius <= locatorRadius)
   {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
  if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
  else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
 if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
  else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
  if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
   else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
  if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

     if (lp2Radius <= locatorRadius)
    {
  float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
  float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
  float theta2 = theta + 90;
  float theta3 = theta1 + 180;
  float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
    if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
    if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
    if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 10)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
    if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 10)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
    if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 10)) gridscore++;
}
    }
    if (lp7Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(
    Math.Abs(hax[6] - xg) / lp7Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[6] - yg) / lp7Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[6] && yg >= hay[6])  //Region 1
{
    if (theta1 >= hsa[6] && theta1 <= (hsa[6] + 10)) gridscore++;
}
else if (xg <= hax[6] && yg >= hay[6])  //Region 2
{
    if (theta2 >= hsa[6] && theta2 <= (hsa[6] + 10)) gridscore++;

}
else if (xg <= hax[6] && yg <= hay[6])  //Region 3
{
    if (theta3 >= hsa[6] && theta3 <= (hsa[6] + 10)) gridscore++;

}
else if (xg >= hax[6] && yg <= hay[6])  //Region 4
{
    if (theta4 >= hsa[6] && theta4 <= (hsa[6] + 10)) gridscore++;
}
    }
    if (lp8Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[7] - xg) / lp8Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[7] - yg) / lp8Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[7] && yg >= hay[7])  //Region 1
{
    if (theta1 >= hsa[7] && theta1 <= (hsa[7] + 10)) gridscore++;
}
else if (xg <= hax[7] && yg >= hay[7])  //Region 2
{
    if (theta2 >= hsa[7] && theta2 <= (hsa[7] + 10)) gridscore++;

}
else if (xg <= hax[7] && yg <= hay[7])  //Region 3
{
    if (theta3 >= hsa[7] && theta3 <= (hsa[7] + 10)) gridscore++;

}
else if (xg >= hax[7] && yg <= hay[7])  //Region 4
{
    if (theta4 >= hsa[7] && theta4 <= (hsa[7] + 10)) gridscore++;
}
    }
    if (gridscore == 8)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count8++;
ersum8 += (int)Math.Sqrt(erx * erx + ery * ery);
op8 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + "," + 
	sensor.x + "," + sensor.y + "," + erx + "," + ery + "," + 
	(int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
 }
 if (count8 != 0) opc8 = "," + count8 + "," + "," + "," + "," + 
	" Average Localization Error =" + (ersum8 / count8) + "\n";
    }

    if (checkBox7)
    {
if (sensor.apkts.Count == 7 && t2 == 7)     //seven sectors
{
    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(
        Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(
        Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(
        Math.Pow(hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(
        Math.Pow(hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(
        Math.Pow(hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
    float lp6Radius = (float)Math.Sqrt(
        Math.Pow(hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));
    float lp7Radius = (float)Math.Sqrt(
        Math.Pow(hax[6] - xg, 2) + Math.Pow(hay[6] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
    if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
    if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
    if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 10)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
    if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 10)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
    if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 10)) gridscore++;
}
    }
    if (lp7Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[6] - xg) / lp7Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[6] - yg) / lp7Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[6] && yg >= hay[6])  //Region 1
{
    if (theta1 >= hsa[6] && theta1 <= (hsa[6] + 10)) gridscore++;
}
else if (xg <= hax[6] && yg >= hay[6])  //Region 2
{
    if (theta2 >= hsa[6] && theta2 <= (hsa[6] + 10)) gridscore++;

}
else if (xg <= hax[6] && yg <= hay[6])  //Region 3
{
    if (theta3 >= hsa[6] && theta3 <= (hsa[6] + 10)) gridscore++;

}
else if (xg >= hax[6] && yg <= hay[6])  //Region 4
{
    if (theta4 >= hsa[6] && theta4 <= (hsa[6] + 10)) gridscore++;
}
    }
    if (gridscore == 7)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count7++;
ersum7 += (int)Math.Sqrt(erx * erx + ery * ery);
op7 += "," + d + "," + (Xest / MG) + "," + 
    (Yest / MG) + "," + sensor.x + "," + sensor.y + "," +
    erx + "," + ery + "," + (int)Math.Sqrt(erx * erx + 
    ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count7 != 0) opc7 = "," + count7 + "," + "," + 
    "," + "," + " Average Localization Error =" + 
    (ersum7 / count7) + "\n";
    }

    if (checkBox6)
    {
if (sensor.apkts.Count == 6 && t2 == 6)     //six sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(
        hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(
        hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(
        hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(Math.Pow(
        hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));
    float lp6Radius = (float)Math.Sqrt(Math.Pow(
        hax[5] - xg, 2) + Math.Pow(hay[5] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 15)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 15)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 15)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 15)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 15)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 15)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 15)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 15)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 15)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 15)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 15)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 15)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 15)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 15)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 15)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 15)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 15)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 15)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 15)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 15)) gridscore++;
}
    }
    if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
    if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 15)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
    if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 15)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
    if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 15)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
    if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 15)) gridscore++;
}
    }
    if (gridscore == 6)
    {
MG++; Xest += xg; Yest += yg;
//g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count6++;
ersum6 += (int)Math.Sqrt(erx * erx + ery * ery);
op6 += "," + d + "," + (Xest / MG) + "," + 
    (Yest / MG) + "," + sensor.x + "," + sensor.y + 
    "," + erx + "," + ery + "," + (int)Math.Sqrt(erx * erx + 
    ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count6 != 0) opc6 = "," + count6 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum6 / count6) + "\n";
    }
    if (checkBox5)
    {
if (sensor.apkts.Count == 5 && t2 == 5)     //five sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(
        hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(
        hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(
        hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));
    float lp5Radius = (float)Math.Sqrt(Math.Pow(
        hax[4] - xg, 2) + Math.Pow(hay[4] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
    if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[4] && yg >= hay[4])  //Region 1
{
    if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
    if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
    if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
    if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
    if (gridscore == 5)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, Brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count5++;
ersum5 += (int)Math.Sqrt(erx * erx + ery * ery);
op5 += "," + d + "," + (Xest / MG) + "," + 
    (Yest / MG) + "," + sensor.x + "," + sensor.y + 
    "," + erx + "," + ery + "," + (int)Math.Sqrt(
    erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count5 != 0) opc5 = "," + count5 + "," + "," + 
    "," + "," + " Average Localization Error =" + (ersum5 / count5) + "\n";
    }

    if (checkBox4)
    {

if (sensor.apkts.Count == 4 && t2 == 4)     //four sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;
    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(
        hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(
        hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(
        hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));
    float lp4Radius = (float)Math.Sqrt(Math.Pow(
        hax[3] - xg, 2) + Math.Pow(hay[3] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && 
        theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && 
    yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
    if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
    if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
    if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
    if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }

    if (gridscore == 4)
    {
MG++; Xest += xg; Yest += yg;
// g.DrawString(gridscore.ToString(), font, brush, xg, yg);
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count4++;
ersum4 += (int)Math.Sqrt(erx * erx + ery * ery);
op4 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
    "," + sensor.x + "," + sensor.y + "," + erx + "," + 
    ery + "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count4 != 0) opc4 = "," + count4 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum4 / count4) + "\n";
    }

    if (checkBox3)
    {

if (sensor.apkts.Count == 3 && t2 == 3)     //three sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;

    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));
    float lp3Radius = (float)Math.Sqrt(Math.Pow(hax[2] - xg, 2) + Math.Pow(hay[2] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10/*sweepangle*/)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
    if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
}
else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
    if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
    if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
    if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
    if (gridscore == 3)
    {
MG++; Xest += xg; Yest += yg;
//g.DrawString(gridscore.ToString(), font, brush, xg, yg);
    }

    xg += 10;
}

yg += 10;
    }
    int erx, ery;    //error (x,y)
    if (MG != 0)
    {
//int time1 = System.DateTime.Now.Millisecond;
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count3++;
ersum3 += (int)Math.Sqrt(erx * erx + ery * ery);
op3 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
    "," + sensor.x + "," + sensor.y + "," + erx + "," + ery + 
    "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);
    }
}
if (count3 != 0) opc3 = "," + count3 + "," + "," + "," + "," + 
    " Average Localization Error =" + (ersum3 / count3) + "\n";
    }

    if (checkBox2)
    {


if (sensor.apkts.Count == 2 && t2 == 2)     //two sectors
{

    int xg, yg = 20, MG = 0;
    Xest = 0; Yest = 0;

    for (int c = 1; c < 51; c++)      //columns
    {
xg = 20;
for (int r = 1; r < 51; r++)       //rows
{
    int gridscore = 0;
    float lp1Radius = (float)Math.Sqrt(Math.Pow(hax[0] - xg, 2) + Math.Pow(hay[0] - yg, 2));
    float lp2Radius = (float)Math.Sqrt(Math.Pow(hax[1] - xg, 2) + Math.Pow(hay[1] - yg, 2));

    if (lp1Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[0] - xg) / lp1Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[0] - yg) / lp1Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[0] && yg >= hay[0])  //Region 1
{
    if (theta1 >= hsa[0] && theta1 <= (hsa[0] + 10/*sweepangle*/)) gridscore++;
}
else if (xg <= hax[0] && yg >= hay[0])  //Region 2
{
    if (theta2 >= hsa[0] && theta2 <= (hsa[0] + 10)) gridscore++;

}
else if (xg <= hax[0] && yg <= hay[0])  //Region 3
{
    if (theta3 >= hsa[0] && theta3 <= (hsa[0] + 10)) gridscore++;

}
else if (xg >= hax[0] && yg <= hay[0])  //Region 4
{
    if (theta4 >= hsa[0] && theta4 <= (hsa[0] + 10)) gridscore++;
}
    }

    if (lp2Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[1] - xg) / lp2Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[1] - yg) / lp2Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
    if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
    if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
    if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
    if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }

    if (gridscore == 2)
    {
MG++; Xest += xg; Yest += yg;
//g.DrawString(gridscore.ToString(), font, brush, xg, yg);
    }

    xg += 10;
}

yg += 10;
    }
    int erx, ery;    //error (x,y)
    if (MG != 0)
    {

erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count2++;
ersum2 += (int)Math.Sqrt(erx * erx + ery * ery);
op2 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
    "," + sensor.x + "," + sensor.y + "," + erx + "," + ery + 
    "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; //Excel
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);

    }
}
if (count2 != 0) opc2 = "," + count2 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum2 / count2) + "\n";
    }
}
d++;
    }
    
    end = System.DateTime.Now.Ticks;
    executiontime = end - start;
    time = " " + (double)(executiontime / 10000);
    
    using (FileStream fs = new FileStream("HiRLoc.xls", FileMode.Create))
    {
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
    w.WriteLine("LHs" + "," + "Node" + "," + "X Estimated" + "," + 
	"Y Estimated" + "," + "X Actual" + "," + "Y Actual" + "," + 
	"Ex" + "," + "Ey" + "," + "Error");
    w.WriteLine("Two" + op2 + "No. of Sensors =" + "," + 
    opc2 + "\nThree" + op3 + "No. of Sensors =" + "," + opc3 + 
    "\nFour" + op4 + "No. of Sensors =" + "," + opc4 + "\nFive" + 
    op5 + "No. of Sensors =" + "," + opc5 + "\nSix" + op6 + 
    "No. of Sensors =" + "," + opc6 + "\nSeven" + op7 + 
    "No. of Sensors =" + "," + opc7 + "\nEight" + op8 + "No. of Sensors =" +
    "," + opc8 + "\n Execution Time = " + time + " ms");
}
    }
}
 
   if (xg >= hax[1] && yg >= hay[1])  //Region 1
{
 if (theta1 >= hsa[1] && theta1 <= (hsa[1] + 10)) gridscore++;
}
   else if (xg <= hax[1] && yg >= hay[1])  //Region 2
{
 if (theta2 >= hsa[1] && theta2 <= (hsa[1] + 10)) gridscore++;

}
    else if (xg <= hax[1] && yg <= hay[1])  //Region 3
{
 if (theta3 >= hsa[1] && theta3 <= (hsa[1] + 10)) gridscore++;

}
     else if (xg >= hax[1] && yg <= hay[1])  //Region 4
{
 if (theta4 >= hsa[1] && theta4 <= (hsa[1] + 10)) gridscore++;
}
    }
    if (lp3Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(
    Math.Abs(hax[2] - xg) / lp3Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[2] - yg) / lp3Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
  if (xg >= hax[2] && yg >= hay[2])  //Region 1
{
       if (theta1 >= hsa[2] && theta1 <= (hsa[2] + 10)) gridscore++;
       }
   else if (xg <= hax[2] && yg >= hay[2])  //Region 2
{
       if (theta2 >= hsa[2] && theta2 <= (hsa[2] + 10)) gridscore++;

}
   else if (xg <= hax[2] && yg <= hay[2])  //Region 3
{
       if (theta3 >= hsa[2] && theta3 <= (hsa[2] + 10)) gridscore++;

}
   else if (xg >= hax[2] && yg <= hay[2])  //Region 4
{
       if (theta4 >= hsa[2] && theta4 <= (hsa[2] + 10)) gridscore++;
}
    }
 if (lp4Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(
    hax[3] - xg) / lp4Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(
    hay[3] - yg) / lp4Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
   
 if (xg >= hax[3] && yg >= hay[3])  //Region 1
{
      if (theta1 >= hsa[3] && theta1 <= (hsa[3] + 10)) gridscore++;
}
  else if (xg <= hax[3] && yg >= hay[3])  //Region 2
{
       if (theta2 >= hsa[3] && theta2 <= (hsa[3] + 10)) gridscore++;

}
  else if (xg <= hax[3] && yg <= hay[3])  //Region 3
{
       if (theta3 >= hsa[3] && theta3 <= (hsa[3] + 10)) gridscore++;

}
  else if (xg >= hax[3] && yg <= hay[3])  //Region 4
{
       if (theta4 >= hsa[3] && theta4 <= (hsa[3] + 10)) gridscore++;
}
    }
      if (lp5Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[4] - xg) / lp5Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[4] - yg) / lp5Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
    if (xg >= hax[4] && yg >= hay[4])  //Region 1
       {
  if (theta1 >= hsa[4] && theta1 <= (hsa[4] + 10)) gridscore++;
}
    else if (xg <= hax[4] && yg >= hay[4])  //Region 2
{
  if (theta2 >= hsa[4] && theta2 <= (hsa[4] + 10)) gridscore++;

}
    else if (xg <= hax[4] && yg <= hay[4])  //Region 3
{
  if (theta3 >= hsa[4] && theta3 <= (hsa[4] + 10)) gridscore++;

}
    else if (xg >= hax[4] && yg <= hay[4])  //Region 4
{
  if (theta4 >= hsa[4] && theta4 <= (hsa[4] + 10)) gridscore++;
}
    }
 if (lp6Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[5] - xg) / lp6Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[5] - yg) / lp6Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;

if (xg >= hax[5] && yg >= hay[5])  //Region 1
{
     if (theta1 >= hsa[5] && theta1 <= (hsa[5] + 10)) gridscore++;
}
else if (xg <= hax[5] && yg >= hay[5])  //Region 2
{
     if (theta2 >= hsa[5] && theta2 <= (hsa[5] + 10)) gridscore++;

}
else if (xg <= hax[5] && yg <= hay[5])  //Region 3
{
     if (theta3 >= hsa[5] && theta3 <= (hsa[5] + 10)) gridscore++;

}
else if (xg >= hax[5] && yg <= hay[5])  //Region 4
{
      if (theta4 >= hsa[5] && theta4 <= (hsa[5] + 10)) gridscore++;
}
    }
    if (lp7Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[6] - xg) / lp7Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[6] - yg) / lp7Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
if (xg >= hax[6] && yg >= hay[6])  //Region 1
{
       if (theta1 >= hsa[6] && theta1 <= (hsa[6] + 10)) gridscore++;
}
 else if (xg <= hax[6] && yg >= hay[6])  //Region 2
{
       if (theta2 >= hsa[6] && theta2 <= (hsa[6] + 10)) gridscore++;

}
else if (xg <= hax[6] && yg <= hay[6])  //Region 3
{
      if (theta3 >= hsa[6] && theta3 <= (hsa[6] + 10)) gridscore++;

}
       else if (xg >= hax[6] && yg <= hay[6])  //Region 4
{
     if (theta4 >= hsa[6] && theta4 <= (hsa[6] + 10)) gridscore++;
}
    }
  if (lp8Radius <= locatorRadius)
    {
float theta1 = (float)(Math.Acos(Math.Abs(hax[7] - xg) / lp8Radius) * (180 * 7 / 22));
float theta = (float)(Math.Acos(Math.Abs(hay[7] - yg) / lp8Radius) * (180 * 7 / 22));
float theta2 = theta + 90;
float theta3 = theta1 + 180;
float theta4 = 270 + theta;
   
 
     if (xg >= hax[7] && yg >= hay[7])  //Region 1
{
    if (theta1 >= hsa[7] && theta1 <= (hsa[7] + 10)) gridscore++;
}
     else if (xg <= hax[7] && yg >= hay[7])  //Region 2
{
    if (theta2 >= hsa[7] && theta2 <= (hsa[7] + 10)) gridscore++;

}
     else if (xg <= hax[7] && yg <= hay[7])  //Region 3
{
    if (theta3 >= hsa[7] && theta3 <= (hsa[7] + 10)) gridscore++;

}
      else if (xg >= hax[7] && yg <= hay[7])  //Region 4
{
    if (theta4 >= hsa[7] && theta4 <= (hsa[7] + 10)) gridscore++;
}
    }
  if (gridscore == 8)
    {
     MG++; Xest += xg; Yest += yg;
    }
    xg += 10;
}
yg += 10;
    }
    if (MG != 0)
    {
int erx, ery;    //error (x,y)
erx = Math.Abs(((Xest / MG) - sensor.x));
ery = Math.Abs(((Yest / MG) - sensor.y));
count8++;
ersum8 += (int)Math.Sqrt(erx * erx + ery * ery);
//write output to Excel file
op8 += "," + d + "," + (Xest / MG) + "," + (Yest / MG) + 
    "," + sensor.x + "," + sensor.y + "," + erx + "," + 
    ery + "," + (int)Math.Sqrt(erx * erx + ery * ery) + "\n"; 
//paint estimated position
g.FillEllipse(sbrush, (Xest / MG) - 5, (Yest / MG) - 5, 10, 10);    }
 }
if (count8 != 0) opc8 = "," + count8 + "," + "," + "," + 
    "," + " Average Localization Error =" + (ersum8 / count8) + "\n";
    
}

  }
d++;
    }

The above code is for eight locators' intersection, the rest of the code is the same but with seven, six, five, four, three, and two locators' intersection. The estimated position is calculated and painted, then it will be written to an Excel file as shown below:

C#
end = System.DateTime.Now.Ticks;
executiontime = end - start;
time = " " + (double)(executiontime / 10000);

using (StreamWriter w = new StreamWriter (textbox,false,Encoding.UTF8))
    {
 w.WriteLine("LHs" + "," + "Node" + "," + "X Estimated" + 
     "," + "Y Estimated" + "," + "X Actual" + "," + 
     "Y Actual" + "," + "Ex" + "," + "Ey" + "," + "Error");
 w.WriteLine("Two" + op2 + "No. of Sensors =" + 
     "," + opc2 + "\nThree" + op3 + "No. of Sensors =" + 
     "," + opc3 + "\nFour" + op4 + "No. of Sensors =" + 
     "," + opc4 + "\nFive" + op5 + "No. of Sensors =" + 
     "," + opc5 + "\nSix" + op6 + "No. of Sensors =" + 
     "," + opc6 + "\nSeven" + op7 + "No. of Sensors =" + 
     "," + opc7 + "\nEight" + op8 + "No. of Sensors =" + 
     "," + opc8 + "\n Execution Time = " + time + " ms");
    }
        }

How to Simulate a Localization Algorithm

For example, HiRLoc.

  1. Choose deployment strategy of locators (e.g. grid).
  2. Choose locator antenna type.
  3. Click on deploy WSN button.
  4. Click on start simulation.
  5. Check Estimate position.
  6. Choose localization algorithm (check HiRLoc).
  7. Write the name and location of Excel file which contains the actual and estimated position, number of localized sensor nodes and running time of that algorithm.
  8. Check number of crossed sectors (2,3,4,5,6,…).
  9. Estimated position of sensor nodes will be painted on the area with BlueViolet color.
  10. Click on Stop simulation button.
Image 3
Figure 5: Steps of HiRloc Localization

Output File

Image 4

Conclusion

We have presented a framework to develop WSN localization algorithms. The framework allows developers to design the required localization algorithm by writing the code into a class library file and referencing it to the framework. Our framework has been validated using different types of localization algorithms: range-free, range-based and hybrid localization for WSNs. Our framework results had been compared to the selected localization algorithms results and it was found that they were compatible with the original results of these algorithms in their papers. Our framework can be extended to implement other localization algorithms and can be used by future researchers.

History

This is a Wireless Sensor Network Localization Simulator v1.1 written by Abdelhady Mohammad Naguib in May 2011. Thank you for using the WSN Localization Simulator.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
Assistant professor at Systems & Computers Engineering Department - Faculty of Engineering - Al-Azhar University - Cairo - Egypt
Assistant professor at Computer Science Department - Jouf University - kingdom of Saudi Arabia

Comments and Discussions

 
QuestionHADLA algorithms Pin
abbey liew19-May-18 7:52
abbey liew19-May-18 7:52 
QuestionFacing problem to run the simulator Pin
Member 1148839321-May-15 19:55
Member 1148839321-May-15 19:55 
QuestionExplain Parameters Used in Simulator Pin
Member 1062470028-Feb-14 0:58
Member 1062470028-Feb-14 0:58 
QuestionFacing problem to run the project Pin
Member 1062470027-Feb-14 8:36
Member 1062470027-Feb-14 8:36 
QuestionHow to prepare Excel Sheet For output Pin
Member 1062470027-Feb-14 8:31
Member 1062470027-Feb-14 8:31 
QuestionGIANT code snippet Pin
Dave Kreskowiak10-Feb-14 7:30
mveDave Kreskowiak10-Feb-14 7:30 
QuestionWireless Sensor Network Localization Pin
nass ahmad13-Jan-14 6:52
nass ahmad13-Jan-14 6:52 
GeneralRe: Wireless Sensor Network Localization Pin
DaveAuld16-Jan-14 5:22
professionalDaveAuld16-Jan-14 5:22 
Questionerror in compiling Mcl Code Pin
Member 1047938520-Dec-13 9:32
Member 1047938520-Dec-13 9:32 
Questionmoving locator with static sensor Pin
Basant El Samadony12-Dec-13 5:57
Basant El Samadony12-Dec-13 5:57 
QuestionDear Author Kindly provide code for DLL Pin
Rajan ch17-Mar-13 6:44
Rajan ch17-Mar-13 6:44 
QuestionLocalization Algorithms Source code Pin
Basant El Samadony16-Mar-13 2:02
Basant El Samadony16-Mar-13 2:02 
AnswerRe: Localization Algorithms Source code Pin
Abdelhady Naguib16-Mar-13 8:37
Abdelhady Naguib16-Mar-13 8:37 
GeneralRe: Localization Algorithms Source code Pin
Basant El Samadony18-Mar-13 11:38
Basant El Samadony18-Mar-13 11:38 
GeneralRe: Localization Algorithms Source code Pin
Member 118259129-Jul-15 5:49
Member 118259129-Jul-15 5:49 
QuestionDIL algorithm Pin
klitimaster29-Sep-12 8:49
klitimaster29-Sep-12 8:49 
Questiontanx Pin
Iman Jafarnezhad6-Sep-12 7:52
Iman Jafarnezhad6-Sep-12 7:52 
GeneralMy vote of 1 Pin
dvptUml7-Jul-12 23:01
dvptUml7-Jul-12 23:01 
Questionrequest Pin
hamidnikmehr25-May-12 7:06
hamidnikmehr25-May-12 7:06 
QuestionDLL file does not exist Pin
hamidnikmehr25-May-12 4:35
hamidnikmehr25-May-12 4:35 
Questionsearch Pin
WeCanTry19-Apr-12 13:09
WeCanTry19-Apr-12 13:09 
GeneralDLL file does not found Pin
tnbalaji199027-Feb-12 3:15
tnbalaji199027-Feb-12 3:15 
QuestionThe algorithms paper Pin
alirzn123-Jan-12 9:39
alirzn123-Jan-12 9:39 
Questionhelp with the ddl files Pin
Omar Ali Zargelin10-Nov-11 20:34
Omar Ali Zargelin10-Nov-11 20:34 
AnswerRe: help with the ddl files Pin
Abdelhady Naguib13-Nov-11 22:14
Abdelhady Naguib13-Nov-11 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.