三角形三个顶点的颜色分别为红绿蓝,使用有效边表算法填充三角形Java
时间: 2024-05-16 11:14:11 浏览: 12
以下是使用有效边表算法填充三角形的Java代码示例:
```
import java.awt.Color;
import java.awt.Graphics;
import java.util.Arrays;
public class TriangleFill {
public static void main(String[] args) {
int[] xPoints = {100, 200, 150};
int[] yPoints = {100, 100, 200};
Color[] colors = {Color.RED, Color.GREEN, Color.BLUE};
fillTriangle(xPoints, yPoints, colors);
}
public static void fillTriangle(int[] xPoints, int[] yPoints, Color[] colors) {
if (xPoints.length != 3 || yPoints.length != 3 || colors.length != 3) {
throw new IllegalArgumentException("Invalid triangle parameters");
}
int[] sortedY = Arrays.copyOf(yPoints, 3);
Arrays.sort(sortedY);
int yMin = sortedY[0];
int yMax = sortedY[2];
int[] xMin = new int[3];
int[] xMax = new int[3];
Color[] colorMin = new Color[3];
Color[] colorMax = new Color[3];
for (int i = 0; i < 3; i++) {
if (yPoints[i] == yMin) {
xMin[i] = xPoints[i];
colorMin[i] = colors[i];
} else if (yPoints[i] == yMax) {
xMax[i] = xPoints[i];
colorMax[i] = colors[i];
}
}
for (int i = 0; i < 3; i++) {
if (yPoints[i] != yMin && yPoints[i] != yMax) {
int j = (i + 1) % 3;
int k = 3 - i - j;
xMin[k] = xPoints[i] + (yMin - yPoints[i]) * (xPoints[j] - xPoints[i]) / (yPoints[j] - yPoints[i]);
colorMin[k] = interpolateColor(colors[i], colors[j], (yMin - yPoints[i]) / (float) (yPoints[j] - yPoints[i]));
xMax[k] = xPoints[i] + (yMax - yPoints[i]) * (xPoints[j] - xPoints[i]) / (yPoints[j] - yPoints[i]);
colorMax[k] = interpolateColor(colors[i], colors[j], (yMax - yPoints[i]) / (float) (yPoints[j] - yPoints[i]));
}
}
int[] activeX = new int[3];
Color[] activeColor = new Color[3];
for (int y = yMin; y <= yMax; y++) {
int activeCount = 0;
for (int i = 0; i < 3; i++) {
if (y >= yMin && y <= yMax) {
if (y == yMin) {
if (xMin[i] < xMax[i]) {
activeX[activeCount] = xMin[i];
activeColor[activeCount] = colorMin[i];
activeCount++;
activeX[activeCount] = xMax[i];
activeColor[activeCount] = colorMax[i];
activeCount++;
} else if (xMin[i] > xMax[i]) {
activeX[activeCount] = xMax[i];
activeColor[activeCount] = colorMax[i];
activeCount++;
activeX[activeCount] = xMin[i];
activeColor[activeCount] = colorMin[i];
activeCount++;
}
} else {
if (y >= yPoints[i] && y <= yPoints[(i + 1) % 3]) {
int x = xPoints[i] + (y - yPoints[i]) * (xPoints[(i + 1) % 3] - xPoints[i]) / (yPoints[(i + 1) % 3] - yPoints[i]);
Color color = interpolateColor(colors[i], colors[(i + 1) % 3], (y - yPoints[i]) / (float) (yPoints[(i + 1) % 3] - yPoints[i]));
activeX[activeCount] = x;
activeColor[activeCount] = color;
activeCount++;
}
}
}
}
Arrays.sort(activeX, 0, activeCount);
for (int i = 0; i < activeCount; i += 2) {
int x1 = activeX[i];
int x2 = activeX[i + 1];
Color color1 = activeColor[i];
Color color2 = activeColor[i + 1];
for (int x = x1; x <= x2; x++) {
float t = (x - x1) / (float) (x2 - x1);
Color color = interpolateColor(color1, color2, t);
plotPixel(x, y, color);
}
}
for (int i = 0; i < activeCount; i++) {
activeX[i] += (int) (1 / (yMax - yMin + 1f) * (xMax[i] - xMin[i]));
}
}
}
public static Color interpolateColor(Color color1, Color color2, float t) {
int r = (int) (color1.getRed() + (color2.getRed() - color1.getRed()) * t);
int g = (int) (color1.getGreen() + (color2.getGreen() - color1.getGreen()) * t);
int b = (int) (color1.getBlue() + (color2.getBlue() - color1.getBlue()) * t);
return new Color(r, g, b);
}
public static void plotPixel(int x, int y, Color color) {
Graphics g = getGraphics(); // 获取图形对象
g.setColor(color);
g.drawLine(x, y, x, y);
}
public static Graphics getGraphics() {
// 返回画布对象
}
}
```
其中,`fillTriangle` 方法接受三个数组作为参数:`xPoints` 是三角形三个顶点的 x 坐标,`yPoints` 是三角形三个顶点的 y 坐标,`colors` 是三个顶点的颜色。该方法使用有效边表算法填充三角形,实现了颜色渐变。
`interpolateColor` 方法接受两个颜色和一个比例值 t,返回两个颜色的 t 比例混合色。
`plotPixel` 方法在画布上绘制一个像素点,使用 `getGraphics` 方法获取画布对象。
这里的 `getGraphics` 方法需要根据具体情况进行实现,例如在 Swing 界面中可以使用 `getGraphics` 方法获取 `JPanel` 对象的图形对象。