1 /* ActiveEdges.java -- A collection of active edges for scanline conversion
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package gnu.java.awt.java2d;
41 import gnu.java.lang.CPStringBuilder;
44 * A collection of active edges for scanline conversion.
46 final class ActiveEdges
50 * The active edges. This can contain null values at arbirary locations.
51 * The method #sort() packs this together.
53 private PolyEdge[] activeEdges;
56 * The actual number of active edges. The array can be bigger than this
59 private int numActiveEdges;
62 * Creates a new ActiveEdges object.
66 activeEdges = new PolyEdge[8];
71 * Clears out all active edges. This is cheap as it simply resets the
72 * counter to 0. It does not release all references to PolyEdge instances.
80 * Adds the specified edge to the list of active edges. This does not yet
81 * sort the edges and therefore does destroy any order of the list.
83 * @param edge the edge to add
85 void add(PolyEdge edge)
87 // Grow array when necessary.
88 int oldSize = activeEdges.length;
89 if (numActiveEdges >= oldSize)
91 int newSize = oldSize + oldSize / 4 + 1;
92 PolyEdge[] newEdges = new PolyEdge[newSize];
93 System.arraycopy(activeEdges, 0, newEdges, 0, oldSize);
94 activeEdges = newEdges;
96 activeEdges[numActiveEdges] = edge;
101 * Intersects all active edges, sorts them according to their intersection
102 * points and packs the array to remove unneeded edges. This does also
103 * remove any edges that do not intersect the scanline (i.e. they end above
106 * @param y the scanline height
108 void intersectSortAndPack(int n, int y)
110 // Intersect and pack in one go.
113 for (int i = 0; i < numActiveEdges; i++)
115 PolyEdge edge = activeEdges[i];
116 // Clear out edge that ends above the scanline.
117 if (edge != null && edge.y1 >= y)
119 assert edge.y1 >= y && edge.y0 <= y : "edge must cross scanline";
120 edge.intersect(n, y);
121 activeEdges[last] = edge;
124 // Bubble up the added edge.
125 for (int j = last - 1; j > 0; j--)
127 if (activeEdges[j].xIntersection
128 < activeEdges[j - 1].xIntersection)
130 tmp = activeEdges[j];
131 activeEdges[j] = activeEdges[j - 1];
132 activeEdges[j - 1] = tmp;
136 // The beginning of the list is already sorted.
142 numActiveEdges = last;
147 * Returns the number of active edges. This is only reliable after a
148 * call to {@link #intersectSortAndPack(int, int)}.
150 * @return the number of active edges
152 int getNumActiveEdges()
154 return numActiveEdges;
158 * Returns the active edge at the position <code>i</code>.
162 * @return the active edge at the specified index
164 PolyEdge getActiveEdge(int i)
166 return activeEdges[i];
170 * Removes all edges that end above the specified height.
172 * @param y the cut-off height
176 for (int i = 0; i < numActiveEdges; i++)
178 PolyEdge edge = activeEdges[i];
179 if (edge != null && edge.y1 < y)
181 activeEdges[i] = null;
186 public String toString()
188 CPStringBuilder s = new CPStringBuilder();
189 s.append("[ActiveEdges] ");
190 for (int i = 0; i < numActiveEdges; i++)
192 s.append(activeEdges[i]);