# 1 "tile.c"



















static char rcsid[] = "$Header: tile.c,v 6.0 90/08/28 18:02:49 mayo Exp $";


# 1 "/usr/include/stdio.h"



























 

















                                                                   




# 1 "/usr/include/standards.h"



























 


































                                                                   



























































































# 52 "/usr/include/stdio.h"























typedef unsigned long   size_t;




typedef long    fpos_t;




































# 1 "/usr/include/sys/seek.h"



















































# 118 "/usr/include/stdio.h"

typedef struct {
	int	_cnt;
	unsigned char	*_ptr;
	unsigned char	*_base;
	int	_bufsiz;
	short	_flag;
	short	_file;




	char	*__newbase;
	void	*_lock;			
	unsigned char	*_bufendp;
} FILE;

extern FILE	_iob[];









extern int     fread();
extern int     fwrite();
















extern int	_flsbuf ();
extern int	_filbuf ();
extern int 	ferror ();
extern int 	feof ();
extern void 	clearerr ();
extern int 	putchar ();
extern int 	getchar ();
extern int 	putc ();
extern int 	getc ();
extern int	remove ();
extern int	rename ();
extern FILE 	*tmpfile ();
extern char 	*tmpnam ();
extern int 	fclose ();
extern int 	fflush ();
extern FILE	*fopen ();
extern FILE 	*freopen ();
extern void 	setbuf ();
extern int 	setvbuf ();
extern int	fprintf ();
extern int	fscanf ();
extern int	printf ();
extern int	scanf ();
extern int	sprintf ();
extern int	sscanf ();








# 1 "/usr/include/va_list.h"



































































typedef struct {
	char	*_a0;		
	int	_offset;		
} va_list;













# 197 "/usr/include/stdio.h"
extern int  vfprintf ();
extern int  vprintf ();
extern int  vsprintf ();











extern int 	fgetc ();
extern char 	*fgets ();
extern int 	fputc ();
extern int 	fputs ();
extern char 	*gets ();
extern int 	puts ();
extern int	ungetc ();
extern int	fgetpos ();
extern int 	fseek ();
extern int	fsetpos ();
extern long	ftell ();
extern void	rewind ();
extern void 	perror ();





























# 304 "/usr/include/stdio.h"
























# 1 "/usr/include/sys/types.h"



























 
















































# 1 "/usr/include/standards.h"



























 


































                                                                   
# 154 "/usr/include/standards.h"

# 78 "/usr/include/sys/types.h"










typedef signed long     ptrdiff_t;














    typedef unsigned int  wchar_t;






typedef unsigned int wctype_t;









typedef int            time_t;




typedef int             clock_t;














typedef long                    ssize_t; 





typedef	unsigned char	uchar_t;
typedef	unsigned short	ushort_t;
typedef	unsigned int	uint_t;
typedef unsigned long	ulong_t;
typedef	volatile unsigned char	vuchar_t;
typedef	volatile unsigned short	vushort_t;
typedef	volatile unsigned int	vuint_t;
typedef volatile unsigned long	vulong_t;












































typedef	struct	{ long r[1]; } *physadr_t;




typedef	struct	label_t	{
	long	val[10];
} label_t;




typedef int		level_t;
typedef	int		daddr_t;	
typedef	char *		caddr_t;	
typedef long *		qaddr_t;        
typedef char *          addr_t;
typedef	uint_t		ino_t;		
typedef short		cnt_t;
typedef int		dev_t;		
typedef	int		chan_t;		




typedef long    off_t;			


typedef unsigned long	rlim_t;		
typedef	int		paddr_t;
typedef	ushort_t	nlink_t;



typedef int    		key_t;		




typedef	uint_t		mode_t;		




typedef uint_t		uid_t;		




typedef uint_t		gid_t;		


typedef	void *		mid_t;		



typedef	int		pid_t;		


typedef char		slab_t[12];	








typedef pid_t		id_t;		
					
					
					
					






typedef ulong_t		shmatt_t;	
typedef ulong_t		msgqnum_t;	
typedef ulong_t		msglen_t;	



        typedef unsigned int wint_t;         





typedef unsigned long	sigset_t;







typedef long            timer_t;        

















typedef void (*sig_t)();




typedef uint_t	major_t;      
typedef uint_t	minor_t;      
typedef uint_t	devs_t;       
typedef uint_t	unit_t;       



















# 1 "/usr/include/mach/machine/vm_types.h"



























 












































typedef	unsigned long	vm_offset_t;
typedef	unsigned long	vm_size_t;



# 344 "/usr/include/sys/types.h"




typedef	uchar_t		uchar;
typedef	ushort_t	ushort;
typedef	uint_t		uint;
typedef ulong_t		ulong;

typedef	physadr_t	physadr;



typedef	uchar_t		u_char;
typedef	ushort_t 	u_short;
typedef	uint_t		u_int;
typedef	ulong_t		u_long;
typedef	vuchar_t	vu_char;
typedef	vushort_t 	vu_short;
typedef	vuint_t		vu_int;
typedef	vulong_t	vu_long;




typedef struct  _quad { int val[2]; } quad;


typedef	long	swblk_t;
typedef u_long	fixpt_t;



















typedef int	fd_mask;






typedef	struct fd_set {
	fd_mask	fds_bits[(((4096)+(( (sizeof(fd_mask) * 8		)	)-1))/( (sizeof(fd_mask) * 8		)	))];
} fd_set;













# 329 "/usr/include/stdio.h"








extern int 	fileno ();
extern FILE 	*fdopen ();

extern char *ctermid ();
extern char *cuserid ();


















# 1 "/usr/include/getopt.h"































 







# 1 "/usr/include/standards.h"



























 


































                                                                   
# 154 "/usr/include/standards.h"

# 41 "/usr/include/getopt.h"





extern int getopt ();




extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;


# 361 "/usr/include/stdio.h"








extern char	*ctermid ();
extern int 	getw ();
extern int 	pclose ();
extern int 	putw ();
extern FILE 	*popen ();
extern char 	*tempnam ();









# 1 "/usr/include/sys/limits.h"



























 


































                                                                   



# 1 "/usr/include/standards.h"



























 


































                                                                   
# 154 "/usr/include/standards.h"

# 68 "/usr/include/sys/limits.h"












# 1 "/usr/include/machine/machlimits.h"



























 












































					



					











# 81 "/usr/include/sys/limits.h"





























































# 1 "/usr/include/sys/syslimits.h"



























 





















































# 143 "/usr/include/sys/limits.h"

























					


















































# 1 "/usr/include/machine/machtime.h"



























 











































# 220 "/usr/include/sys/limits.h"







                       





# 1 "/usr/include/sys/rt_limits.h"


























































# 234 "/usr/include/sys/limits.h"



# 385 "/usr/include/stdio.h"
































extern void 	setbuffer ();
extern void 	setlinebuf ();






# 466 "/usr/include/stdio.h"





# 24 "tile.c"
# 1 "../include/magic.h"


























typedef char *	ClientData;






























extern char *strcpy(), *strncpy(), *index(), *rindex();
extern char *strcat(), *strncat();




extern char CellLibPath[];	
extern char SysLibPath[];	























































extern char *MagicVersion;
extern char AbortMessage[];


















    































































    error1 = You_need_to_define_LITTLE_ENDIAN_or_BIG_ENDIAN_for_your_machine.





    








































































    extern char* sprintf();




# 25 "tile.c"
# 1 "../include/malloc.h"



































extern char *mallocMagic();





































union store {
    union store	*ptr;
    unsigned size;		
    int dummy[1];
};
































struct mallocPageHdr
{
    struct mallocPage	*mPh_next;	
    struct mallocPage	*mPh_prev;	
    union store		*mPh_free;	
    short		 mPh_threshobjs;
    short		 mPh_freeobjs;	
};

struct mallocPage
{
    struct mallocPageHdr	mP_hdr;
    char			mP_data[(4096 - sizeof (struct mallocPageHdr))];
};


































































extern struct mallocPage *mallocPageFirst[];
# 26 "tile.c"
# 1 "../include/geometry.h"






























typedef struct
{
    int p_x;
    int p_y;
} Point;











typedef struct
{
    Point r_ll;			
    Point r_ur;			
} Rect;






typedef struct G1		
{
    Rect r_r;			
    struct G1 *r_next;		
} LinkedRect;



















typedef struct
{
    int t_a, t_b, t_c, t_d, t_e, t_f;
} Transform;






























































    































extern int GeoNameToPos();
extern char * GeoPosToName();
extern void GeoTransRect(), GeoTransTrans();
extern void GeoTransPoint(), GeoInvertTrans();
extern void GeoTranslateTrans();
extern void GeoTransTranslate();
extern int GeoInclude(), GeoTransPos(), GeoIncludeAll();
extern void GeoClip();
extern void GeoClipPoint();
extern int GeoRectPointSide();
extern int GeoRectRectSide();
extern void GeoIncludePoint();
extern void GeoDecomposeTransform();







extern Transform GeoIdentityTransform;
extern Transform GeoUpsideDownTransform;
extern Transform GeoSidewaysTransform;
extern Transform Geo90Transform;
extern Transform Geo180Transform;
extern Transform Geo270Transform;
extern Transform GeoRef45Transform;
extern Transform GeoRef135Transform;

extern Rect GeoNullRect;

extern int GeoOppositePos[];
# 27 "tile.c"
# 1 "./tile.h"



























































typedef struct tile
{
    ClientData	 ti_body;	
    struct tile	*ti_lb;		
    struct tile	*ti_bl;		
    struct tile	*ti_tr;		
    struct tile	*ti_rt;		
    Point	 ti_ll;		
    ClientData	 ti_client;	




} Tile;

    











































typedef struct
{
    Tile	*pl_left;	
    Tile	*pl_top;	
    Tile	*pl_right;	
    Tile	*pl_bottom;	
    Tile	*pl_hint;	


} Plane;






























extern Plane *TiNewPlane();
extern void TiFreePlane();
extern void TiToRect();
extern Tile *TiSplitX();
extern Tile *TiSplitY();
extern Tile *TiSplitX_Left();
extern Tile *TiSplitY_Bottom();
extern void  TiJoinX();
extern void  TiJoinY();
extern int   TiSrArea();
extern Tile *TiSrPoint();

















Tile *TiAlloc();
void TiFree();





























 







































extern Rect TiPlaneRect;	




# 28 "tile.c"







 Rect TiPlaneRect = { (-((1 << (8*sizeof (int) - 6)) - 4))+2, (-((1 << (8*sizeof (int) - 6)) - 4))+2, ((1 << (8*sizeof (int) - 6)) - 4)-2, ((1 << (8*sizeof (int) - 6)) - 4)-2 };




















Plane *
TiNewPlane(tile)
    register Tile *tile;


{
    register Plane *newplane;
    static Tile *infinityTile = (Tile *) 0L;

        { 	register unsigned nwX_ = ((( sizeof (Plane)) + ((sizeof (int))-1)) >> 2				    ); 	register struct mallocPage *ppX_; 	register union store *spX_;  	if (nwX_ < (((150	) + ((sizeof (int))-1)) >> 2				    ) 		&& (ppX_ = mallocPageFirst[nwX_]) 		&& (spX_ = ppX_->mP_hdr.mPh_free)) { 	    ppX_->mP_hdr.mPh_free = spX_->ptr; 	    spX_->size = nwX_; 	    ( newplane) = (Plane *) ( (char *) (((union store *) (spX_)) + 1) ); 	} else ( newplane) = (Plane *) mallocMagic((unsigned) ( sizeof (Plane)));     };
    newplane->pl_top = TiAlloc();
    newplane->pl_right = TiAlloc();
    newplane->pl_bottom = TiAlloc();
    newplane->pl_left = TiAlloc();

    







    if (infinityTile == (Tile *) 0L)
    {
	infinityTile = TiAlloc();
	((infinityTile)->ti_ll.p_x) = ((1 << (8*sizeof (int) - 6)) - 4)+1;
	((infinityTile)->ti_ll.p_y) = ((1 << (8*sizeof (int) - 6)) - 4)+1;
    }

    if (tile)
    {
	((tile)->ti_rt) = newplane->pl_top;
	((tile)->ti_tr) = newplane->pl_right;
	((tile)->ti_lb) = newplane->pl_bottom;
	((tile)->ti_bl) = newplane->pl_left;
    }

    ((newplane->pl_bottom)->ti_ll.p_x) = (-((1 << (8*sizeof (int) - 6)) - 4));
    ((newplane->pl_bottom)->ti_ll.p_y) = (-((1 << (8*sizeof (int) - 6)) - 4));
    ((newplane->pl_bottom)->ti_rt) = tile;
    ((newplane->pl_bottom)->ti_tr) = newplane->pl_right;
    ((newplane->pl_bottom)->ti_lb) = ((Tile *) -1)	;
    ((newplane->pl_bottom)->ti_bl) = newplane->pl_left;
    ((newplane->pl_bottom)->ti_body = (ClientData) ( -1));

    ((newplane->pl_top)->ti_ll.p_x) = (-((1 << (8*sizeof (int) - 6)) - 4));
    ((newplane->pl_top)->ti_ll.p_y) = ((1 << (8*sizeof (int) - 6)) - 4);
    ((newplane->pl_top)->ti_rt) = infinityTile;
    ((newplane->pl_top)->ti_tr) = newplane->pl_right;
    ((newplane->pl_top)->ti_lb) = tile;
    ((newplane->pl_top)->ti_bl) = newplane->pl_left;
    ((newplane->pl_top)->ti_body = (ClientData) ( -1));

    ((newplane->pl_left)->ti_ll.p_x) = (-((1 << (8*sizeof (int) - 6)) - 4));
    ((newplane->pl_left)->ti_ll.p_y) = (-((1 << (8*sizeof (int) - 6)) - 4));
    ((newplane->pl_left)->ti_rt) = newplane->pl_top;
    ((newplane->pl_left)->ti_tr) = tile;
    ((newplane->pl_left)->ti_lb) = newplane->pl_bottom;
    ((newplane->pl_left)->ti_bl) = ((Tile *) -1)	;
    ((newplane->pl_left)->ti_body = (ClientData) ( -1));

    ((newplane->pl_right)->ti_ll.p_x) = ((1 << (8*sizeof (int) - 6)) - 4);
    ((newplane->pl_right)->ti_ll.p_y) = (-((1 << (8*sizeof (int) - 6)) - 4));
    ((newplane->pl_right)->ti_rt) = newplane->pl_top;
    ((newplane->pl_right)->ti_tr) = infinityTile;
    ((newplane->pl_right)->ti_lb) = newplane->pl_bottom;
    ((newplane->pl_right)->ti_bl) = tile;
    ((newplane->pl_right)->ti_body = (ClientData) ( -1));

    newplane->pl_hint = tile;
    return (newplane);
}


















void
TiFreePlane(plane)
    Plane *plane;	
{
        { 	register union store *spX_ = ( ((union store *) ((char *) plane->pl_left)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) plane->pl_left); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
        { 	register union store *spX_ = ( ((union store *) ((char *) plane->pl_right)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) plane->pl_right); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
        { 	register union store *spX_ = ( ((union store *) ((char *) plane->pl_top)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) plane->pl_top); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
        { 	register union store *spX_ = ( ((union store *) ((char *) plane->pl_bottom)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) plane->pl_bottom); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
        { 	register union store *spX_ = ( ((union store *) ((char *) plane)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) plane); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
}

















void
TiToRect(tile, rect)
    register Tile *tile; 
    register Rect *rect; 
{
    rect->r_ll.p_x = ((tile)->ti_ll.p_x);
    rect->r_ur.p_x = (((((tile)->ti_tr))->ti_ll.p_x));
    rect->r_ll.p_y = ((tile)->ti_ll.p_y);
    rect->r_ur.p_y = (((((tile)->ti_rt))->ti_ll.p_y));
}





















Tile *
TiSplitX(tile, x)
    register Tile *tile;	
    register int x;		
{
    register Tile *newtile;
    register Tile *tp;

        ((!(x > ((tile)->ti_ll.p_x) && x < (((((tile)->ti_tr))->ti_ll.p_x)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiSplitX", "x > LEFT(tile) && x < RIGHT(tile)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);

        { 	register unsigned nwX_ = ((( sizeof (Tile)) + ((sizeof (int))-1)) >> 2				    ); 	register struct mallocPage *ppX_; 	register union store *spX_;  	if (nwX_ < (((150	) + ((sizeof (int))-1)) >> 2				    ) 		&& (ppX_ = mallocPageFirst[nwX_]) 		&& (spX_ = ppX_->mP_hdr.mPh_free)) { 	    ppX_->mP_hdr.mPh_free = spX_->ptr; 	    spX_->size = nwX_; 	    ( newtile) = (Tile *) ( (char *) (((union store *) (spX_)) + 1) ); 	} else ( newtile) = (Tile *) mallocMagic((unsigned) ( sizeof (Tile)));     };
    newtile->ti_client = (ClientData) (-((1 << (8*sizeof (int) - 6)) - 4));
    newtile->ti_body = (ClientData) 0;

    ((newtile)->ti_ll.p_x) = x;
    ((newtile)->ti_ll.p_y) = ((tile)->ti_ll.p_y);
    ((newtile)->ti_bl) = tile;
    ((newtile)->ti_tr) = ((tile)->ti_tr);
    ((newtile)->ti_rt) = ((tile)->ti_rt);

    



    for (tp = ((tile)->ti_tr); ((tp)->ti_bl) == tile; tp = ((tp)->ti_lb))
	((tp)->ti_bl) = newtile;
    ((tile)->ti_tr) = newtile;

    



    for (tp = ((tile)->ti_rt); ((tp)->ti_ll.p_x) >= x; tp = ((tp)->ti_bl))
	((tp)->ti_lb) = newtile;
    ((tile)->ti_rt) = tp;

    



    for (tp = ((tile)->ti_lb); (((((tp)->ti_tr))->ti_ll.p_x)) <= x; tp = ((tp)->ti_tr))
	;
    ((newtile)->ti_lb) = tp;
    while (((tp)->ti_rt) == tile)
    {
	((tp)->ti_rt) = newtile;
	tp = ((tp)->ti_tr);
    }

    return (newtile);
}





















Tile *
TiSplitY(tile, y)
    register Tile *tile;	
    register int y;		
{
    register Tile *newtile;
    register Tile *tp;

        ((!(y > ((tile)->ti_ll.p_y) && y < (((((tile)->ti_rt))->ti_ll.p_y)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiSplitY", "y > BOTTOM(tile) && y < TOP(tile)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);

        { 	register unsigned nwX_ = ((( sizeof (Tile)) + ((sizeof (int))-1)) >> 2				    ); 	register struct mallocPage *ppX_; 	register union store *spX_;  	if (nwX_ < (((150	) + ((sizeof (int))-1)) >> 2				    ) 		&& (ppX_ = mallocPageFirst[nwX_]) 		&& (spX_ = ppX_->mP_hdr.mPh_free)) { 	    ppX_->mP_hdr.mPh_free = spX_->ptr; 	    spX_->size = nwX_; 	    ( newtile) = (Tile *) ( (char *) (((union store *) (spX_)) + 1) ); 	} else ( newtile) = (Tile *) mallocMagic((unsigned) ( sizeof (Tile)));     };
    newtile->ti_client = (ClientData) (-((1 << (8*sizeof (int) - 6)) - 4));
    newtile->ti_body = (ClientData) 0;

    ((newtile)->ti_ll.p_x) = ((tile)->ti_ll.p_x);
    ((newtile)->ti_ll.p_y) = y;
    ((newtile)->ti_lb) = tile;
    ((newtile)->ti_rt) = ((tile)->ti_rt);
    ((newtile)->ti_tr) = ((tile)->ti_tr);

    



    for (tp = ((tile)->ti_rt); ((tp)->ti_lb) == tile; tp = ((tp)->ti_bl))
	((tp)->ti_lb) = newtile;
    ((tile)->ti_rt) = newtile;

    



    for (tp = ((tile)->ti_tr); ((tp)->ti_ll.p_y) >= y; tp = ((tp)->ti_lb))
	((tp)->ti_bl) = newtile;
    ((tile)->ti_tr) = tp;

    



    for (tp = ((tile)->ti_bl); (((((tp)->ti_rt))->ti_ll.p_y)) <= y; tp = ((tp)->ti_rt))
	;
    ((newtile)->ti_bl) = tp;
    while (((tp)->ti_tr) == tile)
    {
	((tp)->ti_tr) = newtile;
	tp = ((tp)->ti_rt);
    }

    return (newtile);
}






















Tile *
TiSplitX_Left(tile, x)
    register Tile *tile;	
    register int x;		
{
    register Tile *newtile;
    register Tile *tp;

        ((!(x > ((tile)->ti_ll.p_x) && x < (((((tile)->ti_tr))->ti_ll.p_x)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiSplitX", "x > LEFT(tile) && x < RIGHT(tile)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);

        { 	register unsigned nwX_ = ((( sizeof (Tile)) + ((sizeof (int))-1)) >> 2				    ); 	register struct mallocPage *ppX_; 	register union store *spX_;  	if (nwX_ < (((150	) + ((sizeof (int))-1)) >> 2				    ) 		&& (ppX_ = mallocPageFirst[nwX_]) 		&& (spX_ = ppX_->mP_hdr.mPh_free)) { 	    ppX_->mP_hdr.mPh_free = spX_->ptr; 	    spX_->size = nwX_; 	    ( newtile) = (Tile *) ( (char *) (((union store *) (spX_)) + 1) ); 	} else ( newtile) = (Tile *) mallocMagic((unsigned) ( sizeof (Tile)));     };
    newtile->ti_client = (ClientData) (-((1 << (8*sizeof (int) - 6)) - 4));
    newtile->ti_body = (ClientData) 0;

    ((newtile)->ti_ll.p_x) = ((tile)->ti_ll.p_x);
    ((tile)->ti_ll.p_x) = x;
    ((newtile)->ti_ll.p_y) = ((tile)->ti_ll.p_y);

    ((newtile)->ti_bl) = ((tile)->ti_bl);
    ((newtile)->ti_lb) = ((tile)->ti_lb);
    ((newtile)->ti_tr) = tile;
    ((tile)->ti_bl) = newtile;

    
    for (tp = ((newtile)->ti_bl); ((tp)->ti_tr) == tile; tp = ((tp)->ti_rt))
	((tp)->ti_tr) = newtile;

    
    for (tp = ((tile)->ti_rt); ((tp)->ti_ll.p_x) >= x; tp = ((tp)->ti_bl))
	;
    ((newtile)->ti_rt) = tp;
    for ( ; ((tp)->ti_lb) == tile; tp = ((tp)->ti_bl))
	((tp)->ti_lb) = newtile;

    
    for (tp = ((tile)->ti_lb); (((((tp)->ti_tr))->ti_ll.p_x)) <= x; tp = ((tp)->ti_tr))
	((tp)->ti_rt) = newtile;
    ((tile)->ti_lb) = tp;

    return (newtile);
}






















Tile *
TiSplitY_Bottom(tile, y)
    register Tile *tile;	
    register int y;		
{
    register Tile *newtile;
    register Tile *tp;

        ((!(y > ((tile)->ti_ll.p_y) && y < (((((tile)->ti_rt))->ti_ll.p_y)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiSplitY", "y > BOTTOM(tile) && y < TOP(tile)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);

        { 	register unsigned nwX_ = ((( sizeof (Tile)) + ((sizeof (int))-1)) >> 2				    ); 	register struct mallocPage *ppX_; 	register union store *spX_;  	if (nwX_ < (((150	) + ((sizeof (int))-1)) >> 2				    ) 		&& (ppX_ = mallocPageFirst[nwX_]) 		&& (spX_ = ppX_->mP_hdr.mPh_free)) { 	    ppX_->mP_hdr.mPh_free = spX_->ptr; 	    spX_->size = nwX_; 	    ( newtile) = (Tile *) ( (char *) (((union store *) (spX_)) + 1) ); 	} else ( newtile) = (Tile *) mallocMagic((unsigned) ( sizeof (Tile)));     };
    newtile->ti_client = (ClientData) (-((1 << (8*sizeof (int) - 6)) - 4));
    newtile->ti_body = (ClientData) 0;

    ((newtile)->ti_ll.p_x) = ((tile)->ti_ll.p_x);
    ((newtile)->ti_ll.p_y) = ((tile)->ti_ll.p_y);
    ((tile)->ti_ll.p_y) = y;

    ((newtile)->ti_rt) = tile;
    ((newtile)->ti_lb) = ((tile)->ti_lb);
    ((newtile)->ti_bl) = ((tile)->ti_bl);
    ((tile)->ti_lb) = newtile;

    
    for (tp = ((newtile)->ti_lb); ((tp)->ti_rt) == tile; tp = ((tp)->ti_tr))
	((tp)->ti_rt) = newtile;

    
    for (tp = ((tile)->ti_tr); ((tp)->ti_ll.p_y) >= y; tp = ((tp)->ti_lb))
	;
    ((newtile)->ti_tr) = tp;
    for ( ; ((tp)->ti_bl) == tile; tp = ((tp)->ti_lb))
	((tp)->ti_bl) = newtile;

    
    for (tp = ((tile)->ti_bl); (((((tp)->ti_rt))->ti_ll.p_y)) <= y; tp = ((tp)->ti_rt))
	((tp)->ti_tr) = newtile;
    ((tile)->ti_bl) = tp;

    return (newtile);
}























void
TiJoinX(tile1, tile2, plane)
    Tile *tile1;	
    Tile *tile2;	
    Plane *plane;	
{
    Tile *tp;

    










        ((!(((tile1)->ti_ll.p_y)==((tile2)->ti_ll.p_y) && (((((tile1)->ti_rt))->ti_ll.p_y))==(((((tile2)->ti_rt))->ti_ll.p_y)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiJoinX", "BOTTOM(tile1)==BOTTOM(tile2) && TOP(tile1)==TOP(tile2)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);
        ((!(((tile1)->ti_ll.p_x)==(((((tile2)->ti_tr))->ti_ll.p_x)) || (((((tile1)->ti_tr))->ti_ll.p_x))==((tile2)->ti_ll.p_x))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiJoinX", "LEFT(tile1)==RIGHT(tile2) || RIGHT(tile1)==LEFT(tile2)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);

    



    for (tp = ((tile2)->ti_rt); ((tp)->ti_lb) == tile2; tp = ((tp)->ti_bl))
	((tp)->ti_lb) = tile1;

    



    for (tp = ((tile2)->ti_lb); ((tp)->ti_rt) == tile2; tp = ((tp)->ti_tr))
	((tp)->ti_rt) = tile1;

    




        ((!(((tile1)->ti_ll.p_x) != ((tile2)->ti_ll.p_x))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiJoinX", "LEFT(tile1) != LEFT(tile2)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);
    if (((tile1)->ti_ll.p_x) < ((tile2)->ti_ll.p_x))
    {
	for (tp = ((tile2)->ti_tr); ((tp)->ti_bl) == tile2; tp = ((tp)->ti_lb))
	    ((tp)->ti_bl) = tile1;
	((tile1)->ti_tr) = ((tile2)->ti_tr);
	((tile1)->ti_rt) = ((tile2)->ti_rt);
    }
    else
    {
	for (tp = ((tile2)->ti_bl); ((tp)->ti_tr) == tile2; tp = ((tp)->ti_rt))
	    ((tp)->ti_tr) = tile1;
	((tile1)->ti_bl) = ((tile2)->ti_bl);
	((tile1)->ti_lb) = ((tile2)->ti_lb);
	((tile1)->ti_ll.p_x) = ((tile2)->ti_ll.p_x);
    }

    if (plane->pl_hint == tile2)
	plane->pl_hint = tile1;
        { 	register union store *spX_ = ( ((union store *) ((char *) tile2)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) tile2); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
}























void
TiJoinY(tile1, tile2, plane)
    Tile *tile1;	
    Tile *tile2;	
    Plane *plane;	
{
    Tile *tp;

    










        ((!(((tile1)->ti_ll.p_x)==((tile2)->ti_ll.p_x) && (((((tile1)->ti_tr))->ti_ll.p_x))==(((((tile2)->ti_tr))->ti_ll.p_x)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiJoinY", "LEFT(tile1)==LEFT(tile2) && RIGHT(tile1)==RIGHT(tile2)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);
        ((!((((((tile1)->ti_rt))->ti_ll.p_y))==((tile2)->ti_ll.p_y) || ((tile1)->ti_ll.p_y)==(((((tile2)->ti_rt))->ti_ll.p_y)))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiJoinY", "TOP(tile1)==BOTTOM(tile2) || BOTTOM(tile1)==TOP(tile2)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);

    



    for (tp = ((tile2)->ti_tr); ((tp)->ti_bl) == tile2; tp = ((tp)->ti_lb))
	((tp)->ti_bl) = tile1;

    



    for (tp = ((tile2)->ti_bl); ((tp)->ti_tr) == tile2; tp = ((tp)->ti_rt))
	((tp)->ti_tr) = tile1;

    




        ((!(((tile1)->ti_ll.p_y) != ((tile2)->ti_ll.p_y))) 	? (sprintf(AbortMessage, "%s botched: %s\n",  	     "TiJoinY", "BOTTOM(tile1) != BOTTOM(tile2)"), 	fputs(AbortMessage, (&_iob[2])), 	niceabort(), 	1) 	: 0);
    if (((tile1)->ti_ll.p_y) < ((tile2)->ti_ll.p_y))
    {
	for (tp = ((tile2)->ti_rt); ((tp)->ti_lb) == tile2; tp = ((tp)->ti_bl))
	    ((tp)->ti_lb) = tile1;
	((tile1)->ti_rt) = ((tile2)->ti_rt);
	((tile1)->ti_tr) = ((tile2)->ti_tr);
    }
    else
    {
	for (tp = ((tile2)->ti_lb); ((tp)->ti_rt) == tile2; tp = ((tp)->ti_tr))
	    ((tp)->ti_rt) = tile1;
	((tile1)->ti_lb) = ((tile2)->ti_lb);
	((tile1)->ti_bl) = ((tile2)->ti_bl);
	((tile1)->ti_ll.p_y) = ((tile2)->ti_ll.p_y);
    }

    if (plane->pl_hint == tile2)
	plane->pl_hint = tile1;
        { 	register union store *spX_ = ( ((union store *) ((char *) tile2)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) tile2); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
}

Tile *
TiAlloc()
{
    Tile *newtile;

        { 	register unsigned nwX_ = ((( sizeof (Tile)) + ((sizeof (int))-1)) >> 2				    ); 	register struct mallocPage *ppX_; 	register union store *spX_;  	if (nwX_ < (((150	) + ((sizeof (int))-1)) >> 2				    ) 		&& (ppX_ = mallocPageFirst[nwX_]) 		&& (spX_ = ppX_->mP_hdr.mPh_free)) { 	    ppX_->mP_hdr.mPh_free = spX_->ptr; 	    spX_->size = nwX_; 	    ( newtile) = (Tile *) ( (char *) (((union store *) (spX_)) + 1) ); 	} else ( newtile) = (Tile *) mallocMagic((unsigned) ( sizeof (Tile)));     };
    newtile->ti_client = (ClientData) (-((1 << (8*sizeof (int) - 6)) - 4));
    newtile->ti_body = (ClientData) 0;
    return (newtile);
}

void
TiFree(tp)
    Tile *tp;
{
        { 	register union store *spX_ = ( ((union store *) ((char *) tp)) - 1 ); 	register struct mallocPage *ppX_;  	if (( (spX_)->size >= (((150	) + ((sizeof (int))-1)) >> 2				    ) )) freeMagic((char *) tp); 	else 	{ 	    ppX_ = ((struct mallocPage *) (((int) spX_) & (~(4096-1)))); 	    if (ppX_->mP_hdr.mPh_prev 			&& (++(ppX_->mP_hdr.mPh_freeobjs) == ppX_->mP_hdr.mPh_threshobjs)) 		mallocFreePage(ppX_, spX_); 	    else 	        spX_->ptr = ppX_->mP_hdr.mPh_free, ppX_->mP_hdr.mPh_free = spX_; 	}     };
}







tiPrint(tp)
    Tile *tp;
{
    printf("tp=0x%x LL=(%d,%d) body=0x%x\n",
	tp, ((tp)->ti_ll.p_x), ((tp)->ti_ll.p_y), tp->ti_body);
    printf("BL=0x%x LB=0x%x RT=0x%x TR=0x%x\n", ((tp)->ti_bl), ((tp)->ti_lb), ((tp)->ti_rt), ((tp)->ti_tr));
}

tiPrintAll(tp)
    Tile *tp;
{
    tiPrint(tp);
    printf("UR=(%d,%d)\n", (((((tp)->ti_tr))->ti_ll.p_x)), (((((tp)->ti_rt))->ti_ll.p_y)));

    
    printf("LEAD=%d\n", (int) tp->ti_client);
}
