//点位置初始化的函数(随机连线) void Randomization(ref PointF[] pointf, ref Point[] point) { List <int> list = new List <int>(); Point[] tmp = new Point[point.Length]; PointF[] tmpf = new PointF[pointf.Length]; int i, index; list.Clear(); for (i = 0; i < point.Length; ++i) { list.Add(i); } for (i = 0; i < point.Length; ++i) { index = ro.Next(point.Length - i); tmp[i] = point[list[index]]; tmpf[i] = pointf[list[index]]; list.RemoveAt(index); } tmp.CopyTo(point, 0); tmpf.CopyTo(pointf, 0); }
public void OnMouseDown(MouseEventArgs e) { bool flag2; bool flag3; PointF tf1; if (e.Button != MouseButtons.Left) { return; } this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y)); SizeF ef1 = this.mouseAreaControl.PicturePanel.GridSize; float single1 = ef1.Height; float single2 = ef1.Width; if (this.mouseAreaControl.PicturePanel.SnapToGrid) { int num1 = (int)((this.startpoint.X + (single2 / 2f)) / single2); int num2 = (int)((this.startpoint.Y + (single1 / 2f)) / single1); this.startpoint = (PointF) new Point((int)(num1 * single2), (int)(num2 * single1)); } SvgDocument document1 = this.mouseAreaControl.SVGDocument; bool flag1 = document1.AcceptChanges; document1.NumberOfUndoOperations = 200; this.mousedown = true; switch (this.operate) { case PolyOperate.Draw: { document1.AcceptChanges = false; flag2 = false; if (this.graph != null) { if (!(this.graph is Polygon) && !(this.graph is Polyline)) { flag2 = true; } break; } flag2 = true; break; } case PolyOperate.MovePath: { goto Label_05F5; } case PolyOperate.MovePoint: { PointF tf3; this.nextPoint = tf3 = PointF.Empty; this.prePoint = tf3; if ((this.moveindex < 0) || (this.moveindex >= this.points.Length)) { goto Label_05F5; } flag3 = this.graph is Polygon; if ((this.moveindex - 1) < 0) { if ((this.points.Length >= 3) && flag3) { this.prePoint = this.points[this.points.Length - 1]; } goto Label_042C; } this.prePoint = this.points[this.moveindex - 1]; goto Label_042C; } case PolyOperate.Del: { if ((this.moveindex >= 0) && (this.moveindex < this.points.Length)) { ArrayList list1 = new ArrayList(this.points); list1.RemoveAt(this.moveindex); this.points = new PointF[list1.Count]; list1.CopyTo(this.points); Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone(); matrix1.Invert(); if (this.points.Length > 0) { matrix1.TransformPoints(this.points); } } goto Label_05F5; } case PolyOperate.Add: { if ((this.insertindex < 0) || (this.insertindex >= this.points.Length)) { goto Label_05F5; } this.points = new PointF[0]; if (!(this.graph is Polygon)) { if (this.graph is Polyline) { this.points = ((Polyline)this.graph).Points; } goto Label_058D; } this.points = ((Polygon)this.graph).Points; goto Label_058D; } default: { goto Label_05F5; } } if (flag2) { IGraph graph1 = this.mouseAreaControl.PicturePanel.PreGraph; if (graph1 == null) { return; } this.graph = (Graph)((SvgElement)graph1).Clone(); this.mouseAreaControl.SVGDocument.AcceptChanges = false; if (this.graph != null) { ((SvgElement)this.graph).RemoveAttribute("points"); } if (((SvgElement)this.graph) is IGraphPath) { if ((((SvgElement)graph1).GetAttribute("style") != string.Empty) && (((SvgElement)graph1).GetAttribute("style") != null)) { this.mouseAreaControl.SVGDocument.AcceptChanges = false; AttributeFunc.SetAttributeValue((SvgElement)this.graph, "style", ((SvgElement)this.graph).GetAttribute("style")); } ISvgBrush brush1 = ((IGraphPath)graph1).GraphBrush; if (brush1 is SvgElement) { ISvgBrush brush2 = (ISvgBrush)((SvgElement)brush1).Clone(); ((IGraphPath)this.graph).GraphBrush = brush2; ((SvgElement)brush2).pretime = -1; } else { ((IGraphPath)this.graph).GraphBrush = brush1; } brush1 = ((IGraphPath)graph1).GraphStroke.Brush; if (brush1 is SvgElement) { ISvgBrush brush3 = (ISvgBrush)((SvgElement)brush1).Clone(); ((IGraphPath)this.graph).GraphStroke = new Stroke(brush3); ((SvgElement)brush3).pretime = -1; } else { ((IGraphPath)this.graph).GraphStroke.Brush = brush1; } } } PointF[] tfArray1 = new PointF[0]; if (this.graph is Polygon) { tfArray1 = ((Polygon)this.graph).Points; } else if (this.graph is Polyline) { tfArray1 = ((Polyline)this.graph).Points; } this.points = new PointF[1]; if (tfArray1 != null) { this.points = new PointF[tfArray1.Length + 1]; tfArray1.CopyTo(this.points, 0); } this.points[this.points.Length - 1] = this.startpoint; goto Label_05F5; Label_042C: if ((this.moveindex + 1) < this.points.Length) { this.nextPoint = this.points[this.moveindex + 1]; goto Label_05F5; } if ((this.points.Length >= 3) && flag3) { this.nextPoint = this.points[0]; } goto Label_05F5; Label_058D: tf1 = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y)); ArrayList list2 = new ArrayList(this.points); list2.Insert(this.insertindex, tf1); this.points = new PointF[list2.Count]; list2.CopyTo(this.points); Label_05F5: if (((this.operate == PolyOperate.Del) || (this.operate == PolyOperate.Draw)) || (this.operate == PolyOperate.Add)) { string text1 = string.Empty; int num3 = 0; PointF[] tfArray2 = this.points; for (int num4 = 0; num4 < tfArray2.Length; num4++) { PointF tf2 = tfArray2[num4]; text1 = text1 + tf2.X.ToString() + " " + tf2.Y.ToString(); if (num3 < (this.points.Length - 1)) { text1 = text1 + ","; } num3++; } this.mouseAreaControl.PicturePanel.InvalidateElement((SvgElement)this.graph); if (((SvgElement)this.graph).ParentNode == null) { this.UpdateGraph(text1); document1.AcceptChanges = true; IGraph graph2 = this.graph; this.mouseAreaControl.PicturePanel.AddElement(this.graph); this.graph = graph2; } else { document1.AcceptChanges = true; this.UpdateGraph(text1); } document1.AcceptChanges = flag1; this.mouseAreaControl.Invalidate(); if (this.graph != null) { ((SvgElement)this.graph).pretime = -1; this.mouseAreaControl.PicturePanel.InvalidateElement((SvgElement)this.graph); } } document1.NotifyUndo(); this.reversePath.Reset(); }
public virtual void FillPolygon(Brush brush, PointF[] points) { PointF[] transformedPoints = new PointF[points.Length]; points.CopyTo(transformedPoints, 0); matrix.TransformPoints(transformedPoints); Point[] iTransformedPoints = new Point[transformedPoints.Length]; for(int i = 0; i < transformedPoints.Length; i++) { iTransformedPoints[i] = (Point)transformedPoints[i]; } graphics.FillPolygon(brush, iTransformedPoints); }
public virtual void DrawPolygon(Pen pen, PointF[] points) { PointF[] transformedPoints = new PointF[points.Length]; points.CopyTo(transformedPoints, 0); matrix.TransformPoints(transformedPoints); Point[] iTransformedPoints = new Point[transformedPoints.Length]; for(int i = 0; i < transformedPoints.Length; i++) { iTransformedPoints[i] = (Point)transformedPoints[i]; } graphics.DrawPolygon(pen, iTransformedPoints); }
public void OnMouseDown(MouseEventArgs e) { bool flag2; bool flag3; PointF tf1; if (e.Button == MouseButtons.Left) { //wlwl this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF(e.X, e.Y)); if (this.mouseAreaControl.CurrentOperation == ToolOperation.XPolyLine) { if (points.Length > 0) { this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, points[0].Y)); } else { this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y)); } } if (this.mouseAreaControl.CurrentOperation == ToolOperation.YPolyLine) { if (points.Length > 0) { this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF(points[0].X, (float)e.Y)); } else { this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y)); } } if (this.mouseAreaControl.CurrentOperation == ToolOperation.PolyLine || this.mouseAreaControl.CurrentOperation == ToolOperation.Polygon || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.LeadLine || this.mouseAreaControl.CurrentOperation == ToolOperation.AreaPolygon) { this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y)); } if (this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure) { this.AreaPoints.Add(new PointF((float)e.X, (float)e.Y)); } SizeF ef1 = this.mouseAreaControl.PicturePanel.GridSize; float single1 = ef1.Height; float single2 = ef1.Width; if (this.mouseAreaControl.PicturePanel.SnapToGrid) { int num1 = (int)((this.startpoint.X + (single2 / 2f)) / single2); int num2 = (int)((this.startpoint.Y + (single1 / 2f)) / single1); this.startpoint = (PointF) new Point((int)(num1 * single2), (int)(num2 * single1)); } SvgDocument document1 = this.mouseAreaControl.SVGDocument; bool flag1 = document1.AcceptChanges; document1.NumberOfUndoOperations = 200; this.mousedown = true; switch (this.operate) { case PolyOperate.Draw: { document1.AcceptChanges = false; flag2 = false; if (this.graph != null) { if (!(this.graph is Polygon) && !(this.graph is Polyline)) { flag2 = true; } break; } flag2 = true; break; } case PolyOperate.MovePath: { goto Label_05F5; } case PolyOperate.MovePoint: { PointF tf3; this.nextPoint = tf3 = PointF.Empty; this.prePoint = tf3; if ((this.moveindex < 0) || (this.moveindex >= this.points.Length)) { goto Label_05F5; } flag3 = this.graph is Polygon; if ((this.moveindex - 1) < 0) { if ((this.points.Length >= 3) && flag3) { this.prePoint = this.points[this.points.Length - 1]; } goto Label_042C; } this.prePoint = this.points[this.moveindex - 1]; goto Label_042C; } case PolyOperate.Del: { if ((this.moveindex >= 0) && (this.moveindex < this.points.Length)) { ArrayList list1 = new ArrayList(this.points); list1.RemoveAt(this.moveindex); this.points = new PointF[list1.Count]; list1.CopyTo(this.points); Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone(); matrix1.Invert(); if (this.points.Length > 0) { matrix1.TransformPoints(this.points); } } goto Label_05F5; } case PolyOperate.Break: //线路断开 { if ((this.moveindex > 0) && (this.moveindex < this.points.Length - 1)) { ArrayList list1 = new ArrayList(this.points); PointF[] points2 = new PointF[this.points.Length - moveindex]; this.points = new PointF[moveindex + 1]; list1.CopyTo(0, this.points, 0, this.moveindex + 1); list1.CopyTo(moveindex, points2, 0, list1.Count - moveindex); Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone(); matrix1.Invert(); if (points2.Length > 0) { matrix1.TransformPoints(points2); SvgElement copyEelement = (this.graph as XmlNode).CloneNode(true) as SvgElement; IGraph graph1 = this.graph; copyEelement.SetAttribute("info-name", ((SvgElement)graph1).GetAttribute("info-name") + "-2"); ((SvgElement)graph).SetAttribute("info-name", ((SvgElement)graph1).GetAttribute("info-name") + "-1"); copyEelement = this.mouseAreaControl.PicturePanel.AddElement(copyEelement); this.mouseAreaControl.SVGDocument.CurrentElement = graph1 as SvgElement; copyEelement.RemoveAttribute("points"); UpdateGraph(copyEelement, points2); this.mouseAreaControl.PicturePanel.InvalidateElement(copyEelement); BreakElementEventArgs copy = new BreakElementEventArgs(copyEelement); if (OnPolyLineBreak != null && copyEelement != null) { OnPolyLineBreak(this.mouseAreaControl.SVGDocument.CurrentElement, copy); } } if (this.points.Length > 0) { matrix1.TransformPoints(this.points); } } else { return; } goto Label_05F5; } case PolyOperate.Add: { if ((this.insertindex < 0) || (this.insertindex >= this.points.Length)) { goto Label_05F5; } this.points = new PointF[0]; if (!(this.graph is Polygon)) { if (this.graph is Polyline) { this.points = ((Polyline)this.graph).Points; } goto Label_058D; } this.points = ((Polygon)this.graph).Points; goto Label_058D; } default: { goto Label_05F5; } } if (flag2) { IGraph graph1 = this.mouseAreaControl.PicturePanel.PreGraph; if (graph1 == null) { return; } this.graph = (Graph)((SvgElement)graph1).Clone(); this.mouseAreaControl.SVGDocument.AcceptChanges = false; if (this.graph != null) { ((SvgElement)this.graph).RemoveAttribute("points"); } if (((SvgElement)this.graph) is IGraphPath) { if ((((SvgElement)graph1).GetAttribute("style") != string.Empty) && (((SvgElement)graph1).GetAttribute("style") != null)) { this.mouseAreaControl.SVGDocument.AcceptChanges = false; AttributeFunc.SetAttributeValue((SvgElement)this.graph, "style", ((SvgElement)this.graph).GetAttribute("style")); } ISvgBrush brush1 = ((IGraphPath)graph1).GraphBrush; if (brush1 is SvgElement) { ISvgBrush brush2 = (ISvgBrush)((SvgElement)brush1).Clone(); ((IGraphPath)this.graph).GraphBrush = brush2; ((SvgElement)brush2).pretime = -1; } else { ((IGraphPath)this.graph).GraphBrush = brush1; } brush1 = ((IGraphPath)graph1).GraphStroke.Brush; if (brush1 is SvgElement) { ISvgBrush brush3 = (ISvgBrush)((SvgElement)brush1).Clone(); ((IGraphPath)this.graph).GraphStroke = new Stroke(brush3); ((SvgElement)brush3).pretime = -1; } else { ((IGraphPath)this.graph).GraphStroke.Brush = brush1; } } } PointF[] tfArray1 = new PointF[0]; if (this.graph is Polygon) { tfArray1 = ((Polygon)this.graph).Points; } else if (this.graph is Polyline) { tfArray1 = ((Polyline)this.graph).Points; } this.points = new PointF[1]; int insertIndex = 0; if (tfArray1 != null) { ; this.points = new PointF[tfArray1.Length + 1]; if (addBegin) { tfArray1.CopyTo(this.points, 1); } else { tfArray1.CopyTo(this.points, 0); insertIndex = tfArray1.Length; } } if (addBegin || addEnd) { Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone(); matrix1.Invert(); PointF[] points2 = new PointF[1] { new PointF(e.X, e.Y) }; matrix1.TransformPoints(points2); this.startpoint = points2[0]; } this.points[insertIndex] = this.startpoint; goto Label_05F5; Label_042C: if ((this.moveindex + 1) < this.points.Length) { this.nextPoint = this.points[this.moveindex + 1]; goto Label_05F5; } if ((this.points.Length >= 3) && flag3) { this.nextPoint = this.points[0]; } goto Label_05F5; Label_058D: Matrix matrix2 = this.graph.GraphTransform.Matrix.Clone(); matrix2.Invert(); //tf1 = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y));//new PointF((float) e.X, (float) e.Y) PointF[] tfTemp = new PointF[1] { new PointF(e.X, e.Y) }; matrix2.TransformPoints(tfTemp); ArrayList list2 = new ArrayList(this.points); list2.Insert(this.insertindex, tfTemp[0]); this.points = new PointF[list2.Count]; list2.CopyTo(this.points); Label_05F5: //2006-10-23 设置围栏初始颜色 // if(this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure) // { // ((XmlElement) this.graph).SetAttribute("style","fill:#C0C0FF;fill-opacity:0.3;stroke:#000000;stroke-opacity:1;"); // } if (((this.operate == PolyOperate.Del) || (this.operate == PolyOperate.Draw)) || (this.operate == PolyOperate.Add) || (this.operate == PolyOperate.Break)) { StringBuilder text1 = new StringBuilder(); int num3 = 0; PointF[] tfArray2 = this.points; for (int num4 = 0; num4 < tfArray2.Length; num4++) { PointF tf2 = tfArray2[num4]; text1.Append(tf2.X.ToString() + " " + tf2.Y.ToString()); if (num3 < (this.points.Length - 1)) { text1.Append(","); } num3++; } this.mouseAreaControl.PicturePanel.InvalidateElement((SvgElement)this.graph); if (((SvgElement)this.graph).ParentNode == null) { this.UpdateGraph(text1.ToString()); document1.AcceptChanges = true; IGraph graph2 = this.graph; this.mouseAreaControl.PicturePanel.AddElement(this.graph); this.graph = graph2; } else { document1.AcceptChanges = true; this.UpdateGraph(text1.ToString()); } document1.AcceptChanges = flag1; this.mouseAreaControl.Invalidate(); if (this.graph != null) { ((SvgElement)this.graph).pretime = -1; this.mouseAreaControl.PicturePanel.InvalidateElement((SvgElement)this.graph); } } document1.NotifyUndo(); this.reversePath.Reset(); } else if (e.Button == MouseButtons.Right) //任务1 { if (this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure) { //this.AreaPoints.Add(new PointF((float) e.X, (float) e.Y)); // this.mouseAreaControl.GoBottom((SvgElement)this.graph); // ((SvgElement)this.graph).Clone(); PointF[] tfArray1 = new PointF[this.AreaPoints.Count]; this.AreaPoints.CopyTo(tfArray1, 0); this.AreaPoints.Clear(); Matrix matrix1 = new Matrix(); if (tfArray1.Length < 3) { this.mouseAreaControl.CurrentOperation = ToolOperation.Select; return; } this.selectAreaPath = new GraphicsPath(); this.selectAreaPath.AddLines(tfArray1); this.selectAreaPath.CloseFigure(); Region region1 = new Region(this.selectAreaPath); RectangleF r1 = selectAreaPath.GetBounds(); /* 2005环境 当前区域需要手动添加进集合*/ XmlNode node1 = ((SvgElement)this.graph).Clone(); SvgElement svgele = (SvgElement)node1; this.mouseAreaControl.SVGDocument.ClearSelects(); using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) { foreach (ILayer layer1 in mouseAreaControl.SVGDocument.Layers) { if (!layer1.Visible) { continue; } SvgElementCollection.ISvgElementEnumerator enumerator1 = layer1.GraphList.GetEnumerator(); while (enumerator1.MoveNext()) { IGraph graph1 = (IGraph)enumerator1.Current; if (!graph1.Visible || !graph1.DrawVisible || this.graph == graph1) { continue; } GraphicsPath path1 = (GraphicsPath)graph1.GPath.Clone(); path1.Transform(graph1.GraphTransform.Matrix); Region ef1 = null; if (graph1 is Use) { ef1 = new Region(PathFunc.GetBounds(path1)); } else if (graph1 is Line) { ef1 = new Region(PathFunc.GetBounds(path1)); } else { ef1 = new Region(path1); } // 设置围栏选择方式为完全包含才能选中 if (this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure) { RectangleF rt1 = ef1.GetBounds(g); ef1.Intersect(region1); if (ef1.GetBounds(g) == rt1) { this.mouseAreaControl.SVGDocument.AddSelectElement(graph1); } continue; } // 设置围栏选择方式为搭边即选中 if (this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure) { ef1.Intersect(region1); if (!ef1.GetBounds(g).IsEmpty) //if ((region1.IsVisible(ef1/*new System.Drawing.Rectangle((int) ef1.X, (int) ef1.Y, (int) ef1.Width, (int) ef1.Height)*/) && !graph1.IsLock) && (graph1.DrawVisible /*&& (AnimFunc.GetKeyIndex((SvgElement) graph1, this.mouseAreaControl.SVGDocument.ControlTime, true) >= 0)*/)) { this.mouseAreaControl.SVGDocument.AddSelectElement(graph1); } } } } this.mouseAreaControl.SVGDocument.AddSelectElement(graph); } GraphicsPath path2 = new GraphicsPath(); path2.AddLines(tfArray1); RectangleF ef2 = path2.GetBounds(); /* 2005 环境下使用,2003环境下需删除否则出现重复.原因不明.*/ if (this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure) { this.mouseAreaControl.SVGDocument.AddSelectElement(svgele); } this.mouseAreaControl.Invalidate(new System.Drawing.Rectangle(((int)ef2.X) - 10, ((int)ef2.Y) - 10, ((int)ef2.Width) + 20, ((int)ef2.Height) + 20)); return; } } //鼠标右键抬起 }
void SA(Point[] point, PointF[] pointf) { Randomization(ref pointf, ref point); double now = GetDis(point), tmp, t = T, de, best; PointF[] bestPathf = new PointF[pointf.Length]; Point[] bestPath = new Point[point.Length]; Point[] tmpPoint = new Point[point.Length]; PointF[] tmpPointf = new PointF[pointf.Length]; bool isBlock = false, isChange; int l1 = 0, l2 = 0, whichOpt; best = now; while (true) { //显示当前温度 try{ Invoke((EventHandler) delegate { label1.Text = t.ToString(); }); } catch { return; } for (int i = 0; i < SALoop; ++i) { point.CopyTo(tmpPoint, 0); pointf.CopyTo(tmpPointf, 0); whichOpt = GetNext(point, pointf); tmp = GetDis(point); de = now - tmp; isChange = false; if (de > 0) { ++opt[whichOpt]; now = tmp; isChange = true; l1 = 0; l2 = 0; if (tmp < best) { best = tmp; pointf.CopyTo(bestPathf, 0); point.CopyTo(bestPath, 0); } } else { if (Math.Exp(de / t) > ro.Next() / (double)System.Int32.MaxValue) { now = tmp; isChange = true; } else { tmpPoint.CopyTo(point, 0); tmpPointf.CopyTo(pointf, 0); } ++l1; } if (isChange) { while (isBlock) { Thread.Sleep(1); } isBlock = true; //这里可以改进:不用每次创建一个线程而是用一个线程每次唤醒它 //创建线程的原因:不用卡在Invoke而是继续执行 new Thread((ThreadStart) delegate { try { Invoke((EventHandler) delegate { label3.Text = now.ToString(); label9.Text = ((now - aimValue) / aimValue * 100).ToString("f2") + "%"; if (origin != null) { origin.Dispose(); } origin = new Bitmap(picBoxWidth, picBoxHeight); using (Graphics go = Graphics.FromImage(origin)) { using (GraphicsPath gpo = new GraphicsPath()) { gpo.AddPolygon(pointf); go.DrawPath(new Pen(Color.Black) { Width = 2 }, gpo); pictureBox1.Image = origin; } } }); } catch { return; } isBlock = false; }).Start(); } if (l1 > Limit) { ++l2; break; } } if (l2 > BLimit) { break; } //自己加的东西:每次在该温度下结束后用当前最优值覆盖当前值 bestPath.CopyTo(point, 0); bestPathf.CopyTo(pointf, 0); t *= delta; } //最后再把最优值输出 while (isBlock) { Thread.Sleep(1); } isBlock = true; new Thread((ThreadStart) delegate { try { Invoke((EventHandler) delegate { label3.Text = best.ToString(); label9.Text = ((best - aimValue) / aimValue * 100).ToString("f2") + "%"; if (origin != null) { origin.Dispose(); } origin = new Bitmap(picBoxWidth, picBoxHeight); using (Graphics go = Graphics.FromImage(origin)) { using (GraphicsPath gpo = new GraphicsPath()) { gpo.AddPolygon(bestPathf); go.DrawPath(new Pen(Color.Black) { Width = 2 }, gpo); pictureBox1.Image = origin; } } }); } catch { return; } isBlock = false; }).Start(); }
//爬山法 void HillClimbing(Point[] point, PointF[] pointf) { Randomization(ref pointf, ref point); double now = GetDis(point), tmp; Random ro = new Random(); bool isBlock = false; int x, y, l1 = 0, whichOpt; Point[] tmpPoint = new Point[point.Length]; PointF[] tmpPointf = new PointF[pointf.Length]; while (true) { point.CopyTo(tmpPoint, 0); pointf.CopyTo(tmpPointf, 0); whichOpt = GetNext(point, pointf); tmp = GetDis(point); if (tmp < now) { ++opt[whichOpt]; now = tmp; //防止主线程绘图卡死 while (isBlock) { Thread.Sleep(1); } isBlock = true; new Thread((ThreadStart) delegate { //在运行过程中直接关闭Form1时Invoke仍会调用(background线程也没用) //此时会抛出“无法访问已释放的对象”的异常(Form1对象Dispose了) //一种不完全的解决方法,用try catch忽略掉错误 try { Invoke((EventHandler) delegate { label3.Text = now.ToString(); label9.Text = ((now - aimValue) / aimValue * 100).ToString("f2") + "%"; if (origin != null) { origin.Dispose(); } origin = new Bitmap(picBoxWidth, picBoxHeight); using (Graphics go = Graphics.FromImage(origin)) { using (GraphicsPath gpo = new GraphicsPath()) { gpo.AddPolygon(pointf); go.DrawPath(new Pen(Color.Black) { Width = 2 }, gpo); pictureBox1.Image = origin; } } }); } catch { return; } isBlock = false; }).Start(); } else { tmpPoint.CopyTo(point, 0); tmpPointf.CopyTo(pointf, 0); ++l1; } if (l1 > 100000) { break; } } }
private void Form1_Paint(object sender, PaintEventArgs e) { var scale = (float)(Convert.ToDouble(scaleToolStripTextBox.Text) * 1e-3); var values = new double[fem.mesh.points.Count]; double max = -1.0e10, min = 1.0e10; if (fem.solved) { switch (toolStripMenuItem2.SelectedIndex) { case 0: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.displacements[2 * i]); } break; case 1: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.displacements[2 * i + 1]); } break; case 2: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.strains[i][0]); } break; case 3: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.strains[i][1]); } break; case 4: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.strains[i][2]); } break; case 5: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.stresses[i][0]); } break; case 6: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.stresses[i][1]); } break; case 7: for (int i = 0; i < values.Length; ++i) { values[i] = Math.Abs(fem.stresses[i][2]); } break; } for (int i = 0; i < values.Length; ++i) { if (max < values[i]) { max = values[i]; } } for (int i = 0; i < values.Length; ++i) { if (min > values[i]) { min = values[i]; } } for (int i = 0; i < values.Length; ++i) { values[i] = max > min ? (values[i] - min) / (max - min) : max; } } var gradientTriangles = new List <GradientTriangle>(); AddGradientTriangles(values, gradientTriangles, fem.mesh.triangles.ToArray(), scale); foreach (var quadrangle in fem.mesh.quadrangles) { var triangles = new int[2][] { new int[] { quadrangle[0], quadrangle[1], quadrangle[2] }, new int[] { quadrangle[0], quadrangle[2], quadrangle[3] } }; AddGradientTriangles(values, gradientTriangles, triangles, scale); } foreach (var gradientTriangle in gradientTriangles) { if (fem.solved) { e.Graphics.FillRectangle(gradientTriangle.GetGradientBrush(), this.ClientRectangle); } else { e.Graphics.FillPolygon(Brushes.LawnGreen, gradientTriangle.points); } } var post_triangle_ps = new PointF[3]; for (int j = 0; j < fem.mesh.triangles.Count; ++j) { var ps = new PointF[3]; for (int i = 0; i < ps.Length; ++i) { ps[i] = new PointF(fem.mesh.points[fem.mesh.triangles[j][i]].X / scale, fem.mesh.points[fem.mesh.triangles[j][i]].Y / scale); } if (selectedElementType == ElementType.triangle && j == selectedElementNumber) { ps.CopyTo(post_triangle_ps, 0); } else { e.Graphics.DrawPolygon(Pens.Black, ps); } } var post_quadrangle_ps = new PointF[4]; for (int j = 0; j < fem.mesh.quadrangles.Count; ++j) { var ps = new PointF[4]; for (int i = 0; i < ps.Length; ++i) { ps[i] = new PointF(fem.mesh.points[fem.mesh.quadrangles[j][i]].X / scale, fem.mesh.points[fem.mesh.quadrangles[j][i]].Y / scale); } if (selectedElementType == ElementType.quadrangle && j == selectedElementNumber) { ps.CopyTo(post_quadrangle_ps, 0); } e.Graphics.DrawPolygon(Pens.Black, ps); } if (selectedElementType == ElementType.triangle) { e.Graphics.DrawPolygon(new Pen(Color.Red, 3), post_triangle_ps); } else if (selectedElementType == ElementType.quadrangle) { e.Graphics.DrawPolygon(new Pen(Color.Red, 3), post_quadrangle_ps); } foreach (var i in fem.mesh.fixXs) { var tri = new PointF[] { new PointF(0, 0), new PointF(-20, -10), new PointF(-20, 10) }; tri = tri.Select(x => new PointF(x.X + fem.mesh.points[i].X / scale, x.Y + fem.mesh.points[i].Y / scale)).ToArray(); e.Graphics.DrawPolygon(Pens.Blue, tri); } foreach (var i in fem.mesh.fixYs) { var tri = new PointF[] { new PointF(0, 0), new PointF(-10, 20), new PointF(10, 20) }; tri = tri.Select(x => new PointF(x.X + fem.mesh.points[i].X / scale, x.Y + fem.mesh.points[i].Y / scale)).ToArray(); e.Graphics.DrawPolygon(Pens.Blue, tri); } foreach (var f in fem.mesh.forceXs) { var tri = new PointF[] { new PointF(0 + 50 * f.value / 2, 0), new PointF(-10 + 50 * f.value / 2, -5), new PointF(-10 + 50 * f.value / 2, 5) }; tri = tri.Select(x => new PointF(x.X + fem.mesh.points[f.index].X / scale, x.Y + fem.mesh.points[f.index].Y / scale)).ToArray(); var line = new PointF[] { new PointF(0, 0), new PointF(50 * f.value / 2, 0) }; line = line.Select(x => new PointF(x.X + fem.mesh.points[f.index].X / scale, x.Y + fem.mesh.points[f.index].Y / scale)).ToArray(); e.Graphics.FillPolygon(Brushes.Red, tri); e.Graphics.DrawLine(Pens.Red, line[0], line[1]); } foreach (var f in fem.mesh.forceYs) { var tri = new PointF[] { new PointF(0, 0 - 50 * f.value / 2), new PointF(-5, 10 - 50 * f.value / 2), new PointF(5, 10 - 50 * f.value / 2) }; tri = tri.Select(x => new PointF(x.X + fem.mesh.points[f.index].X / scale, x.Y + fem.mesh.points[f.index].Y / scale)).ToArray(); var line = new PointF[] { new PointF(0, 0), new PointF(0, -50 * f.value / 2) }; line = line.Select(x => new PointF(x.X + fem.mesh.points[f.index].X / scale, x.Y + fem.mesh.points[f.index].Y / scale)).ToArray(); e.Graphics.FillPolygon(Brushes.Red, tri); e.Graphics.DrawLine(Pens.Red, line[0], line[1]); } var bar = new GradientTriangle[] { new GradientTriangle(new PointF[] { new PointF(530, 220), new PointF(560, 220), new PointF(530, 300) }, new Color[] { Color.Red, Color.Red, Color.LawnGreen }), new GradientTriangle(new PointF[] { new PointF(560, 220), new PointF(560, 300), new PointF(530, 300) }, new Color[] { Color.Red, Color.LawnGreen, Color.LawnGreen }), new GradientTriangle(new PointF[] { new PointF(530, 300), new PointF(560, 300), new PointF(530, 380) }, new Color[] { Color.LawnGreen, Color.LawnGreen, Color.Blue }), new GradientTriangle(new PointF[] { new PointF(560, 300), new PointF(560, 380), new PointF(530, 380) }, new Color[] { Color.LawnGreen, Color.Blue, Color.Blue }) }; foreach (var t in bar) { e.Graphics.FillRectangle(t.GetGradientBrush(), this.ClientRectangle); } if (fem.solved) { var i = toolStripMenuItem2.SelectedIndex; string unit = ""; if (i < 2) { unit = " [m]"; } else if (i < 5) { unit = ""; } else if (i < 8) { unit = " [Pa]"; } e.Graphics.DrawString(max.ToString("e2") + unit, DefaultFont, Brushes.Black, 560, 220); e.Graphics.DrawString(min.ToString("e2") + unit, DefaultFont, Brushes.Black, 560, 380); } e.Graphics.DrawLine(Pens.Black, new Point(20, 530), new Point(20 + 100, 530)); e.Graphics.DrawLine(Pens.Black, new Point(20, 530 - 5), new Point(20, 530 + 5)); e.Graphics.DrawLine(Pens.Black, new Point(20 + 100, 530 - 5), new Point(20 + 100, 530 + 5)); e.Graphics.DrawString((scale * 1e+3 * 100).ToString("F0") + " [mm]", DefaultFont, Brushes.Black, 20 + 30, 530 - 15); }
public static void CalcSpline(PointF[] points, int filecount, out PointF[] output, float minVal, float maxVal) { int Pcount = points.Length; output = new PointF[filecount]; //to find out where the curve calculation should start: (in case there are equal Y values wich should build a straight line) int start = 0; for (int i = 1; i < Pcount; i++) { if (points[i].Y != points[i - 1].Y) { start = i - 1; break; } } if (Pcount == 2) { float stepY = (points[points.Length - 1].Y - points[0].Y) / (float)(filecount - 1); float stepX = (points[points.Length - 1].X - points[0].X) / (float)(filecount - 1); for (int i = 0; i < filecount; i++) { output[i].X = points[0].X + (stepX * i); output[i].Y = points[0].Y + (stepY * i); if (output[i].Y < minVal) { output[i].Y = minVal; } else if (output[i].Y > maxVal) { output[i].Y = maxVal; } } } else if (Pcount > 2) { float[] yCoords = new float[Pcount]; // Newton form coefficients float[] xCoords = new float[Pcount]; // x-coordinates of nodes float y; float x; int precision = (filecount - 1) / (Pcount - 1); int npp = (Pcount * precision); // number of points used for drawing for (int i = 0; i < Pcount; i++) { xCoords[i] = points[i].X; yCoords[i] = points[i].Y; } float[] a = new float[Pcount]; float x1; float x2; float[] h = new float[Pcount]; for (int i = 1; i < Pcount; i++) { h[i] = xCoords[i] - xCoords[i - 1]; } float[] sub = new float[Pcount - 1]; float[] diag = new float[Pcount - 1]; float[] sup = new float[Pcount - 1]; for (int i = 1; i < Pcount - 1; i++) { diag[i] = (h[i] + h[i + 1]) / 3; sup[i] = h[i + 1] / 6; sub[i] = h[i] / 6; a[i] = (yCoords[i + 1] - yCoords[i]) / h[i + 1] - (yCoords[i] - yCoords[i - 1]) / h[i]; } solveTridiag(sub, diag, sup, ref a, Pcount - 2); int count = 1; output[0].X = points[0].X; output[0].Y = points[0].Y; for (int i = 1; i < Pcount; i++) { for (int j = 1; j <= precision; j++) { x1 = (h[i] * j) / precision; x2 = h[i] - x1; y = ((-a[i - 1] / 6 * (x2 + h[i]) * x1 + yCoords[i - 1]) * x2 + (-a[i] / 6 * (x1 + h[i]) * x2 + yCoords[i]) * x1) / h[i]; x = xCoords[i - 1] + x1; output[count].X = x; output[count].Y = y; if (output[count].Y < minVal) { output[count].Y = minVal; } else if (output[count].Y > maxVal) { output[count].Y = maxVal; } if (start > 0 && i <= start) { output[count].X = points[i].X; output[count].Y = points[i].Y; if (output[count].Y < minVal) { output[count].Y = minVal; } else if (output[count].Y > maxVal) { output[count].Y = maxVal; } } count++; } } //interpolate if there isn´t the same amount of points and filecount: if (npp - filecount - precision != 0) { int part = filecount / (Math.Abs(npp - filecount - precision) + 1); PointF[] tmpOut = new PointF[output.Length]; output.CopyTo(tmpOut, 0); int nr = 0; for (int i = 0; i < filecount; i++) { if (i == part * (nr + 1) && nr < Math.Abs(npp - filecount - precision)) { output[i].X = tmpOut[i - nr].X + ((tmpOut[i - nr].X - tmpOut[i - nr + 1].X) / 2); output[i].Y = tmpOut[i - nr].Y + ((tmpOut[i - nr].Y - tmpOut[i - nr + 1].Y) / 2); nr++; } else if (nr < Math.Abs(npp - filecount - precision)) { output[i] = tmpOut[i - nr]; } else { output[i] = tmpOut[i - nr + 1]; } } } } else { for (int i = 0; i < filecount; i++) { output[i].Y = points[0].Y; output[i].X = points[0].X; } } }