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

📄 object.h

📁 基于SDL的图形化贪心算法演示,支持节点的拖放
💻 H
字号:
#ifndef __OBJECT_H___
#define __OBJECT_H___

#include "SDLGUI.h"
#include <string.h>
#include <deque>
using std::deque;

#include <math.h>

extern SDLGUI* g_gui;

extern SDL_Surface* g_blackcircle;
extern SDL_Surface* g_redcircle;

class MapObject
{
public:
	
	MapObject( int x, int y )
		:m_x(x), m_y(y)
	{
	}

	void SetPos( int x, int y )
	{
		m_x = x;
		m_y = y;
	}
	
	void GetPos( int*out_x, int*out_y )
	{
		*out_x = m_x;
		*out_y = m_y;
	}

	bool PointIn( int x, int y )
	{
		if( x > m_x && y > m_y && x < m_x + 48 && y < m_y + 48 )
			return true;

		return false;
	}


protected:
	int m_x;
	int m_y;

};

class MapLine;

class MapNode : public MapObject
{
public:	

	MapNode( char* text, int x, int y )
		:MapObject( x, y )
	{
		m_bFocus = false;		
		strcpy( m_Text, text );
	}
		
	void Draw();


	void AddLine( MapLine* line )
	{
		deque_line.push_back( line );
	}

	void SetText( char* t )
	{
		strcpy( m_Text, t );
	}


	void Focus( void )
	{
		m_bFocus = true;
	}

	void UnFocus( void )
	{
		m_bFocus = false;
	}


	int GetLengthToNode( int index );

private:
	deque<MapLine*>		deque_line;
	char				m_Text[5];	
	bool				m_bFocus;
	
	static deque<MapNode*>*	p_NodeDeque;	
	
	
};

class MapLine
{
public:

	MapLine( int length, MapNode* from, MapNode* to )
	{
		m_Length = length;
		m_NodeFrom = from;
		m_NodeTo = to;
		strcpy( m_sLength, "10" );

	}

	void Draw()
	{
		int x_from, x_to;
		int y_from, y_to;
		
		int x_plus;
		int y_plus;

		m_NodeFrom->GetPos( &x_from, &y_from );
		m_NodeTo->GetPos( &x_to, &y_to );
	
		float rate_x =( x_to - x_from ) / sqrt( (double)( y_to - y_from )*( y_to - y_from ) + ( x_to - x_from )*( x_to - x_from ) );
		float rate_y = ( y_to - y_from  ) / sqrt( (double)( y_to - y_from )*( y_to - y_from ) + ( x_to - x_from )*( x_to - x_from ) );
		y_plus = 34*rate_y;
		x_plus = 34*rate_x;

		if( m_NodeFrom && m_NodeTo )
		{
			
			g_gui->ArrowLine( x_from + 24 + x_plus  , y_from + 24 + y_plus, x_to + 24 - x_plus , y_to + 24 - y_plus  ,0,0,false, true, BLACK );
		}
		
		if( m_pTextBox )
		{
			char *length = m_pTextBox->GetContent();
			m_Length = atol( length );	
		}
	}



	void FocusFrom( MapNode* from )
	{
		m_NodeFrom = from;
	}

	void FocusTo( MapNode* to )
	{
		m_NodeTo = to;

		int x_f, y_f;
		int x_t, y_t;
		int width = 40;
		int height = 20;
		m_NodeFrom->GetPos( &x_f, &y_f );
		m_NodeTo->GetPos( &x_t, &y_t );
		m_pTextBox = (SDLTextBox*)g_gui->GetItem( g_gui->AddTextBox( (x_t+x_f+ width/2)/2, (y_t + y_f + height/2) /2 , width, height, m_sLength, 2, 0, BLACK, WHITE, BLACK,true, NULL  ) );
	}
	
	MapNode* GetFromNode()
	{
		return m_NodeFrom;
	}

	MapNode* GetToNode()
	{	
		return m_NodeTo;
	}

	void ReleaseLine( void )
	{
		m_NodeFrom = NULL;
		m_NodeTo = NULL;
	}

	int Legnth( void )
	{
		return m_Length;
	}

private:
	
	char		m_sLength[5];
	int			m_Length;
	MapNode*	m_NodeFrom;
	MapNode*	m_NodeTo;
	SDLTextBox* m_pTextBox;
		
};


#endif

⌨️ 快捷键说明

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