Source for gnu.java.awt.java2d.PolyEdge

   1: /* PolyEdge.java -- An edge in a polygon, used for polygon filling
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.awt.java2d;
  40: 
  41: /**
  42:  * An edge in a polygon. This is used by the scanline conversion algorithm
  43:  * implemented in {@link AbstractGraphics2D#rawFillShape}.
  44:  *
  45:  * @author Roman Kennke (kennke@aicas.com)
  46:  */
  47: public class PolyEdge
  48:   implements Comparable
  49: {
  50: 
  51:   /**
  52:    * The start and end coordinates of the edge. y0 is always smaller or equal
  53:    * than y1.
  54:    */
  55:   public double x0, y0, x1, y1;
  56: 
  57:   /**
  58:    * The slope of the edge. This is dx / dy.
  59:    */
  60:   double slope;
  61: 
  62:   /**
  63:    * The intersection of this edge with the current scanline.
  64:    */
  65:   double xIntersection;
  66: 
  67:   /**
  68:    * Indicates whether this edge is from the clip or from the target shape.
  69:    */
  70:   boolean isClip;
  71: 
  72:   /**
  73:    * Creates a new PolyEdge with the specified coordinates.
  74:    *
  75:    * @param x0 the starting point, x coordinate
  76:    * @param y0 the starting point, y coordinate
  77:    * @param x1 the end point, x coordinate
  78:    * @param y1 the end point, y coordinate
  79:    */
  80:   PolyEdge(double x0, double y0, double x1, double y1, boolean clip)
  81:   {
  82:     isClip = clip;
  83:     if (y0 < y1)
  84:       {
  85:         this.x0 = x0;
  86:         this.y0 = y0;
  87:         this.x1 = x1;
  88:         this.y1 = y1;
  89:       }
  90:     else
  91:       {
  92:         this.x0 = x1;
  93:         this.y0 = y1;
  94:         this.x1 = x0;
  95:         this.y1 = y0;
  96:       }
  97:     slope = (this.x1 - this.x0) / (this.y1 - this.y0);
  98:     if (this.y0 == this.y1) // Horizontal edge.
  99:       xIntersection = Math.min(this.x0, this.x1);
 100:     else
 101:       xIntersection = this.x0 + slope * (Math.ceil(this.y0) - this.y0);
 102:   }
 103: 
 104:   /**
 105:    * Sorts PolyEdges by the x coordinate from the minimum x value.
 106:    */
 107:   public int compareTo(Object o)
 108:   {
 109:     PolyEdge other = (PolyEdge) o;
 110:     int comp = 0;
 111:     if (x0 < other.x0)
 112:       comp = -1;
 113:     else if (x0 > other.x0)
 114:       comp = 1;
 115:     return comp;
 116:   }
 117: 
 118:   public String toString()
 119:   {
 120:     return "Edge: " + x0 + ", " + y0 + ", " + x1 + ", " + y1 + ", slope: "
 121:            + slope + ", xIntersection: " + xIntersection
 122:            + ", isClip: " + isClip;
 123:   }
 124: }