4d1e777ccf5ef5be89f64d9090db444bcad5cbf2
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
;
42 * A collection of active edges for scanline conversion.
44 final class ActiveEdges
48 * The active edges. This can contain null values at arbirary locations.
49 * The method #sort() packs this together.
51 private PolyEdge
[] activeEdges
;
54 * The actual number of active edges. The array can be bigger than this
57 private int numActiveEdges
;
60 * Creates a new ActiveEdges object.
64 activeEdges
= new PolyEdge
[8];
69 * Clears out all active edges. This is cheap as it simply resets the
70 * counter to 0. It does not release all references to PolyEdge instances.
78 * Adds the specified edge to the list of active edges. This does not yet
79 * sort the edges and therefore does destroy any order of the list.
81 * @param edge the edge to add
83 void add(PolyEdge edge
)
85 // Grow array when necessary.
86 int oldSize
= activeEdges
.length
;
87 if (numActiveEdges
>= oldSize
)
89 int newSize
= oldSize
+ oldSize
/ 4 + 1;
90 PolyEdge
[] newEdges
= new PolyEdge
[newSize
];
91 System
.arraycopy(activeEdges
, 0, newEdges
, 0, oldSize
);
92 activeEdges
= newEdges
;
94 activeEdges
[numActiveEdges
] = edge
;
99 * Intersects all active edges, sorts them according to their intersection
100 * points and packs the array to remove unneeded edges. This does also
101 * remove any edges that do not intersect the scanline (i.e. they end above
104 * @param y the scanline height
106 void intersectSortAndPack(int n
, int y
)
108 // Intersect and pack in one go.
111 for (int i
= 0; i
< numActiveEdges
; i
++)
113 PolyEdge edge
= activeEdges
[i
];
114 // Clear out edge that ends above the scanline.
115 if (edge
!= null && edge
.y1
>= y
)
117 assert edge
.y1
>= y
&& edge
.y0
<= y
: "edge must cross scanline";
118 edge
.intersect(n
, y
);
119 activeEdges
[last
] = edge
;
122 // Bubble up the added edge.
123 for (int j
= last
- 1; j
> 0; j
--)
125 if (activeEdges
[j
].xIntersection
126 < activeEdges
[j
- 1].xIntersection
)
128 tmp
= activeEdges
[j
];
129 activeEdges
[j
] = activeEdges
[j
- 1];
130 activeEdges
[j
- 1] = tmp
;
134 // The beginning of the list is already sorted.
140 numActiveEdges
= last
;
145 * Returns the number of active edges. This is only reliable after a
146 * call to {@link #intersectSortAndPack(int, int)}.
148 * @return the number of active edges
150 int getNumActiveEdges()
152 return numActiveEdges
;
156 * Returns the active edge at the position <code>i</code>.
160 * @return the active edge at the specified index
162 PolyEdge
getActiveEdge(int i
)
164 return activeEdges
[i
];
168 * Removes all edges that end above the specified height.
170 * @param y the cut-off height
174 for (int i
= 0; i
< numActiveEdges
; i
++)
176 PolyEdge edge
= activeEdges
[i
];
177 if (edge
!= null && edge
.y1
< y
)
179 activeEdges
[i
] = null;
184 public String
toString()
186 StringBuilder s
= new StringBuilder();
187 s
.append("[ActiveEdges] ");
188 for (int i
= 0; i
< numActiveEdges
; i
++)
190 s
.append(activeEdges
[i
]);