private static IEnumerable <HpglLine> OptimizeDistance(IEnumerable <HpglLine> lines) { var newList = new List <HpglLine> { lines.First() }; var list = new List <HpglLine>(); list.AddRange(lines.Skip(1)); while (list.Any()) { Point3D fromPt = newList.Last().Commands.Last().PointTo; double maxDist = double.MaxValue; HpglLine minDistLine = null; foreach (var l in list) { Point3D pt = l.Commands.First().PointFrom; double dx = (pt.X ?? 0.0) - (fromPt.X ?? 0.0); double dy = (pt.Y ?? 0.0) - (fromPt.Y ?? 0.0); double dist = Math.Sqrt(dx * dx + dy * dy); if (dist < maxDist) { maxDist = dist; minDistLine = l; } } list.Remove(minDistLine); newList.Add(minDistLine); } return(newList); }
public IList <HpglCommand> ConvertInvert(IList <HpglCommand> list) { // split int startIdx = 0; var lineList = new List <HpglLine>(); IEnumerable <HpglCommand> postCommands = null; IEnumerable <HpglCommand> preCommands = list.Skip(startIdx).TakeWhile(e => !e.IsPenCommand); startIdx += preCommands.Count(); while (startIdx < list.Count) { HpglLine line = GetHpglLine(list, ref startIdx); if (startIdx >= list.Count && !line.Commands.Any()) { postCommands = line.PreCommands; } else { lineList.Add(line); } } // rearrange var lines = OrderLines(lineList); // rebuild list var newList = new List <HpglCommand>(); newList.AddRange(preCommands); foreach (var line in lines) { newList.AddRange(line.PreCommands); newList.AddRange(line.Commands); newList.AddRange(line.PostCommands); } if (postCommands != null) { newList.AddRange(postCommands); } return(newList); }
private static HpglLine GetHpglLine(IList <HpglCommand> list, ref int startIdx) { var line = new HpglLine { PreCommands = list.Skip(startIdx).TakeWhile(e => !e.IsPenDownCommand) }; startIdx += line.PreCommands.Count(); line.Commands = list.Skip(startIdx).TakeWhile(e => e.IsPenDownCommand); startIdx += line.Commands.Count(); line.PostCommands = list.Skip(startIdx).TakeWhile(e => false); // always empty startIdx += line.PostCommands.Count(); return(line); }
public bool IsEmbedded(HpglLine to) { if (ReferenceEquals(this, to)) { return(false); } bool isRectangleEmbedded = MaxX >= to.MaxX && MinX <= to.MinX && MaxY >= to.MaxY && MinY <= to.MinY; if (!isRectangleEmbedded) { return(false); } return(IsEmbeddedEx(to)); }
private IEnumerable <HpglLine> OffsetLine(double offset, HpglLine line) { var newlines = new List <HpglLine> { line }; var co = new ClipperOffset(); var solution = new List <List <IntPoint> >(); var solution2 = new List <List <IntPoint> >(); solution.Add(line.Commands.Select(x => new IntPoint(_scale * x.PointFrom.X0, _scale * x.PointFrom.Y0)).ToList()); co.AddPaths(solution, JoinType.jtRound, EndType.etClosedPolygon); co.Execute(ref solution2, offset); var existingLine = line; foreach (var polygon in solution2) { var newCmds = new List <HpglCommand>(); HpglCommand last = null; foreach (var pt in polygon) { var from = new Point3D { X = pt.X / _scale, Y = pt.Y / _scale }; var hpgl = new HpglCommand { PointFrom = from, CommandType = HpglCommand.HpglCommandType.PenDown }; newCmds.Add(hpgl); if (last != null) { last.PointTo = @from; } last = hpgl; } last.PointTo = newCmds.First().PointFrom; if (existingLine == null) { // add new line existingLine = new HpglLine { PreCommands = new List <HpglCommand> { new HpglCommand { CommandType = HpglCommand.HpglCommandType.PenUp } }, PostCommands = new List <HpglCommand>(), ParentLine = line.ParentLine }; newlines.Add(existingLine); } existingLine.Commands = newCmds; existingLine.PreCommands.Last(l => l.IsPenCommand).PointTo = newCmds.First().PointFrom; existingLine = null; } return(newlines); }
public bool IsEmbeddedEx(HpglLine to) { return(_polygon.ArePointsInPolygon(to._polygon.Points)); }