我需要一个例程来获得随机点的概率更高的随机2D矢量。斑点也可以是矩形。请帮帮我。
该功能必须看起来像这些
public Vector GetRandomHotSpotVectors(Vector minPosition, Vector maxPostion, int spotsCount, float minSpotSize, float maxSpotSize, int VectorCount);
var vectors = GetRandomHotSpotVectors(new Vector(0,0), new Vector(200,200), 2, 20, 30, 44);
答案 0 :(得分:0)
public static readonly Random Rand = new Random();
public static float NextFloat(float min, float max)
{
return min + ((float)Rand.NextDouble() * (max - min));
}
public static Vector4[] GetHotSpotVectors(
Vector2 leftTop,
Vector2 rightBottom,
int spotsCount,
float minSpotSize,
float maxSpotSize,
float minVectorObjSize,
float maxVectorObjSize,
int vectorCount)
{
var vectors = new Vector4[vectorCount];
var vectorsProceeded = 0;
for (var i = 0; i != spotsCount; i++)
{
var spotSize = NextFloat(minSpotSize, maxSpotSize);
var left = NextFloat(leftTop.X, rightBottom.X - spotSize);
var top = NextFloat(leftTop.Y, rightBottom.Y - spotSize);
var right = left + spotSize;
var bottom = top + spotSize;
var currSpotCount = vectorCount / spotsCount;
for (var j = vectorsProceeded; j < vectorsProceeded + currSpotCount; j++)
{
var vecObjSize = NextFloat(minSpotSize, maxVectorObjSize);
vectors[j] = new Vector4(
NextFloat(left, right),
NextFloat(top, bottom),
vecObjSize,
vecObjSize);
}
vectorsProceeded += currSpotCount;
}
return vectors;
}