⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.cpp

📁 无线传感器网络leach协议无线传感器网络leach协议无线传感器网络leach协议
💻 CPP
字号:
#include "StdAfx.h"

#include <stdlib.h>
#include "Node.h"
#include <math.h>

Node::Node():IsCluster(false)
{
    DistanceList = NULL;
}
Node::Node(unsigned int Count, int mid):id(mid), px(0), py(0), IsCluster(false)
{
    NodeCount = Count;
    DistanceList = new NodeSort[NodeCount];
}

Node::~Node(void)
{
    delete [] DistanceList;
}

int Node::OnBecomeCluster(void)
{
    //成为簇头
    printf("%d ", id);
    return 0;
}

int Node::OnNoneCluster(void)
{
    // 寻找离自己最近的簇头
	int itsClusterID;
    for (unsigned int i = 0; i < NodeCount; i++)
    {
        if (this->DistanceList[i].DstNode->IsCurrentCluster)
        {
            //取消注释可以显示该节点的簇头
           // printf("%d --> %d\n", id, DistanceList[i].DstNode->id);
			itsClusterID = DistanceList[i].DstNode->id ;
            break;
        }
    }
    
    return itsClusterID;
}

//计算到其他节点的距离并排序

void Node::SetDistance(Node * DstList)
{
    if (DistanceList == NULL)
    {
        printf("error!");
        return;
    }
    for (unsigned int i = 0; i < NodeCount; i++)
    {
        this->DistanceList[i].ComputeDistance(this, &DstList[i]);
       
    }
    
    qsort(DistanceList, NodeCount, sizeof(NodeSort), (int (*)(const void *,const void *))NodeSort::Compare);


}

void Node::SetNode(unsigned int Count, int id)
{
    NodeCount = Count;
    if (DistanceList == NULL)
    {
       DistanceList = new NodeSort[NodeCount];
    }
    this->NodeCount = Count;
    this->id = id;

}

void Node::SetE(double e)
{
	ne = e;
}
double Node::GetE()
{
	return ne;
}
int Node::consumeE(Node &a ,Node &b)
{	
	double E_static = 40 / 1000000000 ;
	double E_amp = 80 / 1000000000000 ;
	int k = 50 ;
	double d = sqrt( pow(a.px - b.px, 2) + pow(a.py - b.py, 2));
	a.ne  =a.ne - ( k * E_static + k * E_amp * d *d );
	b.ne  =b.ne - k * E_static;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -