Class Lattice2D

java.lang.Object
  |
  +--Lattice2D

class Lattice2D
extends java.lang.Object

Class to assist in the display of a 2D lattice.


Inner Class Summary
static interface Lattice2D.Basis
          Class representing a basis.
 class Lattice2D.CircularBasis
          An implementation of Basis which displays as a circle.
 class Lattice2D.PolygonBasis
          Polygon-based implementation of a basis
 
Field Summary
protected  Lattice2D.Basis basis
          The selected basis
protected  java.awt.Color basisCol
          The colour to use when drawing the bases
protected  int basisSize
          Constant scaling factor for the size of the bases.
static int MARK_RADIUS
          Size of the draggable blobs on the ends of the vectors
static double THRESHOLD
          Square of the radius of the circle of points around the end of a vector such that a click within that circle is considered close enough to be interpreted as a click on said vector.
protected  java.awt.Color[] vectorCols
          A two-element array containing colours (that's right, with a 'u') for the vectors
 Vector2D[] vectors
          A two-element array containing the lattice vectors
protected  double xscl
          The ratio between x coordinates in the model and those in screen space.
protected  double yscl
          The ratio between x coordinates in the model and those in screen space.
 
Constructor Summary
Lattice2D()
          Creates a new lattice with orthogonal lattice vectors.
Lattice2D(double x1, double y1, double x2, double y2)
          Creates a new lattice
Lattice2D(Vector2D[] vectors)
          Creates a new lattice
Lattice2D(Vector2D v1, Vector2D v2)
          Creates a new lattice
 
Method Summary
 Vector2D chooseVector(int x, int y, int w, int h)
          Returns the vector that is closest (of the two lattice vectors) to the specified screen coordinates.
protected  double distanceFrom(Vector2D point, int vector)
          Returns the distance between the specifed point and one of the two lattice vectors.
 Vector2D fromScreen(int x, int y, int w, int h)
          Converts a pair of coordinates from screen space to a lattice space vector.
 double fromScreenX(int x, int w)
          Converts an x coordinate from screen space to lattice space
 double fromScreenY(int y, int h)
          Converts a y coordinate from screen space to lattice space
 void paint(java.awt.Graphics g, int w, int h)
          Displays this lattice, inlcuding the two vectors.
protected  void paintLattice(java.awt.Graphics g, int w, int h)
          Paints just the lattice (without the vectors).
protected  void paintVector(java.awt.Graphics g, int w, int h, int vector)
          Paints a vector.
 void setBasis(Lattice2D.Basis b)
          Sets the selected basis
 int toScreenX(double x, int w)
          Converts an x coordinate from lattice space to screen space
 int toScreenY(double y, int h)
          Converts a y coordinate from lattice space to screen space
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

basis

protected Lattice2D.Basis basis
The selected basis

vectorCols

protected java.awt.Color[] vectorCols
A two-element array containing colours (that's right, with a 'u') for the vectors

basisCol

protected java.awt.Color basisCol
The colour to use when drawing the bases

xscl

protected double xscl
The ratio between x coordinates in the model and those in screen space.

yscl

protected double yscl
The ratio between x coordinates in the model and those in screen space.

vectors

public Vector2D[] vectors
A two-element array containing the lattice vectors

basisSize

protected int basisSize
Constant scaling factor for the size of the bases.

MARK_RADIUS

public static final int MARK_RADIUS
Size of the draggable blobs on the ends of the vectors

THRESHOLD

public static final double THRESHOLD
Square of the radius of the circle of points around the end of a vector such that a click within that circle is considered close enough to be interpreted as a click on said vector.
Constructor Detail

Lattice2D

public Lattice2D(Vector2D[] vectors)
Creates a new lattice
Parameters:
vectors - A two-element array containing the lattice vectors

Lattice2D

public Lattice2D(Vector2D v1,
                 Vector2D v2)
Creates a new lattice
Parameters:
v1 - The first lattice vector
v2 - The second lattice vector

Lattice2D

public Lattice2D(double x1,
                 double y1,
                 double x2,
                 double y2)
Creates a new lattice
Parameters:
x1 - The x component of the first lattice vector
y1 - The y component of the first lattice vector
x2 - The x component of the second lattice vector
y2 - The y component of the second lattice vector

Lattice2D

public Lattice2D()
Creates a new lattice with orthogonal lattice vectors.
Method Detail

toScreenX

public int toScreenX(double x,
                     int w)
Converts an x coordinate from lattice space to screen space
Parameters:
x - The lattice coordinate
w - The width of the display

toScreenY

public int toScreenY(double y,
                     int h)
Converts a y coordinate from lattice space to screen space
Parameters:
y - The lattice coordinate
h - The height of the display

fromScreenX

public double fromScreenX(int x,
                          int w)
Converts an x coordinate from screen space to lattice space
Parameters:
x - The screen coordinate
w - The width of the display

fromScreenY

public double fromScreenY(int y,
                          int h)
Converts a y coordinate from screen space to lattice space
Parameters:
y - The screen coordinate
h - The height of the display

fromScreen

public Vector2D fromScreen(int x,
                           int y,
                           int w,
                           int h)
Converts a pair of coordinates from screen space to a lattice space vector.
Parameters:
x - The screen x coordinate
y - The screen y coordinate
w - The width of the display
h - The height of the display

paint

public void paint(java.awt.Graphics g,
                  int w,
                  int h)
Displays this lattice, inlcuding the two vectors.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the area on which to paint
h - The height (in pixels) of the area on which to paint

paintVector

protected void paintVector(java.awt.Graphics g,
                           int w,
                           int h,
                           int vector)
Paints a vector.
Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the area on which to paint
h - The height (in pixels) of the area on which to paint
vector - The vector to paint

chooseVector

public Vector2D chooseVector(int x,
                             int y,
                             int w,
                             int h)
Returns the vector that is closest (of the two lattice vectors) to the specified screen coordinates.
Parameters:
x - The screen x coordinate
y - The screen y coordinate
w - The width of the display
h - The height of the display

distanceFrom

protected double distanceFrom(Vector2D point,
                              int vector)
Returns the distance between the specifed point and one of the two lattice vectors.
Parameters:
point - The point
vector - The index (0 or 1) of a lattice vector

paintLattice

protected void paintLattice(java.awt.Graphics g,
                            int w,
                            int h)
Paints just the lattice (without the vectors).

The basic algorithm is to display a "grid" of bases, each at a position av1 + bv2, where a, b are integers in the interval [-30,30].

However, with near-dependent lattice vectors, this is often insufficient; a large number of bases will fall outside the view area, and the view area will not be entirely filled.

The solution is to make use of the fact that u, v describe the same vector space as u, v - nu, where n is any integer. The two lattice vectors will be transformed in this way, subtracting the smaller from the larger, until the modulae are minimised, at which point the vectors will be the closest-to-orthogonal vectors which still describe the same vector space.

Parameters:
g - The Graphics surface on which to paint
w - The width (in pixels) of the area on which to paint
h - The height (in pixels) of the area on which to paint

setBasis

public void setBasis(Lattice2D.Basis b)
Sets the selected basis