SRJRCFrames
v0.1.296

de.schwarzrot.util
Class GeometricCalculator

java.lang.Object
  extended by de.schwarzrot.util.GeometricCalculator

public class GeometricCalculator
extends Object

helper class to support intersection shapes between rectangle and circle and offers calls around circle calculations, as well as line calculations. Rectangle is the base, which can be intersected/subtracted by circles. First use-case is a sun in a rectangle surrounded by shapes that symbolize the shafts of sunlight. This use-case is supported by calcQuadrant, which determines the quadrant of the circles center and based on the quadrant the maximum angles for the shafts of sunlights. Base of calculation is the inner rectangle taking into account the different padding settings.

Additionally the class offers helper functions, that calculate a circle dot or a border dot - based on given angle. All angles for calculations must be in radians.

Author:
Reinhard Mantey

Field Summary
protected static double DELTA_FACTOR
           
 
Constructor Summary
GeometricCalculator(int width, int height, int xCenter, int yCenter, int radius)
          constructor to create a geometric calculator
GeometricCalculator(PageProperties pp)
          constructor that uses given PageProperties as internal data store
 
Method Summary
protected  int angleQuadrant(double angle)
          calculates the quadrant where the angle points to.
 int[] calcBorderPoint(double angle)
          calculates a border point from rectangle (0, 0, width, height) using internal settings of circle center and rectangle dimensions.
 int[] calcBorderPoint(int xCenter, int yCenter, double angle)
          calculates a border point from rectangle (0, 0, width, height) using the given coordinates as center of the circle and given angle.
 int[] calcBorderPoint(int xCenter, int yCenter, int xMin, int xMax, int yMin, int yMax, double angle)
          caculates a border point from rectangle (xMin, yMin, xMax, yMax) using the given coordinates as center of the circle and given angle.
 int[] calcCirclePoint(double angle)
          calculate a point from circles border using given angle to hit the border from circles center.
 int[] calcCirclePoint(int radius, double angle)
          calculate a point from circles border using given angle to hit the border from circles center.
 int[] calcCirclePoint(int xCenter, int yCenter, int radius, double angle)
          calculate a point from circles border using given angle to hit the border from circles center.
 int[] calcLimits(double startAngle, double endAngle)
          calculates the limits based on calculator settings and given angles.
 int[] calcLimits(int xCenter, int yCenter, double startAngle, double endAngle)
          calculates the limits based on calculator settings and given angles.
protected  int[] calcLimits(int xMin, int yMin, int xMax, int yMax, int xCenter, int yCenter, double startAngle, double endAngle)
           
 double calcLineAngle(Coordinate p0, Coordinate p1)
          calculates the angle of the given line
 double calcLineGradient(Coordinate p0, Coordinate p1)
          calculates the gradient of given line
 Coordinate calcLinesCrossPoint(Coordinate p0, Coordinate p1, Coordinate p2, Coordinate p3)
          calculates the intersection of the line {p0, p1} with the line {p2, p3} - line {p2, p3} should not be a vertical line, in deed the failure minimizes with gradient of line 2 beeing not vertical.
 int[] calcOvalPoint(double circleAngle, double shearAngle)
           
 int[] calcOvalPoint(int radius, double circleAngle, double shearAngle)
           
 int[] calcOvalPoint(int xCenter, int yCenter, int radius, double circleAngle, double shearAngle)
           
 int[] calcPadBorderPoint(double angle)
          calculates a border point using given angle from circles center.
 int[] calcPadBorderPoint(int xCenter, int yCenter, double angle)
          calculates a border point using given angle from circles center.
 int[] calcPadBorderPoint(int xCenter, int yCenter, double angle, int padLeft, int padTop, int padRight, int padBottom)
          calculates a border point using given angle from circles center.
 double[] calcQuadrant()
          base of major use-case: calculates first and last angle for shafts of sunlight base on the circles settings.
protected  double[] calcQuadrant(int xMin, int yMin, int xMax, int yMax, int xCenter, int yCenter, int r)
           
 int getBottomPadding()
           
 int getHeight()
           
 int getLeftPadding()
           
 int getRadius()
           
 int getRightPadding()
           
 int getTopPadding()
           
 int getWidth()
           
 int getXCenter()
           
 int getYCenter()
           
 void setBottomPadding(int bottomPadding)
           
static void setDebug(boolean debugFlag)
           
 void setHeight(int height)
           
 void setHorizontalPadding(int padding)
           
 void setLeftPadding(int leftPadding)
           
 void setPadding(int padding)
           
 void setRadius(int radius)
           
 void setRightPadding(int rightPadding)
           
 void setTopPadding(int topPadding)
           
 void setVerticalPadding(int padding)
           
 void setWidth(int width)
           
 void setXCenter(int xCenter)
           
 void setYCenter(int yCenter)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELTA_FACTOR

protected static final double DELTA_FACTOR
See Also:
Constant Field Values
Constructor Detail

GeometricCalculator

public GeometricCalculator(int width,
                           int height,
                           int xCenter,
                           int yCenter,
                           int radius)
constructor to create a geometric calculator

Parameters:
width - - rectangles width (unlikely to be subject of change)
height - - rectangles height (unlikely to be subject of change)
xCenter - - the x coordinate of the center of the circle
yCenter - - the y coordinate of the center of the circle
radius - - the circles radius

GeometricCalculator

public GeometricCalculator(PageProperties pp)
constructor that uses given PageProperties as internal data store

Parameters:
pp - - the PageProperties to use
Method Detail

calcBorderPoint

public int[] calcBorderPoint(double angle)
calculates a border point from rectangle (0, 0, width, height) using internal settings of circle center and rectangle dimensions.

Parameters:
angle - - the angle to use to hit the border (in radians)
Returns:
- array of 2 integers { x, y }

calcBorderPoint

public int[] calcBorderPoint(int xCenter,
                             int yCenter,
                             double angle)
calculates a border point from rectangle (0, 0, width, height) using the given coordinates as center of the circle and given angle.

Parameters:
xCenter - - x coordinate of the center of the circle
yCenter - - y coordinate of the center of the circle
angle - - the angle to use to hit the border (in radians)
Returns:
- array of 2 integers { x, y }

calcBorderPoint

public int[] calcBorderPoint(int xCenter,
                             int yCenter,
                             int xMin,
                             int xMax,
                             int yMin,
                             int yMax,
                             double angle)
caculates a border point from rectangle (xMin, yMin, xMax, yMax) using the given coordinates as center of the circle and given angle.

Parameters:
xCenter - - x coordinate of the center of the circle
yCenter - - y coordinate of the center of the circle
xMin - - minimal x of the rectangle
xMax - - maximal x of the rectangle
yMin - - minimal y of the rectangle
yMax - - maximal y of the rectangle
angle - - the angle to use to hit the border
Returns:
- array of 2 integers { x, y }

calcCirclePoint

public int[] calcCirclePoint(double angle)
calculate a point from circles border using given angle to hit the border from circles center. Uses the internal settings of circles center and radius.

Parameters:
angle - - the angle to use to hit the circles border
Returns:
- array of 2 integers { x, y }

calcCirclePoint

public int[] calcCirclePoint(int radius,
                             double angle)
calculate a point from circles border using given angle to hit the border from circles center. Uses given radius and internal settings for circles center.

Parameters:
radius - - the circles radius
angle - - the angle to use to hit the circles border
Returns:
- array of 2 integers { x, y }

calcCirclePoint

public int[] calcCirclePoint(int xCenter,
                             int yCenter,
                             int radius,
                             double angle)
calculate a point from circles border using given angle to hit the border from circles center.

Parameters:
xCenter - - x coordinate of circles center
yCenter - - y coordinate of circles center
radius - - radius of the circle
angle - - angle to use to hit circles border
Returns:
- array of 2 integers { x, y }

calcLimits

public int[] calcLimits(double startAngle,
                        double endAngle)
calculates the limits based on calculator settings and given angles. Returns an array of 12 integers, which together with the circles center form a path, that can be cut away from desired shape. Uses internal center of circle.

Parameters:
startAngle - - one angle that encloses desired shape
endAngle - - other angle that encloses desired shape
Returns:
- array of 12 integers { x0, y0, x1, y1, x0Cut0, y0Cut0, x0Cut1, y0Cut1, x1Cut0, y1Cut0, x1Cut1, y1Cut1 }, so two GeneralPaths can be created like this to reduce rectangle to shape enclosed by startAngle and endAngle:
 
  int[] limits = calcLimit(startAngle, endAngle);
  
  gp0 = new GeneralPath(); // cuts the rectangle before startAngle 
  gp1 = new GeneralPath(); // cuts the rectangle after endAngle

  gp0.moveTo(xCenter, yCenter);
  gp0.lineTo(limits[0], limits[1]);
  gp0.lineTo(limits[4], limits[5]);
  gp0.lineTo(limits[6], limits[7]);
  gp0.closePath();

  gp1.moveTo(xCenter, yCenter);
  gp1.lineTo(limits[2], limits[3]);
  gp1.lineTo(limits[8], limits[9]);
  gp1.lineTo(limits[10], limits[11]);
  gp1.closePath();
  
  Area a = new Area(getBounds());
  
  a.subtract(new Area(gp0));
  a.subtract(new Area(gp1));
 

calcLimits

public int[] calcLimits(int xCenter,
                        int yCenter,
                        double startAngle,
                        double endAngle)
calculates the limits based on calculator settings and given angles. Returns an array of 12 integers, which together with the circles center form a path, that can be cut away from desired shape.

Parameters:
xCenter - - x coordinate of circles center
yCenter - - y coordinate of circles center
startAngle - - one angle that encloses desired shape
endAngle - - other angle that encloses desired shape
Returns:
- array of 12 integers { x0, y0, x1, y1, x0Cut0, y0Cut0, x0Cut1, y0Cut1, x1Cut0, y1Cut0, x1Cut1, y1Cut1 }, so 2 GeneralPath can be created like this to reduce rectangle to shape enclosed by startAngle and endAngle:
 
  int[] limits = calcLimit(startAngle, endAngle);
  
  gp0 = new GeneralPath(); // cuts the rectangle before startAngle 
  gp1 = new GeneralPath(); // cuts the rectangle after endAngle

  gp0.moveTo(xCenter, yCenter);
  gp0.lineTo(limits[0], limits[1]);
  gp0.lineTo(limits[4], limits[5]);
  gp0.lineTo(limits[6], limits[7]);
  gp0.closePath();

  gp1.moveTo(xCenter, yCenter);
  gp1.lineTo(limits[2], limits[3]);
  gp1.lineTo(limits[8], limits[9]);
  gp1.lineTo(limits[10], limits[11]);
  gp1.closePath();
  
  Area a = new Area(getBounds());
  
  a.subtract(new Area(gp0));
  a.subtract(new Area(gp1));
 

calcLineAngle

public double calcLineAngle(Coordinate p0,
                            Coordinate p1)
calculates the angle of the given line

Parameters:
p0 - - first point from line
p1 - - second point from line
Returns:
- the angle of the line in radians

calcLineGradient

public double calcLineGradient(Coordinate p0,
                               Coordinate p1)
calculates the gradient of given line

Parameters:
p0 - - first point from line
p1 - - second point from line
Returns:
- the lines gradient

calcLinesCrossPoint

public Coordinate calcLinesCrossPoint(Coordinate p0,
                                      Coordinate p1,
                                      Coordinate p2,
                                      Coordinate p3)
calculates the intersection of the line {p0, p1} with the line {p2, p3} - line {p2, p3} should not be a vertical line, in deed the failure minimizes with gradient of line 2 beeing not vertical.

Parameters:
p0 - - first point from first line
p1 - - second point from first line
p2 - - first point from second line
p3 - - second point from second line
Returns:
- the coordinate of the point, where both lines intersect

calcOvalPoint

public int[] calcOvalPoint(double circleAngle,
                           double shearAngle)

calcOvalPoint

public int[] calcOvalPoint(int radius,
                           double circleAngle,
                           double shearAngle)

calcOvalPoint

public int[] calcOvalPoint(int xCenter,
                           int yCenter,
                           int radius,
                           double circleAngle,
                           double shearAngle)

calcPadBorderPoint

public int[] calcPadBorderPoint(double angle)
calculates a border point using given angle from circles center. Opposed to calcBorderPoint this method uses the inner rectangle taking into account the various padding settings. Uses internal center of circle and internal padding settings.

Parameters:
angle - - the angle to use to hit the border from circles center
Returns:
- array of 2 integers { x, y }

calcPadBorderPoint

public int[] calcPadBorderPoint(int xCenter,
                                int yCenter,
                                double angle)
calculates a border point using given angle from circles center. Opposed to calcBorderPoint this method uses the inner rectangle taking into account the various padding settings. Uses internal padding settings.

Parameters:
xCenter - - x coordinate of circles center
yCenter - - y coordinate of circles center
angle - - the angle to use to hit the border from circles center
Returns:
- array of 2 integers { x, y }

calcPadBorderPoint

public int[] calcPadBorderPoint(int xCenter,
                                int yCenter,
                                double angle,
                                int padLeft,
                                int padTop,
                                int padRight,
                                int padBottom)
calculates a border point using given angle from circles center. Opposed to calcBorderPoint this method uses the inner rectangle taking into account the various padding settings.

Parameters:
xCenter - - x coordinate of circles center
yCenter - - y coordinate of circles center
angle - - the angle to use to hit the border from circles center
padLeft - - left side padding
padTop - - top side padding
padRight - - right side padding
padBottom - - bottom side padding
Returns:
- array of 2 integers { x, y }

calcQuadrant

public double[] calcQuadrant()
base of major use-case: calculates first and last angle for shafts of sunlight base on the circles settings. Circles center defines the quadrant. This calculation is based on the inner rectangle defined by padding settings. Dividing resulting width and height in half creates four rectangles (the quadrants) in the order: top-left, top-right, bottom-right, bottom-left.

Returns:
- an array of 4 double:
  • startAngle
  • used radius in debug mode - otherwise undefined
  • endAngle
  • number of quadrant - in the range 1 to 4

getBottomPadding

public int getBottomPadding()

getHeight

public int getHeight()

getLeftPadding

public int getLeftPadding()

getRadius

public int getRadius()

getRightPadding

public int getRightPadding()

getTopPadding

public int getTopPadding()

getWidth

public int getWidth()

getXCenter

public int getXCenter()

getYCenter

public int getYCenter()

setBottomPadding

public void setBottomPadding(int bottomPadding)

setHeight

public void setHeight(int height)

setHorizontalPadding

public void setHorizontalPadding(int padding)

setLeftPadding

public void setLeftPadding(int leftPadding)

setPadding

public void setPadding(int padding)

setRadius

public void setRadius(int radius)

setRightPadding

public void setRightPadding(int rightPadding)

setTopPadding

public void setTopPadding(int topPadding)

setVerticalPadding

public void setVerticalPadding(int padding)

setWidth

public void setWidth(int width)

setXCenter

public void setXCenter(int xCenter)

setYCenter

public void setYCenter(int yCenter)

angleQuadrant

protected int angleQuadrant(double angle)
calculates the quadrant where the angle points to. Quadrant logic follows the logic from calcQuadrant.

Parameters:
angle - - the angle to evaluate
Returns:
- number of quadrant (from 1 to 4)

calcLimits

protected int[] calcLimits(int xMin,
                           int yMin,
                           int xMax,
                           int yMax,
                           int xCenter,
                           int yCenter,
                           double startAngle,
                           double endAngle)

calcQuadrant

protected double[] calcQuadrant(int xMin,
                                int yMin,
                                int xMax,
                                int yMax,
                                int xCenter,
                                int yCenter,
                                int r)

setDebug

public static void setDebug(boolean debugFlag)

SRJRCFrames
v0.1.296

hosted at
Find SRJRCFrames at SourceForge.net. Fast, secure and free:
           Open Source Software download
Submit a bug or request a feature

SRJRCFrames is published according to the GNU General Public License
Copyright 2005-2012 Reinhard Mantey - some rights reserved.