[libbluray-devel] BDGraphics: implement fillPolygon()
Ian Curtis
git at videolan.org
Tue Mar 25 12:39:48 CET 2014
libbluray | branch: master | Ian Curtis <i.curtis at gmail.com> | Tue Mar 25 13:26:34 2014 +0200| [d79c6c8eb37d15a40091e331e593db92642c0a58] | committer: hpi1
BDGraphics: implement fillPolygon()
> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=d79c6c8eb37d15a40091e331e593db92642c0a58
---
.../bdj/java-j2se/java/awt/BDGraphics.java | 66 +++++++++++++++++-
src/libbluray/bdj/java/java/awt/PolyEdge.java | 73 ++++++++++++++++++++
2 files changed, 138 insertions(+), 1 deletion(-)
diff --git a/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java b/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java
index 49c274e..bc1016b 100644
--- a/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java
+++ b/src/libbluray/bdj/java-j2se/java/awt/BDGraphics.java
@@ -21,7 +21,10 @@ package java.awt;
import java.lang.reflect.Field;
import java.text.AttributedCharacterIterator;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.ImageConsumer;
@@ -643,7 +646,68 @@ class BDGraphics extends Graphics2D implements ConstrainableGraphics {
/** Fills a polygon with the current fill mask */
public void fillPolygon(int xPoints[], int yPoints[], int nPoints) {
- logger.unimplemented("fillPolygon");
+
+ int minY = Integer.MAX_VALUE;
+ int maxY = Integer.MIN_VALUE;
+ int colour = foreground.getRGB();
+
+ if (nPoints < 3) {
+ return;
+ }
+
+ for (int i = 0; i < nPoints; i++) {
+ if (yPoints[i] > maxY) {
+ maxY = yPoints[i];
+ }
+ if (yPoints[i] < minY) {
+ minY = yPoints[i];
+ }
+ }
+
+ // check the last point to see if its the same as the first
+ if (xPoints[0] == xPoints[nPoints - 1] && yPoints[0] == yPoints[nPoints - 1]) {
+ nPoints--;
+ }
+
+ PolyEdge[] polyEdges = new PolyEdge[nPoints];
+
+ for (int i = 0; i < nPoints - 1; i++) {
+ polyEdges[i] = new PolyEdge(xPoints[i], yPoints[i], xPoints[i + 1], yPoints[i + 1]);
+ }
+
+ // add the last one
+ polyEdges[nPoints - 1] = new PolyEdge(xPoints[nPoints - 1], yPoints[nPoints - 1], xPoints[0], yPoints[0]);
+ ArrayList xList = new ArrayList();
+ for (int i = minY; i <= maxY; i++) {
+ for (int j = 0; j < nPoints; j++) {
+ if (polyEdges[j].intersects(i)) {
+ int x = polyEdges[j].intersectionX(i);
+ xList.add(new Integer(x));
+ }
+ }
+
+ // probably a better way of doing this (removing duplicates);
+ HashSet hs = new HashSet();
+ hs.addAll(xList);
+ xList.clear();
+ xList.addAll(hs);
+
+ if (xList.size() % 2 > 0) {
+ xList.clear();
+ continue; // this should be impossible unless the poly is open somewhere
+ }
+
+ Collections.sort(xList);
+
+ for (int j = 0; j < xList.size(); j +=2 ) {
+ int x1 = ((Integer)xList.get(j)).intValue();
+ int x2 = ((Integer)xList.get(j + 1)).intValue();
+
+ drawSpan(x1, i, x2 - x1, colour);
+ }
+
+ xList.clear();
+ }
}
/** Draws an oval to fit in the given rectangle */
diff --git a/src/libbluray/bdj/java/java/awt/PolyEdge.java b/src/libbluray/bdj/java/java/awt/PolyEdge.java
new file mode 100644
index 0000000..f83558d
--- /dev/null
+++ b/src/libbluray/bdj/java/java/awt/PolyEdge.java
@@ -0,0 +1,73 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2014 libbluray
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package java.awt;
+
+class PolyEdge {
+
+ private int x1;
+ private int y1;
+ private int x2;
+ private int y2;
+ private float m;
+ private float c;
+ private boolean vertical;
+
+ PolyEdge(int x1, int y1, int x2, int y2) {
+
+ // sort lowest to highest
+ if (y2 < y1) {
+ int swap;
+ swap = x1; x1 = x2; x2 = swap;
+ swap = y1; y1 = y2; y2 = swap;
+ }
+
+ this.x1 = x1;
+ this.y1 = y1;
+ this.x2 = x2;
+ this.y2 = y2;
+
+ if (x1 == x2) {
+ vertical = true;
+ m = 0;
+ } else {
+ m = (float)(y2 - y1) / (float)(x2 - x1);
+ c = (-x1 * m) + y1;
+ vertical = false;
+ }
+ }
+
+ public boolean intersects(int y) {
+
+ if (y <= y2 && y >= y1 && y1 != y2) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public int intersectionX(int y) {
+
+ if (vertical) {
+ return x1;
+ }
+
+ return (int)((y - c) / m);
+ }
+}
More information about the libbluray-devel
mailing list