public void Rotate() { var p2 = new Vec2d(2, 1); Assert.That(new Vec2d(-0.9999999999999999, 2).IsEqual(p2.Rotated(Math.PI / 2), 0.000001, 0.000001)); p2.Rotate(Math.PI / 2); Assert.That(new Vec2d(-0.9999999999999999, 2).IsEqual(p2, 0.000001, 0.000001)); }
public Vec2d GetEdge(int edge) { Vec2d result = new Vec2d(0, OCRRadius); result.Rotate(MathExt.ToRadians(30 + edge * 60)); result += OCRCenter; return(result); }
private static Rectangle GetBoundingBox(Vec2d center, double radius) { Vec2d top = new Vec2d(0, radius); top.Rotate(MathExt.ToRadians(30 + 3 * 60)); Vec2d right = new Vec2d(0, radius); right.Rotate(MathExt.ToRadians(30 + 4 * 60)); Vec2d bottom = new Vec2d(0, radius); bottom.Rotate(MathExt.ToRadians(30 + 5 * 60)); Vec2d left = new Vec2d(0, radius); left.Rotate(MathExt.ToRadians(30 + 1 * 60)); return(new Rectangle((int)(center.X + left.X), (int)(center.Y + top.Y), (int)(right.X - left.X), (int)(bottom.Y - top.Y))); }
public void ValueType_Vec2d() { var p1 = new Vec2d(1, 0); var p2 = new Vec2d(0, 1); Assert.IsFalse(p1.IsEqual(p2, 0.99, 0.1)); Assert.IsTrue(p1.IsEqual(p2, 1.01, 0.1)); Assert.IsTrue(p1.IsEqual(p2, 0.99, Math.PI / 2)); Assert.IsTrue(p1.IsNormal(p2, 0.1)); Assert.IsFalse(p1.IsOpposite(p2, 0.1)); Assert.IsTrue(p1.IsOpposite(p2, Math.PI / 2)); Assert.IsFalse(p1.IsParallel(p2, 0.1)); Assert.IsTrue(p1.IsParallel(p2, Math.PI / 2)); p1 = new Vec2d(1, 2); p2 = new Vec2d(4, 5); Assert.AreEqual(5, p1.SquareMagnitude()); Assert.AreEqual(Math.Sqrt(5), p1.Magnitude()); p2 = p1; p2.Add(new Vec2d(1, 2)); Assert.AreEqual(new Vec2d(2, 4), p2); Assert.AreEqual(new Vec2d(2, 4), p1.Added(new Vec2d(1, 2))); p2 = new Vec2d(1, 2); p2.Subtract(new Vec2d(3, 2)); Assert.AreEqual(new Vec2d(-2, 0), p2); Assert.AreEqual(new Vec2d(-2, 0), p1.Subtracted(new Vec2d(3, 2))); p2 = new Vec2d(1, 2); Assert.AreEqual(-4, p1.Crossed(new Vec2d(3, 2))); Assert.AreEqual(Math.Sqrt(16), p1.CrossMagnitude(new Vec2d(3, 2))); Assert.AreEqual(16, p1.CrossSquareMagnitude(new Vec2d(3, 2))); p2 = new Vec2d(1, 2); p2.Divide(2); Assert.AreEqual(new Vec2d(0.5, 1), p2); Assert.AreEqual(new Vec2d(0.5, 1), p1.Divided(2)); Assert.AreEqual(5, p1.Dot(new Vec2d(1, 2))); p2 = new Vec2d(1, 2); p2.Multiply(2); Assert.AreEqual(new Vec2d(2, 4), p2); Assert.AreEqual(new Vec2d(2, 4), p1.Multiplied(2)); p2 = new Vec2d(1, 2); p2.Scale(2); Assert.AreEqual(new Vec2d(2, 4), p2); Assert.AreEqual(new Vec2d(2, 4), p1.Scaled(2)); p2 = new Vec2d(1, 23); Assert.AreEqual("0.0434372242763069,0.99905615835506", p2.Normalized().ToString()); p2.Normalize(); Assert.AreEqual("0.0434372242763069,0.99905615835506", p2.ToString()); p2 = new Vec2d(1, 2); p2.Reverse(); Assert.AreEqual(new Vec2d(-1, -2), p2); Assert.AreEqual(new Vec2d(-1, -2), p1.Reversed()); p2.SetLinearForm(new Vec2d(1, 2), new Vec2d(4, 5)); Assert.AreEqual(new Vec2d(5, 7), p2); p2.SetLinearForm(2, new Vec2d(1, 2), new Vec2d(4, 5)); Assert.AreEqual(new Vec2d(6, 9), p2); p2.SetLinearForm(2, new Vec2d(1, 2), 3, new Vec2d(4, 5)); Assert.AreEqual(new Vec2d(14, 19), p2); p2.SetLinearForm(2, new Vec2d(1, 2), 3, new Vec2d(4, 5), new Vec2d(7, 8)); Assert.AreEqual(new Vec2d(21, 27), p2); p2 = new Vec2d(2, 1); Assert.AreEqual(new Vec2d(1, 0), p2.Mirrored(new Vec2d(1, 0))); p2.Mirror(new Vec2d(1, 0)); Assert.AreEqual(new Vec2d(1, 0), p2); var m2 = new Ax2d(new Pnt2d(-1, 2), new Dir2d(-1, 0)); p2 = new Vec2d(2, 1); Assert.AreEqual(new Vec2d(2, -1), p2.Mirrored(m2)); p2.Mirror(m2); Assert.AreEqual(new Vec2d(2, -1), p2); p2 = new Vec2d(2, 1); Assert.AreEqual("-1,2", p2.Rotated(Math.PI / 2).ToString()); p2.Rotate(Math.PI / 2); Assert.AreEqual("-1,2", p2.ToString()); //TestContext.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", gp2.x, gp2.y, gp2.z)); Trsf2d t1 = new Trsf2d(); t1.SetRotation(new Pnt2d(1, 2), Math.PI / 2); p2 = new Vec2d(2, 1); Assert.AreEqual("-1,2", p2.Transformed(t1).ToString()); p2.Transform(t1); Assert.AreEqual("-1,2", p2.ToString()); }
//-------------------------------------------------------------------------------------------------- bool _MakeSketch(IShapeOperand sourceShape) { var sourceBRep = GetOperandBRep(0); if (sourceBRep == null) { return(false); } // Calculate Offsets var extents = sourceBRep.BoundingBox().Extents(); var interval1 = new Vec2d(_CalculateOffset(DistanceMode1, Quantity1, Distance1, extents.X), 0); interval1.Rotate(_Rotation.ToRad()); var offset = Vec2d.Zero; switch (_Alignment1) { case AlignmentMode.Center: offset += interval1 * (Quantity1 - 1) * -0.5; break; case AlignmentMode.Last: interval1 *= -1; break; } var interval2 = new Vec2d(0, _CalculateOffset(DistanceMode2, Quantity2, Distance2, extents.Y)); interval2.Rotate(_Rotation.ToRad()); switch (_Alignment2) { case AlignmentMode.Center: offset += interval2 * (Quantity2 - 1) * -0.5; break; case AlignmentMode.Last: interval2 *= -1; break; } // Build Transforms List <Trsf2d> transforms = new List <Trsf2d>((int)(Quantity1 * Quantity2)); for (var index1 = 0; index1 < Quantity1; index1++) { for (var index2 = 0; index2 < Quantity2; index2++) { if (_Border && index1 != 0 && index1 != Quantity1 - 1 && index2 != 0 && index2 != Quantity2 - 1) { continue; // Skip inner parts } var transform = new Trsf2d(); transform.SetTranslation(interval1 * index1 + interval2 * index2 + offset); transforms.Add(transform); } } // Do it! var resultShape = Topo2dUtils.TransformSketchShape(sourceBRep, transforms, false); if (resultShape == null) { return(false); } // Finalize BRep = resultShape; return(true); }
public Bitmap DisplayOCR(Bitmap shot, HexGrid grid) { shot = new Bitmap(shot); using (Graphics g = Graphics.FromImage(shot)) { g.FillRectangle(new SolidBrush(Color.White), 0, 0, shot.Width, shot.Height); Font fnt = new Font("Arial", 12, FontStyle.Bold); Font fnt_big = new Font("Arial", 22, FontStyle.Bold); Brush fntBush1 = new SolidBrush(Color.Black); Brush fntBush2 = new SolidBrush(Color.FromArgb(32, Color.Black)); Brush fntBush3 = new SolidBrush(Color.Red); Pen bluepen = new Pen(Color.FromArgb(0, 164, 235)); Brush bluebrush = new SolidBrush(Color.FromArgb(128, 0, 164, 235)); StringFormat fmt = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; foreach (var hex in grid) { var points = Enumerable.Range(0, 7).Select(p => hex.Value.GetEdge(p)).Select(p => new Point((int)p.X, (int)p.Y)).ToArray(); if (hex.Value.Type == HexagonType.NOCELL) { g.FillPolygon(new SolidBrush(Color.Gainsboro), points); } else { g.FillPolygon(new SolidBrush(Color.SlateGray), points); } if (hex.Value.Hint.Type != CellHintType.NONE) { if (hex.Value.Hint.Area == CellHintArea.CIRCLE) { Vec2d center = hex.Value.Image.OCRCenter; double hexheight = hex.Value.Image.OCRHeight * 0.8; g.FillEllipse(bluebrush, (int)(center.X - hexheight), (int)(center.Y - hexheight), (int)(2 * hexheight), (int)(2 * hexheight)); g.DrawEllipse(bluepen, (int)(center.X - hexheight), (int)(center.Y - hexheight), (int)(2 * hexheight), (int)(2 * hexheight)); } else if (hex.Value.Hint.Area == CellHintArea.COLUMN_LEFT) { Vec2d p1 = hex.Value.Image.GetEdge(0); Vec2d p2 = hex.Value.Image.GetEdge(1); Vec2d pm = p1 - p2; pm.Rotate(MathExt.ToRadians(-90)); pm.SetLength(hex.Value.Image.OCRHeight / 4); Point[] polypoints = new[] { p1, p2, p2 + pm, p1 + pm, p1 }.Select(p => new Point((int)p.X, (int)p.Y)).ToArray(); g.FillPolygon(bluebrush, polypoints); g.DrawPolygon(bluepen, polypoints); } else if (hex.Value.Hint.Area == CellHintArea.COLUMN_DOWN) { Vec2d p1 = hex.Value.Image.GetEdge(5); Vec2d p2 = hex.Value.Image.GetEdge(0); Vec2d pm = p1 - p2; pm.Rotate(MathExt.ToRadians(-90)); pm.SetLength(hex.Value.Image.OCRHeight / 4); Point[] polypoints = new[] { p1, p2, p2 + pm, p1 + pm, p1 }.Select(p => new Point((int)p.X, (int)p.Y)).ToArray(); g.FillPolygon(bluebrush, polypoints); g.DrawPolygon(bluepen, polypoints); } else if (hex.Value.Hint.Area == CellHintArea.COLUMN_RIGHT) { Vec2d p1 = hex.Value.Image.GetEdge(4); Vec2d p2 = hex.Value.Image.GetEdge(5); Vec2d pm = p1 - p2; pm.Rotate(MathExt.ToRadians(-90)); pm.SetLength(hex.Value.Image.OCRHeight / 4); Point[] polypoints = new[] { p1, p2, p2 + pm, p1 + pm, p1 }.Select(p => new Point((int)p.X, (int)p.Y)).ToArray(); g.FillPolygon(bluebrush, polypoints); g.DrawPolygon(bluepen, polypoints); } g.DrawString(hex.Value.Hint.ToString(), fnt, fntBush1, hex.Value.Image.BoundingBox, fmt); } else { if (hex.Value.Type == HexagonType.NOCELL || hex.Value.Type == HexagonType.HIDDEN) { g.DrawString(hex.Value.Hint.ToString(), fnt, fntBush2, hex.Value.Image.BoundingBox, fmt); } else if (hex.Value.Type == HexagonType.UNKNOWN) { g.DrawString(hex.Value.Hint.ToString(), fnt, fntBush3, hex.Value.Image.BoundingBox, fmt); } else { g.DrawString(hex.Value.Hint.ToString(), fnt, fntBush1, hex.Value.Image.BoundingBox, fmt); } } } g.FillRectangle(new SolidBrush(Color.SlateGray), grid.CounterArea.BoundingBox); g.DrawString(grid.CounterArea.Value.Value.ToString(), fnt_big, fntBush1, grid.CounterArea.BoundingBox, fmt); } return(shot); }