/*!
@file utility.hpp
*/

//一回だけ読み込むように仕向けるための印
#ifndef DKUTIL_GRAPHICS_2D_POLYGON_HPP
#define DKUTIL_GRAPHICS_2D_POLYGON_HPP

//#include <DxLibMake/DxDraw.h>
#include <dkutil/graphics/utility.hpp>



namespace dxex{
#if 0
template<class DRAW_POLICY_T,class POINT_T=Point<int> ,class COLOR_T=int>
class DrawPolygon2D{
public:
	typedef POINT_T point_type;


	struct MinMaxData{
		int min;
		int max;
		size_t min_offset;
	};
	struct State{
		///増加X
		double inc_x;
		///カウント
		double x;
	};
private:
	static double getXIncrease(int x,int x2,int y,int y2){
		double t = (y-y2);
	  return (x-x2) / t;
	}
	///Y座標値で最大・最小のものを求める
	static bool CalcMinMaxY(point_type *lpVer,size_t size,MinMaxData *result){
		int ty,min,max;

		size_t toffset = 0;
		ty = max = min = lpVer[0].y;

		for(size_t i=0;i<size;i++){
	    ty = lpVer[i].y;
	    if(min>ty){
				toffset = i;
				min=ty;
	    }
	    if(max<ty){
				max=ty;
			}
	  }
	  min=(int)floor(min+0.5);
	  max=(int)floor(max+0.5);
	  if(min==max){
			return false;
		}
		//set result
		result->min = min;
		result->max = max;
		result->min_offset = toffset;
		
		return true;
	}
	static void CalcLeftX(point_type *lpVertex,size_t count,const MinMaxData &mmd,State *state){


		size_t i;
		i=mmd.min_offset;
		
			//左側にある頂点とX方向増加値の計算
		do{
				i++;
				if(i>count-1) i=0;
		} while(lpVertex[i].y < mmd.min+0.5);
		
		size_t m;
		if(i < 1){
			m = count - 1;
		}
		/*size_t m=i-1;
		
		if(m<0){
			m=count-1;
		}*/
		
		state->inc_x = getXIncrease(lpVertex[i].x,lpVertex[m].x,lpVertex[i].y,lpVertex[m].y);


		//dxA=(lpVertex[i].x-lpVertex[m].x) / (lpVertex[i].y-lpVertex[m].y);
		state->x =lpVertex[m].x;


	}
	static void CalcRightX(point_type *lpVertex,size_t count,const MinMaxData &mmd,State *state){
		
		size_t i=mmd.min_offset;
		//右側にある頂点とX方向増加値の計算
		do{
				i--;
				if(i<0) i=count-1;
		} while(lpVertex[i].y<sy+0.5);
		m=i+1;
		
		if(m>count-1){ 
			m=0; 
		}

		//dd=lpVertex[i].y-lpVertex[m].y;
		//dxB=(lpVertex[i].x-lpVertex[m].x)/dd;
		state->inc_x = getXIncrease(
			lpVertex[i].x,
			lpVertex[m].x,
			lpVertex[i].y,
			lpVertex[m].y);

		//dxB=(lpVertex[i].x-lpVertex[m].x) / (lpVertex[i].y-lpVertex[m].y);
		state->x = lpVertex[m].x;


	}

	
	int draw(point_type *lpVertex,size_t count,Color<int> col){
		
		switch(count){
		case 0:
			return false;
		case 1:
			
		MinMaxData data;
		if(false==CalcMinMaxY(lpVertex,count,&data)){
			return false;
		}
		State leftx,rightx;
		
		CalcLeftX(lpVertex,count,data,&leftx);
		CalcRightX(lpVertex,count,data,&rightx);
		
	}
	virtual draw(
};
#endif

}//end of dxex namespace


#endif