public PlottableVectorField(Vector2[,] vectors, double[] xs, double[] ys, string label, Color color, Colormap colormap, double scaleFactor) { //the magnitude squared is faster to compute than the magnitude double minMagnitudeSquared = vectors[0, 0].Length(); double maxMagnitudeSquared = vectors[0, 0].Length(); for (int i = 0; i < xs.Length; i++) { for (int j = 0; j < ys.Length; j++) { if (vectors[i, j].LengthSquared() > maxMagnitudeSquared) { maxMagnitudeSquared = vectors[i, j].LengthSquared(); } else if (vectors[i, j].LengthSquared() < minMagnitudeSquared) { minMagnitudeSquared = vectors[i, j].LengthSquared(); } } } double minMagnitude = Math.Sqrt(minMagnitudeSquared); double maxMagnitude = Math.Sqrt(maxMagnitudeSquared); double[,] intensities = new double[xs.Length, ys.Length]; for (int i = 0; i < xs.Length; i++) { for (int j = 0; j < ys.Length; j++) { if (colormap != null) { intensities[i, j] = (vectors[i, j].Length() - minMagnitude) / (maxMagnitude - minMagnitude); } vectors[i, j] = Vector2.Multiply(vectors[i, j], (float)(scaleFactor / (maxMagnitude * 1.2))); //This is not a true normalize } } if (colormap != null) { double[] flattenedIntensities = intensities.Cast <double>().ToArray(); arrowColors = Colormap.GetColors(flattenedIntensities, colormap); } this.vectors = vectors; this.xs = xs; this.ys = ys; this.label = label; this.color = color; this.colormap = colormap; this.scaleFactor = scaleFactor; pen = new Pen(color); pen.CustomEndCap = new AdjustableArrowCap(2, 2); }
public VectorField(Vector2[,] vectors, double[] xs, double[] ys, Colormap colormap, double scaleFactor, Color defaultColor) { double minMagnitudeSquared = vectors[0, 0].LengthSquared(); double maxMagnitudeSquared = vectors[0, 0].LengthSquared(); for (int i = 0; i < xs.Length; i++) { for (int j = 0; j < ys.Length; j++) { if (vectors[i, j].LengthSquared() > maxMagnitudeSquared) { maxMagnitudeSquared = vectors[i, j].LengthSquared(); } else if (vectors[i, j].LengthSquared() < minMagnitudeSquared) { minMagnitudeSquared = vectors[i, j].LengthSquared(); } } } double minMagnitude = Math.Sqrt(minMagnitudeSquared); double maxMagnitude = Math.Sqrt(maxMagnitudeSquared); double[,] intensities = new double[xs.Length, ys.Length]; for (int i = 0; i < xs.Length; i++) { for (int j = 0; j < ys.Length; j++) { if (colormap != null) { intensities[i, j] = (vectors[i, j].Length() - minMagnitude) / (maxMagnitude - minMagnitude); } vectors[i, j] = Vector2.Multiply(vectors[i, j], (float)(scaleFactor / (maxMagnitude * 1.2))); } } double[] flattenedIntensities = intensities.Cast <double>().ToArray(); VectorColors = colormap is null? Enumerable.Range(0, flattenedIntensities.Length).Select(x => defaultColor).ToArray() : Colormap.GetColors(flattenedIntensities, colormap); this.Vectors = vectors; this.Xs = xs; this.Ys = ys; }