但这对我来说还不够,我需要超过1。 因此,当我尝试制作3个渐变,并且它们相互碰撞时,结果如下:
很显然,我在那里合并图层的方式出了问题。
这是源代码。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
namespace Gradients {
static class Gradient {
public static Bitmap GetGradient() {
Random random = new Random();
Bitmap image = new Bitmap(300, 300);
int size = 300;
double[,] colorValue = new double[300, 300];
int amount = 3;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
colorValue[x, y] = 1;
}
}
for (int i = 0; i < amount; i++) {
int centerX = random.Next(size / 10 - 1, size - (size / 10 - 1));
int centerY = random.Next(size / 10 - 1, size - (size / 10 - 1));
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
double distanceX = (centerX - x) * (centerX - x);
double distanceY = (centerY - y) * (centerY - y);
double distanceToCenter = Math.Sqrt(distanceX + distanceY) * 5;
distanceToCenter /= size;
if (distanceToCenter > 1)
distanceToCenter = 1;
if (distanceToCenter < 0)
distanceToCenter = 0;
double number = 1 - distanceToCenter; // inverting number range from 1-0 to 0-1
colorValue[x, y] -= number;
if (colorValue[x, y] < 0)
colorValue[x,y] = 0;
}
}
}
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
int cValue = Convert.ToInt32(colorValue[x, y] * 255);
image.SetPixel(x, y, Color.FromArgb(cValue, 0, 0, 0));
}
}
return image;
}
// euclidean distance
private static double getDistance(int x1, int y1, int x2, int y2) {
return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
}
}