public void Plot(Func <double, double> function, Interv xRange, int color = -1) { double delta = xRange.Length / _option.SampleCount; var points = Enumerable.Range(0, _option.SampleCount + 1).Select(x => { double xx = xRange.Start + x * delta; return(new Point2d(xx, function(xx))); }).ToArray(); Plot(points, color); }
public void Plot(Func <double, double> function, Interv xRange, int color = -1) { double delta = xRange.Length / this.Option.SampleCount; var points = Enumerable .Range(0, this.Option.SampleCount + 1) .Select(index => { double coord = xRange.Start + index * delta; return(new Point2d(coord, function(coord))); }) .ToArray(); this.Plot(points, color); }
private static double[] GetDivStops(double delta, Interv range, double redundanceFactor = 1) { var result = new List <double>(); double redundance = (redundanceFactor - 1) / 2 * range.Length; result.Add(range.Start - redundance); double start = Math.Ceiling((range.Start - redundance) / delta) * delta; for (double t = start; t < range.End + redundance; t += delta) { result.Add(t); } result.Add(range.End + redundance); return(result.ToArray()); }
private double[] GetDivStops(int divs, Interv range) { // 算法有问题。从数学上考虑,对应一个不定方程,很复杂。 // 每个刻度点都是刻度间隔的整数倍。问题转化为求divs个连续整数。 double delta = Math.Ceiling(range.Length / divs); int nDigit = (int)Math.Log10(range.Length / divs); //delta.ToString().Length; double scale = Math.Pow(10, nDigit - 1); delta = Math.Ceiling(delta / scale) * scale; int mid = (int)Math.Floor((range.Start + range.End) / 2 / delta); int start = mid - divs / 2; return(Enumerable.Range(start, divs + 1).Select(x => x * delta).ToArray()); }
public void Plot(IEnumerable <Point2d> points, int color = -1) { this.Curves.Add(new Tuple <IEnumerable <Point2d>, int>(points.OrderBy(point => point.X).ToArray(), color)); var xRange = new Interv(points.Min(point => point.X), points.Max(point => point.X)); var yRange = new Interv(points.Min(point => point.Y), points.Max(point => point.Y)); if (this.XRange == null) { this.XRange = xRange; } else { this.XRange = this.XRange.AddInterval(xRange); } if (this.YRange == null) { this.YRange = yRange; } else { this.YRange = this.YRange.AddInterval(yRange); } }
public void Plot(IEnumerable <Point2d> points, int color = -1) { _curves.Add(points.OrderBy(x => x.X).ToArray(), color); Interv xRange = new Interv(points.Min(x => x.X), points.Max(x => x.X)); Interv yRange = new Interv(points.Min(x => x.Y), points.Max(x => x.Y)); if (_xRange == null) { _xRange = xRange; } else { _xRange = _xRange.AddInterval(xRange); } if (_yRange == null) { _yRange = yRange; } else { _yRange = _yRange.AddInterval(yRange); } }
public ObjectId GetGraphBlock() { if (_xRange == null || _yRange == null) { throw new System.Exception("未指定绘制内容"); } // 控制最小范围 if (_xRange.Length < Consts.Epsilon) { _xRange = new Interv(_xRange.Start - Consts.Epsilon, _xRange.End + Consts.Epsilon); } if (_yRange.Length < Consts.Epsilon) { _yRange = new Interv(_yRange.Start - Consts.Epsilon, _yRange.End + Consts.Epsilon); } // 获取刻度值 double[] xStops = GetDivStops(_option.xDelta, _xRange, _option.xRedundanceFactor); double[] yStops = GetDivStops(_option.yDelta, _yRange, _option.yRedundanceFactor); // 刻度网格 List <ObjectId> gridLines = new List <ObjectId>(); foreach (var xStop in xStops) { gridLines.Add(Draw.Line(new Point3d(xStop, RealRatio * yStops.First(), 0), new Point3d(xStop, RealRatio * yStops.Last(), 0))); } foreach (var yStop in yStops) { gridLines.Add(Draw.Line(new Point3d(xStops.First(), RealRatio * yStop, 0), new Point3d(xStops.Last(), RealRatio * yStop, 0))); } gridLines.QForEach <Entity>(x => x.ColorIndex = _option.GridColor); _entIds.AddRange(gridLines); // 刻度标记 List <ObjectId> labels = new List <ObjectId>(); double txtHeight = _xRange.Length / 50; foreach (var xStop in xStops) { labels.Add(Draw.MText(xStop.ToString("0.###"), txtHeight, new Point3d(xStop, RealRatio * yStops.First() - 2 * txtHeight, 0), 0, true)); } foreach (var yStop in yStops) { labels.Add(Draw.MText(yStop.ToString("0.###"), txtHeight, new Point3d(xStops.First() - 2 * txtHeight, RealRatio * yStop, 0), 0, true)); } labels.QForEach <Entity>(x => x.ColorIndex = _option.LabelColor); _entIds.AddRange(labels); // 曲线 foreach (var curve in _curves) { ObjectId plineId = Draw.Pline(curve.Item1.OrderBy(x => x.X).Select(x => new Point3d(x.X, RealRatio * x.Y, 0))); int color1 = curve.Item2 == -1 ? _option.CurveColor : curve.Item2; plineId.QOpenForWrite <Entity>(x => x.ColorIndex = color1); _entIds.Add(plineId); } // 返回块 ObjectId result = Draw.Block(_entIds, "tjGraph" + LogManager.GetTimeBasedName(), _entIds.GetCenter()); _entIds.QForEach(x => x.Erase()); _entIds.Clear(); return(result); }
public GraphPlotter(GraphOption opt) { _option = opt; _xRange = opt.xRange; _yRange = opt.yRange; }