// // WeightMap2D.java // Anneal // // Created by sw on Sun Sep 15 2002. // Copyright (c) 2001 __MyCompanyName__. All rights reserved. // // WeightMap2D extends WeightMap to 2D in the "obvious" way: // positions in the 2D space are aligned with positions in the // WeightMap by laying the rows end to end. // // set(), get(), total() and pick() are implemented, but not // totalLeftOf(), because the corresponding meaning would be // implementation-dependent and peculiar. // // pick() has to return a point to return both x and y. // set() and get() are defined for points and for x,y pairs. package net.tiac.home.sw.anneal; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class WeightMap2D { private int xsize, ysize; private WeightMap map; public WeightMap2D( int xsize, int ysize ) { this.xsize = xsize; this.ysize = ysize; map = new WeightMap( xsize * ysize ); } private int mapIndex( int x, int y ) { return( y * xsize + x ); } public void set( Point p, int w ) { set( p.x, p.y, w ); } public void set( int x, int y, int w ) { map.set( mapIndex( x, y ), w ); } public int get( Point p ) { return( get( p.x, p.y ) ); } public int get( int x, int y ) { return( map.get( mapIndex( x, y ) ) ); } public int getTotal( ) { return( map.getTotal( ) ); } public Point pick( RandomRange rg ) { Point p; int r = map.pick( rg ); if( r == 0 ) { return( null ); } else { return( new Point( r % xsize, r / xsize ) ); } } }