protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); int offset = 50; float twip = 20f; float q2c = 2 / 3f; e.Graphics.SmoothingMode = SmoothingMode.None; if (bmpNormal != null) { e.Graphics.DrawImage(bmpNormal, offset, offset); e.Graphics.DrawImage(bmpLarge, offset + bmpNormal.Width + 50, offset); } e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; if (edges != null) { Pen p2 = new Pen(Color.FromArgb(0xFF, 0, 0x66, 0x66), 2); Pen p = new Pen(Color.FromArgb(0xFF, 0, 0, 0x66), 2); foreach (Edge edge in edges) { if (edge is CurvedEdge) { CurvedEdge ce = (CurvedEdge)edge; e.Graphics.DrawBezier(p2, ce.startX / twip + offset, ce.startY / twip + offset, ((ce.curveX - ce.startX) * q2c + ce.startX) / twip + offset, ((ce.curveY - ce.startY) * q2c + ce.startY) / twip + offset, (ce.endX - (ce.endX - ce.curveX) * q2c) / twip + offset, (ce.endY - (ce.endY - ce.curveY) * q2c) / twip + offset, ce.endX / twip + offset, ce.endY / twip + offset); } else { e.Graphics.DrawLine(p, edge.startX / twip + offset, edge.startY / twip + offset, edge.endX / twip + offset, edge.endY / twip + offset); } } } }
public static Edge[] SplitAtPeak( int p0x, int p0y, int p1x, int p1y, int p2x, int p2y, byte fill0, byte fill1, byte line) { Edge[] result = new Edge[2]; // find the place where the y's are the same, that is the peak // first height divided by total height float h0 = (float)(p0y - p1y); float h1 = (float)(p2y - p1y); float t = h0 / (h0 + h1); float u = 1f - t; float mx = u * u * p0x + 2f * u * t * p1x + t * t * p2x; float my = u * u * p0y + 2f * u * t * p1y + t * t * p2y; float m0x = (p0x - p1x) * u + p1x; float m1x = (p2x - p1x) * t + p1x; //float mx = (m1x - m0x) * t + m0x; //float m0y = (p1y - p0y) * t + p0y; //float m1y = (p1y - p2y) * u + p2y; //float my = (m1y - m0y) * t + m0y; result[0] = new CurvedEdge(p0x, p0y, (int)m0x, (int)my, (int)mx, (int)my, fill0, fill1, line); result[1] = new CurvedEdge((int)mx, (int)my, (int)m1x, (int)my, p2x, p2y, fill0, fill1, line); if (result[0].startY > (int)my || result[0].endY < (int)my) { Console.WriteLine(my); } if (result[1].startY > (int)my || result[1].endY < (int)my) { Console.WriteLine(my); } return(result); }
public static Edge[] SplitAtPeak( int p0x, int p0y, int p1x, int p1y, int p2x, int p2y, byte fill0, byte fill1, byte line) { Edge[] result = new Edge[2]; // find the place where the y's are the same, that is the peak // first height divided by total height float h0 = (float)(p0y - p1y); float h1 = (float)(p2y - p1y); float t = h0 / (h0 + h1); float u = 1f - t; float mx = u * u * p0x + 2f * u * t * p1x + t * t * p2x; float my = u * u * p0y + 2f * u * t * p1y + t * t * p2y; float m0x = (p0x - p1x) * u + p1x; float m1x = (p2x - p1x) * t + p1x; //float mx = (m1x - m0x) * t + m0x; //float m0y = (p1y - p0y) * t + p0y; //float m1y = (p1y - p2y) * u + p2y; //float my = (m1y - m0y) * t + m0y; result[0] = new CurvedEdge(p0x, p0y, (int)m0x, (int)my, (int)mx, (int)my, fill0, fill1, line); result[1] = new CurvedEdge((int)mx, (int)my, (int)m1x, (int)my, p2x, p2y, fill0, fill1, line); if (result[0].startY > (int)my || result[0].endY < (int)my) { Console.WriteLine(my); } if (result[1].startY > (int)my || result[1].endY < (int)my) { Console.WriteLine(my); } return result; }
Edge[] ParseEdges(ShapeWithStyle s, Rect bounds) { List <Edge> edges = new List <Edge>(); uint f0 = 0; uint f1 = 0; uint ln = 0; int curX = -bounds.XMin; // always 0,0 int curY = -bounds.YMin; FillStyleArray curFills = s.FillStyles; LineStyleArray curLines = s.LineStyles; int fillOffset = 0; int lineOffset = 0; foreach (IShapeRecord rec in s.ShapeRecords) { if (rec is StyleChangedRecord) { StyleChangedRecord r = (StyleChangedRecord)rec; if (r.HasNewStyles) // todo: this also sets 'grouped' elements (and therefore layers) { fillOffset = curFills.FillStyles.Count; lineOffset = curLines.LineStyles.Count; curFills.FillStyles.AddRange(r.FillStyles.FillStyles); curLines.LineStyles.AddRange(r.LineStyles.LineStyles); } if (r.HasFillStyle0) { f0 = r.FillStyle0 == 0 ? 0 : (uint)(r.FillStyle0 + fillOffset); } if (r.HasFillStyle1) { f1 = r.FillStyle1 == 0 ? 0 : (uint)(r.FillStyle1 + fillOffset); } if (r.HasLineStyle) { ln = r.LineStyle == 0 ? 0 : (uint)(r.LineStyle + lineOffset); } if (r.HasMove) { curX = r.MoveDeltaX; // this is based on the initial location, which is now 0,0 curY = r.MoveDeltaY; } } else if (rec is StraightEdgeRecord) { StraightEdgeRecord r = (StraightEdgeRecord)rec; Edge e = new Edge(curX, curY, curX + r.DeltaX, curY + r.DeltaY, (byte)f0, (byte)f1, (byte)ln); curX += r.DeltaX; curY += r.DeltaY; edges.Add(e); } else if (rec is CurvedEdgeRecord) { CurvedEdgeRecord r = (CurvedEdgeRecord)rec; int curveX = curX + r.ControlX; int curveY = curY + r.ControlY; int destX = curveX + r.AnchorX; int destY = curveY + r.AnchorY; // split nodes if curve is outside start/end Y if (curveY >= curY && curveY <= destY) { Edge e = new CurvedEdge(curX, curY, curveX, curveY, destX, destY, (byte)f0, (byte)f1, (byte)ln); edges.Add(e); } else { Edge[] split = CurvedEdge.SplitAtPeak(curX, curY, curveX, curveY, destX, destY, (byte)f0, (byte)f1, (byte)ln); edges.Add(split[0]); edges.Add(split[1]); } curX = destX; curY = destY; } } edges.Sort(Edge.MaxYComparer); return(edges.ToArray()); }
Edge[] ParseEdges(ShapeWithStyle s, Rect bounds) { List<Edge> edges = new List<Edge>(); uint f0 = 0; uint f1 = 0; uint ln = 0; int curX = -bounds.XMin; // always 0,0 int curY = -bounds.YMin; FillStyleArray curFills = s.FillStyles; LineStyleArray curLines = s.LineStyles; int fillOffset = 0; int lineOffset = 0; foreach (IShapeRecord rec in s.ShapeRecords) { if (rec is StyleChangedRecord) { StyleChangedRecord r = (StyleChangedRecord)rec; if (r.HasNewStyles) // todo: this also sets 'grouped' elements (and therefore layers) { fillOffset = curFills.FillStyles.Count; lineOffset = curLines.LineStyles.Count; curFills.FillStyles.AddRange(r.FillStyles.FillStyles); curLines.LineStyles.AddRange(r.LineStyles.LineStyles); } if (r.HasFillStyle0) { f0 = r.FillStyle0 == 0 ? 0 : (uint)(r.FillStyle0 + fillOffset); } if (r.HasFillStyle1) { f1 = r.FillStyle1 == 0 ? 0 : (uint)(r.FillStyle1 + fillOffset); } if (r.HasLineStyle) { ln = r.LineStyle == 0 ? 0 : (uint)(r.LineStyle + lineOffset); } if(r.HasMove) { curX = r.MoveDeltaX; // this is based on the initial location, which is now 0,0 curY = r.MoveDeltaY; } } else if (rec is StraightEdgeRecord) { StraightEdgeRecord r = (StraightEdgeRecord)rec; Edge e = new Edge(curX, curY, curX + r.DeltaX, curY + r.DeltaY, (byte)f0, (byte)f1, (byte)ln); curX += r.DeltaX; curY += r.DeltaY; edges.Add(e); } else if (rec is CurvedEdgeRecord) { CurvedEdgeRecord r = (CurvedEdgeRecord)rec; int curveX = curX + r.ControlX; int curveY = curY + r.ControlY; int destX = curveX + r.AnchorX; int destY = curveY + r.AnchorY; // split nodes if curve is outside start/end Y if(curveY >= curY && curveY <= destY) { Edge e = new CurvedEdge(curX, curY, curveX, curveY, destX, destY, (byte)f0, (byte)f1, (byte)ln); edges.Add(e); } else { Edge[] split = CurvedEdge.SplitAtPeak(curX, curY, curveX, curveY, destX, destY, (byte)f0, (byte)f1, (byte)ln); edges.Add(split[0]); edges.Add(split[1]); } curX = destX; curY = destY; } } edges.Sort(Edge.MaxYComparer); return edges.ToArray(); }