/* 
 * University of Leipzig: world.cc, v0.1b  21.9.97 22:58 
 * 
 * 
 * Copyright 1997 by University of Leipzig
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the U.L. not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The U.L. makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@aix520.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */

#include "copyright.h"

#include "world.h"
#include <stdio.h>

#include "graphix.h"

void t_world::Init(void)
{
  max.x = + 1/1000000000.0;    // are these real small numbers ? 
  max.y = + 1/1000000000.0;
  min.x = + 1000000000.0;
  min.y = + 1000000000.0;
  size.x = 10.0;
  size.y = 10.0;
  center.x = 0.0;
  center.y = 0.0;
  grow = true;
  steps = 0; 	
}

void t_world::Update(t_vector &pos)
{
   grow = false;
   
   if ((pos.x==0) || (pos.y==0))
      return;
      
   if (max.x < pos.x)           
       max.x = pos.x;

   if (max.y < pos.y)           
       max.y = pos.y;

   if (min.x > pos.x)           
       min.x = pos.x;

   if (min.y > pos.y)           
       min.y = pos.y;


   grow = ((max.x - min.x) > size.x) || ((max.y - min.y) > size.y);

   if (grow)
      {      
#ifdef DEBUG
       fprintf(stderr,"world.update(%g,%g)\n",pos.x,pos.y);
#endif
       NewSize();	
      } 
}


void t_world::NewSize(void)
{
       size.Init(max);
       size.Sub(&min);		
 
       center.Init(size);
       center.Mult(0.5);
       center.Add(&min);
}


void t_world::Shift(t_vector &dir)
{
	max.Add(&dir);
	min.Add(&dir);
	center.Add(&dir);	
}


void t_world::WrapMove(t_vector &pos)
{
//	char moved = false;
	t_vector s;

	s.Init(0.0, 0.0);

	if (pos.x > max.x)
	    s.x = pos.x - max.x;

	if (pos.y > max.y)
	    s.y = pos.y - max.y;

	if (pos.x < min.x)
	    s.x = pos.x - min.x;

	if (pos.y < min.y)
	    s.y = pos.y - min.y;

	if ((s.x != 0.0) || (s.y != 0.0))
	   {
	    max.Add(&s);
	    min.Add(&s);
	    center.Add(&s);
	    fprintf(stderr,"wrapmoved: (%g,%g)\n",s.x,s.y);
	   }
}


void t_world::Expand(t_vector &dir)
{
	t_vector n,a,b;

	n.Init(dir);
	n.Mult(2.0);
	
	a.Init(min);
	b.Init(max);
	
	a.Add(&n);
	b.Add(&n);

	Update(a);
	Update(b);


//	NewSize();
//	grow = true;	
}


void t_world::Draw(void)
{
	double vx = (center.x - min.x) / size.x; 	
	double vy = (center.y - min.y) / size.y; 	

	int ix = (int)(vx * (double)VScr.size);
	int iy = (int)(vy * (double)VScr.size);

	drawline(ix - 20,iy,ix + 20,iy);
	drawline(ix,iy - 20,ix,iy + 20);
}









/**/
/**/
/**/
/**/
/**/
/**/
/**/
/**/
/*****  End Of File  ****************************/
