OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / awt / java2d / ActiveEdges.java
1 /* ActiveEdges.java -- A collection of active edges for scanline conversion
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 import gnu.java.lang.CPStringBuilder;
42
43 /**
44  * A collection of active edges for scanline conversion.
45  */
46 final class ActiveEdges
47 {
48
49   /**
50    * The active edges. This can contain null values at arbirary locations.
51    * The method #sort() packs this together.
52    */
53   private PolyEdge[] activeEdges;
54
55   /**
56    * The actual number of active edges. The array can be bigger than this
57    * number.
58    */
59   private int numActiveEdges;
60
61   /**
62    * Creates a new ActiveEdges object.
63    */
64   ActiveEdges()
65   {
66     activeEdges = new PolyEdge[8];
67     numActiveEdges = 0;
68   }
69
70   /**
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.
73    */
74   void clear()
75   {
76     numActiveEdges = 0;
77   }
78
79   /**
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.
82    *
83    * @param edge the edge to add
84    */
85   void add(PolyEdge edge)
86   {
87     // Grow array when necessary.
88     int oldSize = activeEdges.length;
89     if (numActiveEdges >= oldSize)
90       {
91         int newSize = oldSize + oldSize / 4 + 1;
92         PolyEdge[] newEdges = new PolyEdge[newSize];
93         System.arraycopy(activeEdges, 0, newEdges, 0, oldSize);
94         activeEdges = newEdges;
95       }
96     activeEdges[numActiveEdges] = edge;
97     numActiveEdges++;
98   }
99
100   /**
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
104    * of the scanline).
105    *
106    * @param y the scanline height
107    */
108   void intersectSortAndPack(int n, int y)
109   {
110     // Intersect and pack in one go.
111     int last = 0;
112     PolyEdge tmp;
113     for (int i = 0; i < numActiveEdges; i++)
114       {
115         PolyEdge edge = activeEdges[i];
116         // Clear out edge that ends above the scanline.
117         if (edge != null && edge.y1 >= y)
118           {
119             assert edge.y1 >= y && edge.y0 <= y : "edge must cross scanline";
120             edge.intersect(n, y);
121             activeEdges[last] = edge;
122             last++;
123
124             // Bubble up the added edge.
125             for (int j = last - 1; j > 0; j--)
126               {
127                 if (activeEdges[j].xIntersection
128                     < activeEdges[j - 1].xIntersection)
129                   {
130                     tmp = activeEdges[j];
131                     activeEdges[j] = activeEdges[j - 1];
132                     activeEdges[j - 1] = tmp;
133                   }
134                 else
135                   {
136                     // The beginning of the list is already sorted.
137                     break;
138                   }
139               }
140           }
141       }
142     numActiveEdges = last;
143
144   }
145
146   /**
147    * Returns the number of active edges. This is only reliable after a
148    * call to {@link #intersectSortAndPack(int, int)}.
149    *
150    * @return the number of active edges
151    */
152   int getNumActiveEdges()
153   {
154     return numActiveEdges;
155   }
156
157   /**
158    * Returns the active edge at the position <code>i</code>.
159    *
160    * @param i the index
161    *
162    * @return the active edge at the specified index
163    */
164   PolyEdge getActiveEdge(int i)
165   {
166     return activeEdges[i];
167   }
168
169   /**
170    * Removes all edges that end above the specified height.
171    *
172    * @param y the cut-off height
173    */
174   void remove(int y)
175   {
176     for (int i = 0; i < numActiveEdges; i++)
177       {
178         PolyEdge edge = activeEdges[i];
179         if (edge != null && edge.y1 < y)
180           {
181             activeEdges[i] = null;
182           }
183       }
184   }
185
186   public String toString()
187   {
188     CPStringBuilder s = new CPStringBuilder();
189     s.append("[ActiveEdges] ");
190     for (int i = 0; i < numActiveEdges; i++)
191       {
192         s.append(activeEdges[i]);
193         s.append(',');
194       }
195     return s.toString();
196   }
197 }