001 package com.croftsoft.core.awt;
002
003 import java.awt.*;
004
005 /*********************************************************************
006 * <P>
007 * Supplementary static methods for the java.awt.Graphics class.
008 * <P>
009 * @version
010 * 1997-04-16
011 * @author
012 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
013 *********************************************************************/
014
015 public final class GraphicsLib {
016 //////////////////////////////////////////////////////////////////////
017 //////////////////////////////////////////////////////////////////////
018
019 //////////////////////////////////////////////////////////////////////
020 //////////////////////////////////////////////////////////////////////
021
022 /*********************************************************************
023 * Coughs a hairball if the targetPoint and the originPoint are equal.
024 *********************************************************************/
025 public static Point [ ] rasterize (
026 Point originPoint,
027 Point targetPoint ) {
028 //////////////////////////////////////////////////////////////////////
029 int delta_x = targetPoint.x - originPoint.x;
030 int delta_y = targetPoint.y - originPoint.y;
031 // if ( ( delta_x == 0 ) && ( delta_y == 0 ) ) return null;
032 Point [ ] array;
033 int step = -1;
034 int y = originPoint.y;
035 int num_y = delta_y > 0 ? delta_y : -delta_y;
036 if ( delta_x == 0 ) {
037 array = new Point [ num_y ];
038 if ( targetPoint.y > originPoint.y ) step = 1;
039 for ( int i = 0; i < array.length; i++ ) {
040 y += step;
041 array [ i ] = new Point ( originPoint.x, y );
042 }
043 return array;
044 }
045 int x = originPoint.x;
046 int num_x = delta_x > 0 ? delta_x : -delta_x;
047 if ( delta_y == 0 ) {
048 array = new Point [ num_x ];
049 if ( targetPoint.x > originPoint.x ) step = 1;
050 for ( int i = 0; i < array.length; i++ ) {
051 x += step;
052 array [ i ] = new Point ( x, originPoint.y );
053 }
054 return array;
055 }
056 double m = ( ( double ) delta_y ) / ( ( double ) delta_x );
057 double b = y - m * x;
058 if ( num_y > num_x ) {
059 array = new Point [ num_y ];
060 if ( targetPoint.y > originPoint.y ) step = 1;
061 for ( int i = 0; i < array.length; i++ ) {
062 y += step;
063 double x_double = ( y - b ) / m;
064 if ( x_double > x + 0.5 ) x++;
065 else if ( x_double < x - 0.5 ) x--;
066 array [ i ] = new Point ( x, y );
067 }
068 } else {
069 array = new Point [ num_x ];
070 if ( targetPoint.x > originPoint.x ) step = 1;
071 for ( int i = 0; i < array.length; i++ ) {
072 x += step;
073 double y_double = m * x + b;
074 if ( y_double > y + 0.5 ) y++;
075 else if ( y_double < y - 0.5 ) y--;
076 array [ i ] = new Point ( x, y );
077 }
078 }
079 return array;
080 }
081
082 //////////////////////////////////////////////////////////////////////
083 //////////////////////////////////////////////////////////////////////
084 }