void VoronoiDiagramGenerator::line(float x1, float y1, float x2, float y2, float s1x, float s1y, float s2x, float s2y )
{	
	pushGraphEdge(x1,y1,x2,y2, s1x, s1y, s2x, s2y);

}
void VoronoiDiagramGenerator::line(float x1, float y1, float x2, float y2)
{	
	pushGraphEdge(x1,y1,x2,y2);

}
void VoronoiDiagramGenerator::line(float x1, float y1, float x2, float y2, struct Site *s1, struct Site *s2)
{	
	pushGraphEdge(x1,y1,x2,y2,s1,s2);

}
void VoronoiDiagramGenerator::CloseEdges()
{
	RhinoApp().Print(L"\n close edges\n");
	float x1,y1,x2,y2;
	//Site *s1;
	//Site *s2;
	int arraysize = ((int)sqrt((double)numedges)+1);
	/*Point* bpln = (Point*)malloc(arraysize * sizeof(Point));
	Point* bpls = (Point*)malloc(arraysize * sizeof(Point));
	Point* bple = (Point*)malloc(arraysize * sizeof(Point));
	Point* bplw = (Point*)malloc(arraysize * sizeof(Point));*/

	
	//Point* pe, pr;
	int north=0;
	int south=0;
	int east=0;
	int west=0;
	int total = 0;
	int z,q; 
	//int elements;
	resetIterator();
	while(getNext(x1,y1,x2,y2))
	{
		if(sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))) < minDistanceBetweenSites)
		{
			//ignore it
		}
		else
		{
			if(x1 == borderMinX){west++;}
			if(x2 == borderMinX){west++;}
			if(x1 == borderMaxX){east++;}
			if(x2 == borderMaxX){east++;}
			if(y1 == borderMinY){south++;}
			if(y2 == borderMinY){south++;}
			if(y1 == borderMaxY){north++;}
			if(y2 == borderMaxY){north++;}
		}
	}

	Point* bpln = (Point*)malloc(north * sizeof(Point));
	Point* bpls = (Point*)malloc(south * sizeof(Point));
	Point* bple = (Point*)malloc(east * sizeof(Point));
	Point* bplw = (Point*)malloc(west * sizeof(Point));

	north=0;
	south=0;
	east=0;
	west=0;

	resetIterator();
	while(getNext(x1,y1,x2,y2))
	{
		if(sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))) < minDistanceBetweenSites)
		{
			//ignore it
		}
		else
		{
			if(x1 == borderMinX){bplw[west].x = x1; bplw[west].y = y1; west++;}
			if(x2 == borderMinX){bplw[west].x = x2; bplw[west].y = y2; west++;}
			if(x1 == borderMaxX){bple[east].x = x1; bple[east].y = y1; east++;}
			if(x2 == borderMaxX){bple[east].x = x2; bple[east].y = y2; east++;}
			if(y1 == borderMinY){bpls[south].x = x1; bpls[south].y = y1; south++;}
			if(y2 == borderMinY){bpls[south].x = x2; bpls[south].y = y2; south++;}
			if(y1 == borderMaxY){bpln[north].x = x1; bpln[north].y = y1; north++;}
			if(y2 == borderMaxY){bpln[north].x = x2; bpln[north].y = y2; north++;}
		}
	}
	total = (north+south+east+west+4);

	Point northwest, northeast, southeast, southwest;
	southwest.x = borderMinX; southwest.y = borderMinY;
	southeast.x = borderMaxX; southeast.y = borderMinY;
	northwest.x = borderMinX; northwest.y = borderMaxY;
	northeast.x = borderMaxX; northeast.y = borderMaxY;
	Point* bplt = (Point*)malloc(total * sizeof(Point));
	//RhinoApp().Print(L"\n filling pblt\n");
	RhinoApp().Print(L"numedges: %d, total: %d, north: %d, south: %d, east: %d, west: %d\n",numedges, total, north, south, east, west);
	q=0;

	bplt[q] = southwest; q++;

	qsort(bpls,south,sizeof(Point),EdgeCompare);
	for(z=0; z<south; z++){bplt[q] = bpls[z]; q++;}	
	//free(bpls);

	bplt[q] = southeast; q++;
	qsort(bple,east,sizeof(Point),EdgeCompare);
	for(z=0; z<east; z++) {bplt[q] = bple[z]; q++;}
	//free(bple);

	bplt[q] = northeast; q++;
	qsort(bpln,north,sizeof(Point),EdgeCompare);
	for(z=0; z<north; z++){bplt[q] = bpln[z]; q++;}
	//free(bpln);

	bplt[q] = northwest; q++;
	qsort(bplw,west,sizeof(Point),EdgeCompare);
	for(z=0; z<west; z++) {bplt[q] = bplw[z]; q++;}			
	//free(bplw);

	bplt[q] = southwest;
	RhinoApp().Print(L"q: %d\n",q);
	int e;
	for( e=1; e<=total; e++)
	{
			//bplt[e-1];
			//bplt[e];
			RhinoApp().Print(L"pushing edge: e = %d : %f,%f -> %f,%f \n",e,bplt[e-1].x,bplt[e-1].y,bplt[e].x, bplt[e].y);
			pushGraphEdge(bplt[e-1].x, bplt[e-1].y, bplt[e].x,bplt[e].y,NULL,NULL);
	}
	RhinoApp().Print(L"e: %d\n",e);
	//free(bplt);
}
void VoronoiDiagramGenerator::line(double x1, double y1, double x2, double y2)
{    
    pushGraphEdge(x1,y1,x2,y2);

}
void VoronoiDiagramGenerator::clip_line(struct Edge *e)
{
	struct Site *s1, *s2;
	float x1=0,x2=0,y1=0,y2=0, temp = 0;
	Site *v1= 0, *v2 = 0;
	bool needNewVertex1 = false,needNewVertex2 = false;

	x1 = e->reg[0]->coord.x;
	x2 = e->reg[1]->coord.x;
	y1 = e->reg[0]->coord.y;
	y2 = e->reg[1]->coord.y;

	//if the distance between the two points this line was created from is less than 
	//the square root of 2, then ignore it
	if(sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))) < minDistanceBetweenSites)
	{
		return;
	}
	pxmin = borderMinX;
	pxmax = borderMaxX;
	pymin = borderMinY;
	pymax = borderMaxY;

//	printf("\nEdge (%d), minX = %f, maxX = %f, minY = %f, maxY = %f",e->edgenbr,pxmin, pxmax, pymin,pymax);
	
	if(e -> a == 1.0 && e ->b >= 0.0)
	{	
		s1 = e -> ep[1];
		s2 = e -> ep[0];
	}
	else 
	{
		s1 = e -> ep[0];
		s2 = e -> ep[1];
	};

	v1 = s1; 
	v2 = s2;
	
	if(e -> a == 1.0)
	{
		y1 = pymin;
		if (s1!=(struct Site *)NULL && s1->coord.y > pymin)
		{
			y1 = s1->coord.y;
		}
		else
		{
			needNewVertex1 = true;
		}

		if(y1>pymax) 
		{
			y1 = pymax;
			needNewVertex1 = true;
		}

		x1 = e -> c - e -> b * y1;
		y2 = pymax;
		if (s2!=(struct Site *)NULL && s2->coord.y < pymax) 
		{
			y2 = s2->coord.y;
		}
		else
		{
			needNewVertex2 = true;
		}


		if(y2<pymin) 
		{
			y2 = pymin;
			needNewVertex2 = true;
		}

		x2 = (e->c) - (e->b) * y2;
		if (((x1> pxmax) & (x2>pxmax)) | ((x1<pxmin)&(x2<pxmin))) 
		{
			return;
		}
		if(x1> pxmax)
		{	x1 = pxmax; y1 = (e -> c - x1)/e -> b;needNewVertex1 = true;}
		if(x1<pxmin)
		{	x1 = pxmin; y1 = (e -> c - x1)/e -> b;needNewVertex1 = true;}
		if(x2>pxmax)
		{	x2 = pxmax; y2 = (e -> c - x2)/e -> b;needNewVertex2 = true;}
		if(x2<pxmin)
		{	x2 = pxmin; y2 = (e -> c - x2)/e -> b;needNewVertex2 = true;}
	}
	else
	{
		x1 = pxmin;
		if (s1!=(struct Site *)NULL && s1->coord.x > pxmin) 
		{
			x1 = s1->coord.x;
		}
		else
		{
			needNewVertex1 = true;
		}

		if(x1>pxmax) 
		{
			x1 = pxmax;
			needNewVertex1 = true;
		}
		y1 = e -> c - e -> a * x1;
		x2 = pxmax;
		if (s2!=(struct Site *)NULL && s2->coord.x < pxmax) 
		{
			x2 = s2->coord.x;
		}
		else
		{
			needNewVertex2 = true;
		}

		if(x2<pxmin) 
		{
			x2 = pxmin;
			needNewVertex2 = true;
		}
		y2 = e -> c - e -> a * x2;
		if (((y1> pymax) & (y2>pymax)) | ((y1<pymin)&(y2<pymin))) 
		{
			return;
		}
		if(y1> pymax)
		{	y1 = pymax; x1 = (e -> c - y1)/e -> a;needNewVertex1 = true;}
		if(y1<pymin)
		{	y1 = pymin; x1 = (e -> c - y1)/e -> a;needNewVertex1 = true;}
		if(y2>pymax)
		{	y2 = pymax; x2 = (e -> c - y2)/e -> a;needNewVertex2 = true;}
		if(y2<pymin)
		{	y2 = pymin; x2 = (e -> c - y2)/e -> a;needNewVertex2 = true;}
	}

	
	
	
	//printf("\nPushing line (%f,%f,%f,%f)",x1,y1,x2,y2);
//	
	if(!((x1 == x2 && x2== pxmin) || (x1 == x2 && x2 == pxmax) || 
		(y1 == y2 && y2 == pymin) || (y1 == y2 && y2 == pymax)))
	{
		pushGraphEdge(x1,y1,x2,y2);
		if(needNewVertex1)
		{
			//printf("\nCreate new vertex 1 
			v1 = (struct Site *) getfree(&sfl);
			v1 -> refcnt = 0;
			v1 -> coord.x = x1;
			v1 -> coord.y = y1;
			makevertex(v1);

		}

		if(needNewVertex2)
		{
			v2 = (struct Site *) getfree(&sfl);
			v2 -> refcnt = 0;
			v2 -> coord.x = x2;
			v2 -> coord.y = y2;
			makevertex(v2);
		}
		insertVertexLink(v1->sitenbr,v2->sitenbr);
	}

	
}