FlexiblePatternCoordinateConverter.java

00001 package edu.stanford.hci.r3.pattern.coordinates.conversion;
00002 
00003 import edu.stanford.hci.r3.paper.Region;
00004 import edu.stanford.hci.r3.units.Percentage;
00005 import edu.stanford.hci.r3.units.Units;
00006 import edu.stanford.hci.r3.units.coordinates.PercentageCoordinates;
00007 import edu.stanford.hci.r3.units.coordinates.StreamedPatternCoordinates;
00008 
00028 public class FlexiblePatternCoordinateConverter implements PatternCoordinateConverter {
00029 
00030         private StreamedPatternCoordinates anchor;
00031 
00032         private double anchorXVal;
00033 
00034         private double anchorYVal;
00035 
00036         private double bottomMostBorder;
00037 
00038         private final Units height;
00039 
00040         private double heightInDots;
00041 
00042         private Region region;
00043 
00044         private double rightMostBorder;
00045 
00046         private final Units width;
00047 
00048         private double widthInDots;
00049 
00053         public FlexiblePatternCoordinateConverter(Region floatingRegion) {
00054                 region = floatingRegion;
00055                 width = region.getWidth();
00056                 height = region.getHeight();
00057                 widthInDots = width.getValueInPatternDots();
00058                 heightInDots = height.getValueInPatternDots();
00059         }
00060 
00065         public boolean contains(StreamedPatternCoordinates coord) {
00066                 if (anchor == null) {
00067                         setAnchor(coord);
00068                 }
00069                 final double xval = coord.getXVal();
00070                 final double yval = coord.getYVal();
00071                 return xval >= anchorXVal && //
00072                                 xval < rightMostBorder && //
00073                                 yval >= anchorYVal && //
00074                                 yval < bottomMostBorder;
00075         }
00076 
00077         /*
00078          * (non-Javadoc)
00079          * 
00080          * @see edu.stanford.hci.r3.pattern.coordinates.conversion.PatternCoordinateConverter#getArea()
00081          */
00082         @Override
00083         public double getArea() {
00084                 return widthInDots * heightInDots;
00085         }
00086 
00087         /*
00088          * (non-Javadoc)
00089          * 
00090          * @see edu.stanford.hci.r3.pattern.coordinates.conversion.PatternCoordinateConverter#getRegionName()
00091          */
00092         public String getRegionName() {
00093                 return region.getName();
00094         }
00095 
00096         /*
00097          * (non-Javadoc)
00098          * 
00099          * @see edu.stanford.hci.r3.pattern.coordinates.conversion.PatternCoordinateConverter#getRelativeLocation(edu.stanford.hci.r3.units.coordinates.StreamedPatternCoordinates)
00100          */
00101         public PercentageCoordinates getRelativeLocation(StreamedPatternCoordinates coord) {
00102                 if (anchor == null) {
00103                         setAnchor(coord);
00104                 }
00105                 final double xval = coord.getXVal();
00106                 final double yval = coord.getYVal();
00107 
00108                 final double pctX = (xval - anchorXVal) / widthInDots * 100;
00109                 final double pctY = (yval - anchorYVal) / heightInDots * 100;
00110 
00111                 return new PercentageCoordinates( // 
00112                                 new Percentage(pctX, width), // fraction of width
00113                                 new Percentage(pctY, height)); // fraction of height
00114         }
00115 
00116         private void setAnchor(StreamedPatternCoordinates coord) {
00117                 anchor = coord;
00118                 anchorXVal = anchor.getXVal();
00119                 anchorYVal = anchor.getYVal();
00120                 rightMostBorder = anchorXVal + widthInDots;
00121                 bottomMostBorder = anchorYVal + heightInDots;
00122         }
00123 }

Generated on Sat Apr 14 18:21:35 2007 for R3 Paper Toolkit by  doxygen 1.4.7