/// <summary> /// 根据每个后续的概率随机选择一个后续,并返回后续的索引 /// </summary> /// <returns>随机选择的后续索引值</returns> private int RandomlySelectResultList() { int result = 0; int countOfSuccessor = m_listSuccessor.Count; float[] probabilityArray = new float[countOfSuccessor]; for (int i = 0; i < countOfSuccessor; i++) { if (i == 0) { probabilityArray[0] = m_listSuccessor[0].Probability.Value; } else { probabilityArray[i] = m_listSuccessor[i].Probability.Value + probabilityArray[i - 1]; } } //根据随机产生的数字判断使用哪个结果链表 double randomNum = RandomNumer.Double() * probabilityArray[countOfSuccessor - 1]; for (int i = 0; i < countOfSuccessor; i++) { if (randomNum < probabilityArray[i]) { result = i; break; } } return(result); }
/// <summary> /// 根据Y坐标的值,随机(若有多个边界点的话)获取其边界点的X坐标 /// </summary> private static int GetXCoordinateOfBoundaryPointRandomly(int pointY, Texture2D referenceTex) { List <int> boundaryOfX = new List <int>(); bool isTransparent_current; //当前像素是否透明 bool isTransparent_prev = true; //前一个像素是否透明 for (int x = 0; x < referenceTex.width; x++) { isTransparent_current = referenceTex.GetPixel(x, pointY).a == 0; //判断是否透明 if (isTransparent_current != isTransparent_prev) //相邻两个像素一个为透明一个为非透明,即为边界 { boundaryOfX.Add(x); } isTransparent_prev = isTransparent_current; } isTransparent_current = true; //当前像素超过纹理边界,即为透明 if (isTransparent_current != isTransparent_prev) //判断纹理一边是否为边界 { boundaryOfX.Add(referenceTex.width - 1); } return (boundaryOfX.Count == 0 ? /*若为0,则说明该行均为透明像素,即无边界*/ RandomNumer.ParabolaVariable_Int(0, referenceTex.width - 1) : boundaryOfX[RandomNumer.Int(0, boundaryOfX.Count)]); }
public double InsectSim(double insectIntake) { /* * 当该叶片的最低被啃食比例小于细胞纹理的最低被啃食比例时 * 说明该比例无或者存在一定的问题 * 因此将该比例设置为细胞纹理的最低被啃食比例 */ if (LimitRatio < CelluarTex.LimitRatio) { LimitRatio = CelluarTex.LimitRatio; /* * 调整最低被啃食比例 * 减少重复感 */ LimitRatio += (0.8 - LimitRatio) * RandomNumer.Double(); } /* * 叶片当前最大可被进食量 * 用于计算当前实际被进食量 */ double maxInsectIntake = (InsectedRatio - LimitRatio) * LeafArea_Uninsected; /* * 若该叶片已经老去 * 则不会被并病虫啃食 * 若还未老去,则根据叶片最大被进食量计算实际被进食量 * 当最大可被进食量不大于0时,说明无可被进食部分,因此实际被进食量为0 * 当最大可被进食量大于当前病虫进食量,则说明该叶片满足病虫进食需求,因此实际进食量为病虫进食量 * 当最大可被进食量大于0并且小于当前病虫进食量,则说明该叶片有被进食,但无法满足病虫进食需求 * 因此最大可被进食量均被进食,故实际进食量为最大可被进食量 */ double actualInsectIntake = Age >= MaizeParams.LEAF_MAX_DEVELOPMENT_AGE ? 0 : maxInsectIntake <= 0 ? 0 : maxInsectIntake >= insectIntake ? insectIntake : maxInsectIntake; //根据实际进食量计算叶片的面积 LeafArea -= actualInsectIntake; int threshold; //设置纹理 GameObjectOperation.SetTexture(Belong, GetWormholeTex(actualInsectIntake, out threshold)); //设置阈值 GameObjectOperation.SetThreshold(Belong, threshold); //返回剩余的病虫进食量 return(insectIntake - actualInsectIntake); }
private static Vector2[] RandomPoints(int count, int width, int height, Texture2D referenceTex = null) { //随机选取特征点 int[] featurePointY = RandomNumer.Ints(count, 0, height - 1, RandomNumer.DistributionFunction.Uniform); int[] featurePointX = referenceTex == null?RandomNumer.Ints(count, 0, width - 1, RandomNumer.DistributionFunction.Parabola) : GetXCoordinatesOfBoundaryPointRandomly(featurePointY, referenceTex); Vector2[] Points = new Vector2[count]; for (int i = 0; i < count; i++) { Points[i] = new Vector2(featurePointX[i], featurePointY[i]); } return(Points); }
/// <summary> /// 根据含有形参的Term以及各实参计算出最终的Term并返回。 /// 如 F(length+1) 为含有形参的Term,length 为 1,则返回F(2) /// </summary> /// <param name="scrTerm">含有形参的Term,如F(length+1)</param> /// <param name="table">包含各实参的DataTable类</param> /// <returns>返回计算完成后的Term</returns> private LTerm GetResultTerm(LTerm scrTerm, DataTable table) { if (scrTerm == null) { return(null); } LTerm destTerm = new LTerm(); destTerm.Symbol = scrTerm.Symbol; //将符号赋值给新的term table.Rows[table.Rows.Count - 1]["RANDOM"] = RandomNumer.Single(); //写入随机数 for (int i = 0; i < scrTerm.Params.Count; i++) { table.Columns["expression"].Expression = scrTerm.Params[i]; //根据参数中的公式进行赋值,如F(length + 1)中的length + 1 destTerm.Params.Add(table.Rows[0][table.Columns.Count - 1].ToString()); //根据实参、公式计算出结果,并赋值给输出的term中的参数列表 } return(destTerm); }
public static Texture2D Create(int width, int height, int count_CenterPoints, int count_PointsPerGroup = 10, float radius = 15, Texture2D referenceTex = null, Texture2D barrierTex = null) { Texture2D texture = new Texture2D(width, height); PixelInfo[] PixelInfoes = new PixelInfo[width * height]; //用于存储各像素点的信息(最近特征点和与其的距离) Vector2[] CenterPoints = null; if (centerPoints == null) { CenterPoints = RandomPoints(count_CenterPoints, width, height, referenceTex); //随机选择特征点 } else { CenterPoints = centerPoints; } centerPoints = CenterPoints; Vector2[] FeaturePoints = new Vector2[CenterPoints.Length * count_PointsPerGroup]; PixelInfo[] FeaturePointInfos = new PixelInfo[FeaturePoints.Length]; PixelInfo[] FeaturePointInfos_FeaturePoint = new PixelInfo[FeaturePoints.Length]; for (int i = 0; i < CenterPoints.Length; i++) { for (int j = 0; j < count_PointsPerGroup; j++) { Vector2 FeaturePoint = CenterPoints[i] + Random.insideUnitCircle * radius; while ((FeaturePoint.x < 0 || FeaturePoint.x > width - 1 || FeaturePoint.y < 0 || FeaturePoint.y > height - 1)) { FeaturePoint = CenterPoints[i] + Random.insideUnitCircle * radius; } FeaturePoint.x = (int)FeaturePoint.x; FeaturePoint.y = (int)FeaturePoint.y; FeaturePoints[i * count_PointsPerGroup + j] = FeaturePoint; } } FeaturePointInfos = ComputeDistance_FC(FeaturePoints, CenterPoints); FeaturePointInfos_FeaturePoint = ComputeDistance_FF(FeaturePoints, FeaturePointInfos); SortFeaturePoints(ref FeaturePoints, ref FeaturePointInfos, ref FeaturePointInfos_FeaturePoint); /* * 计算每个像素与其最近特征点之间的距离 */ PixelInfoes = ComputeDistanceFast(width, height, CenterPoints, FeaturePoints, FeaturePointInfos); /* * 计算最长的距离 * 用于实现颜色变化 */ float MaxDistance = GetMaxDistance(PixelInfoes); /* * 为实现特征点有先后顺序,对中心点的颜色进行干扰 */ for (int i = 0; i < CenterPoints.Length; i++) { texture.SetPixel((int)CenterPoints[i].x, (int)CenterPoints[i].y, DEC2Color((int)(MaxDistance * RandomNumer.Single() * 0.5f * SCALE))); } /* * 赋予每个特征点颜色 */ for (int i = 0; i < FeaturePoints.Length; i++) { Color FeaturePixel = texture.GetPixel((int)FeaturePointInfos_FeaturePoint[i].X_NearestPoint, (int)FeaturePointInfos_FeaturePoint[i].Y_NearestPoint); texture.SetPixel((int)FeaturePoints[i].x, (int)FeaturePoints[i].y, GetColor(FeaturePointInfos[i].Distance, MaxDistance, FeaturePixel)); } /* * 赋予每个像素点颜色 * 根据位置之间的关系,从近到远(黑到白) */ for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Color FeaturePixel = texture.GetPixel((int)PixelInfoes[i * height + j].X_NearestPoint, (int)PixelInfoes[i * height + j].Y_NearestPoint); //获取最近特征点的颜色 Color pixel = GetColor(PixelInfoes[i * height + j].Distance, MaxDistance, FeaturePixel); pixel.a = 1.0f; texture.SetPixel(i, j, pixel); } } //添加障碍物像素 if (barrierTex != null) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (barrierTex.GetPixel(x, y).a != 0) { texture.SetPixel(x, y, GetTransparentColor(texture.GetPixel(x, y))); } } } } //添加轮廓像素 if (referenceTex != null) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (referenceTex.GetPixel(x, y).a == 0) { texture.SetPixel(x, y, GetTransparentColor(texture.GetPixel(x, y))); } } } } texture.Apply(); return(texture); }