Following is the portion of the LineGenerator Javadoc detailing the algorithm used for generating lines.
This page has been generated to accompany Cartesian Plane Lesson 6: Unit Testing.
Line Generation Algorithm
Following are the constraints and parameters governing the generation of lines within the bounds of a given rectangle.
Given:
- rectXco: The x-coordinate of the upper left corner of the rectangle.
- rectYco: The y-coordinate of the upper left corner of the rectangle.
- rectWidth: The width of the rectangle.
- rectHeight: The height of the rectangle.
- [rule: boundingRect] The rectangle bounding the grid is given by the user of this class.
- The left-bound of the rectangle (rectXco) is inside the rectangle.
- The upper-bound of the rectangle (rectYco) is inside the rectangle.
- The right-bound of the rectangle, given by rectXco + rectWidth, is outside the rectangle.
- The lower-bound of the rectangle, given by rectYco + rectHeight, is outside the rectangle.
- [rule: gridUnit] The grid unit (gridUnit), is given by the user of this class; it is the number of pixels (pixels-per-unit or PPU) allocated to the length of a unit.
- [rule: lpu] The lines-per-unit (lpu), is given by the user of this class; it is the number of lines to be drawn for each unit.
- [rule: gridSpacing] The distance between two consecutive grid lines (gridSpacing) is given by gridSpacing = gridUnit / lpu.
- [rule: xAxis] The coordinates of the x-axis are given by y = rectYco + (rectHeight - 1) / 2 (centerYco) for x in the range [rectXco, rectXco + rectWidth).
- [rule: yAxis] The coordinates of the y-axis are given by x = rectXco + (rectWidth - 1) / 2 (centerXco) for y in the range [rectYco, rectYco + rectHeight).
- [rule: numHLinesAbove] The number of horizontal lines above the x-axis is calculated as floor((rectHeight - 1) / 2 / gridSpacing).
- [rule: numHLinesBelow] The number of horizontal lines below the x-axis is always the same as the number of horizontal lines above the x-axis.
- [rule: numHLinesTotal] The total number of horizontal lines is calculated as 2 * floor((rectHeight - 1) / 2 / gridSpacing) + 1.
- [rule: numVLinesLeft] The number of vertical lines left of the y-axis is calculated as floor((rectWidth - 1) / 2 / gridSpacing).
- [rule: numVLinesRight] The number of vertical lines right of the y-axis is always the same as the number of vertical lines left of the y-axis.
- [rule: numVLinesTotal] The total number of vertical lines is calculated as 2 * floor((rectWidth - 1) / 2 / gridSpacing) + 1.
- [rule: nthHLineAbove] The y-coordinate of the nth horizontal line above the x-axis is given by -n * gridSpacing.
- [rule: nthHLineBelow] The y-coordinate of the nth horizontal line below the x-axis is given by n * gridSpacing.
- [rule: nthVLineLeft] The x-coordinate of the nth vertical line left of the y-axis is given by -n * gridSpacing.
- [rule: nthVLineRight] The x-coordinate of the nth vertical line right of the y-axis is given by n * gridSpacing.
- A line segment is defined as a horizontal or vertical line with a specific length n (i.e. the line does not span the width or height of the bounding rectangle).
- [rule: hLineSegmentXco1] The left x-coordinate of a horizontal line segment of length n is the x-coordinate of the y-axis minus n / 2.
- [rule: hLineSegmentXco2] The right x-coordinate of a horizontal line segment of length n left x-coordinate plus n.
- [rule: vLineSegmentYco1] The upper y-coordinate of a vertical line segment of length n is the y-coordinate of the x-axis minus n / 2.
- [rule: vLineSegmentYco2] The lower y-coordinate of a vertical line segment of length n upper y-coordinate plus n.
Caveats:
-
[rule: cumulativeRoundingErrors]
Given:
- The possibility of cumulative rounding errors;
- The fact that calculations by the AWT are not always the most helpful to line positioning;
- The difficulties inherent in converting user coordinates to device coordinates (see the documentation for java.awt.Graphics2D);
- [rule: clipRegionErrors] The clip-region processing by the AWT is occasionally off by a pixel. From a testing and validation point of view this should be tolerated.
No comments:
Post a Comment