public double minYdistance(PointF p, ref bool reversed, ref bool flipx, ref bool flipy) { reversed = false; flipx = false; flipy = false; double dmin = double.MaxValue; int idx = 0; /* double dd1 = new LineF(p, start).length; double dd2 = new LineF(p, end).length; if (dd2 < dd1) { reversed = true; return dd2; } return dd1; */ // we want to find the shortest distance from point // p, even if that means (1) reversing the order of // points; (2) swapping horizontally; or (3) swapping // vertically. these can stack, so this is complicated? // total test cases = 2 * 2 * 2 = 8? double[] d = new double[8]; for (int i = 0; i < 8; i++) d[i] = double.MaxValue; if (start.X == p.X) d[0] = new LineF(p, start).length; if (FlipX().start.X == p.X) d[1] = new LineF(p, FlipX().start).length; if (FlipY().start.X == p.X) d[2] = new LineF(p, FlipY().start).length; if (FlipY().FlipX().start.X == p.X) d[3] = new LineF(p, FlipY().FlipX().start).length; if (end.X == p.X) d[4] = new LineF(p, end).length; if (FlipX().end.X == p.X) d[5] = new LineF(p, FlipX().end).length; if (FlipY().end.X == p.X) d[6] = new LineF(p, FlipY().end).length; if (FlipY().FlipX().end.X == p.X) d[7] = new LineF(p, FlipY().FlipX().end).length; for (int i = 0; i < 8; i++) { if (d[i] < dmin) { idx = i; dmin = d[i]; } } switch (idx) { case 0: reversed = false; flipx = false; flipy = false; break; case 1: reversed = false; flipx = true; flipy = false; break; case 2: reversed = false; flipx = false; flipy = true; break; case 3: reversed = false; flipx = true; flipy = true; break; case 4: reversed = true; flipx = false; flipy = false; break; case 5: reversed = true; flipx = true; flipy = false; break; case 6: reversed = true; flipx = false; flipy = true; break; case 7: reversed = true; flipx = true; flipy = true; break; } return dmin; }
public LineF Clone() { LineF line = new LineF( start ,end ); line.width = width; return line; }
public LineF Reversed() { LineF lf = new LineF(end, start); return lf; }
void readApertures(List<string> lines, out List<Aperture> placedAps, out List<LineF> lineList, ref double maxx, ref double maxy, ref double minx, ref double miny) { Regex reXYLine = new Regex(@"^X(\d+)Y(\d+)D(\d\d)\*$"); Regex reFSLA = new Regex(@"%FSLAX(\d)(\d)Y(\d)(\d)\*%"); Regex reApDef = new Regex(@"%ADD(\d+)R,([\d\.]+)X([\d\.]+)\*%"); Regex reCircleDef = new Regex(@"%ADD(\d+)C,([\d\.]+)\*%"); Regex reOctoDef = new Regex(@"%ADD(\d+)OC8,([\d\.]+)\*%"); Regex reApGroup = new Regex(@"^D(\d+)\*$"); Dictionary<int, Aperture> apertures = new Dictionary<int, Aperture>(); placedAps = new List<Aperture>(); lineList = new List<LineF>(); int currentAperture = -1; PointF pstart = new PointF(0, 0); PointF pend = new PointF(0, 0); double x, y; int d; int decimal_x = 4, decimal_y = 4; double mul_x = Math.Pow(10, decimal_x); double mul_y = Math.Pow(10, decimal_y); foreach (string line in lines) { try { // format is 2.4 (CHECK?) Match m = reApDef.Match(line); if (m.Success) { int id = Int32.Parse(m.Groups[1].Captures[0].Value); double width = Double.Parse(m.Groups[2].Captures[0].Value) * INMM; double height = Double.Parse(m.Groups[3].Captures[0].Value) * INMM; apertures.Add(id, new Aperture(width, height)); //Console.WriteLine("add ap " + id); } m = reCircleDef.Match(line); if (m.Success) { int id = Int32.Parse(m.Groups[1].Captures[0].Value); double diam = Double.Parse(m.Groups[2].Captures[0].Value) * INMM; Aperture apCircle = new Aperture(diam, diam); apCircle.CircleDiameter = (float)(diam); apertures.Add(id, apCircle); //Console.WriteLine("add circle " + id); } m = reOctoDef.Match(line); if (m.Success) { int id = Int32.Parse(m.Groups[1].Captures[0].Value); double diam = Double.Parse(m.Groups[2].Captures[0].Value) * INMM; Aperture apCircle = new Aperture(diam, diam); apCircle.CircleDiameter = (float)(diam); apertures.Add(id, apCircle); //Console.WriteLine("add (octo) circle " + id); } m = reApGroup.Match(line); if (m.Success) { currentAperture = Int32.Parse(m.Groups[1].Captures[0].Value); if (!apertures.ContainsKey(currentAperture)) { throw new Exception("Don't have a definition for aperture " + currentAperture + "!"); } } m = reFSLA.Match(line); if (m.Success) { Group group = m.Groups[2]; CaptureCollection cc = group.Captures; decimal_x = Int32.Parse(cc[0].Value); group = m.Groups[4]; cc = group.Captures; decimal_y = Int32.Parse(cc[0].Value); mul_x = Math.Pow(10, decimal_x); mul_y = Math.Pow(10, decimal_y); } m = reXYLine.Match(line); if (m.Success) { if (currentAperture == -1) { throw new Exception("Not in an aperture group!"); } x = Double.Parse(m.Groups[1].Captures[0].Value) / mul_x * INMM; //* dscale; y = Double.Parse(m.Groups[2].Captures[0].Value) / mul_y * INMM; //* dscale; d = Int32.Parse(m.Groups[3].Captures[0].Value); switch (d) { case 1: case 2: if (apertures[currentAperture].CircleDiameter < 0) { throw new Exception("Dragging non-circle"); } else { Group group = m.Groups[1]; CaptureCollection cc = group.Captures; x = Double.Parse(cc[0].Value) / mul_x * INMM; if (INVERT_X) x = maxx + minx - x; group = m.Groups[2]; cc = group.Captures; y = Double.Parse(cc[0].Value) / mul_y * INMM; if (INVERT_Y) y = maxy + miny - y; group = m.Groups[3]; cc = group.Captures; d = Int32.Parse(cc[0].Value); // D02 is move... if (d == 2) pstart = new PointF((float)(x), (float)(y)); // D01 is line... else if (d == 1) { pend = new PointF((float)(x), (float)(y)); //pointList.Add(new PointF[] { pstart, pend }); LineF newline = new LineF(pstart, pend); newline.width = apertures[currentAperture].CircleDiameter; lineList.Add(newline); pstart = pend; } } break; case 3: { // are these centers or corners? // A: looks like centers. do the offset // when you create the apertures. if (INVERT_X) x = (maxx + minx - x); if (INVERT_Y) y = (maxy + miny - y); Aperture ap = apertures[currentAperture].clone(); ap.OffsetToCenter(x, y); placedAps.Add(ap); //if (ap.CircleDiameter > 0) MessageBox.Show("Placed circle!"); } break; default: throw new Exception(string.Format("D{0:00} in cream file!", d)); } } } catch { } } }