private void Pn_Show_Paint(object sender, PaintEventArgs e) { Graphics grap = e.Graphics; grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; for (int i = 0; i < pointCount; i++) { if (isShowPosition == true) { Utils.showLable(grap, points[i]); } Utils.drawCircle(grap, points[i], i.ToString()); } for (int i = 0; i < pointCount; i++) { for (int j = 0; j < pointCount; j++) { if (matRouting[i, j] != 0 && matRouting[i, j] != -1) { CPoint p = Utils.sub(points[i], points[j]); p = Utils.mul(Utils.normal(p), radius); if (isShowRay) { Utils.drawArrow(grap, Utils.sub(points[i], p), Utils.add(points[j], p), ArrowType.OneWay, Color.Gray, 1, DashStyle.Dash); } } } } for (int i = 0; i < route.Count - 1; i++) { CPoint p = Utils.sub(points[route[i]], points[route[i + 1]]); p = Utils.mul(Utils.normal(p), radius); Utils.drawArrow(grap, Utils.sub(points[route[i]], p), Utils.add(points[route[i + 1]], p), ArrowType.OneWay, Color.Blue, 2); } if (pointSelected != null) { Utils.drawCircleNoFill(grap, pointSelected, Color.Gray, 1); Utils.showLable(grap, pointSelected); } if (pointStart != null) { Utils.drawCircle(grap, pointStart, Color.Blue, 1); Utils.showLable(grap, pointStart, "BĐ"); } if (pointEnd != null) { Utils.drawCircle(grap, pointEnd, Color.Red); Utils.showLable(grap, pointEnd, "KT", -13); } }
public static CPoint normal(CPoint st) { float m = mag(st); if (m != 0) { return(new CPoint(st.x / m, st.y / m)); } return(new CPoint(0, 0)); }
public void ResetInfo() { points.Clear(); pointSelected = null; pointStart = null; pointEnd = null; oneDimCount = 0; twoDimCount = 0; CBB_PointStart.Items.Clear(); CBB_PointEnd.Items.Clear(); route.Clear(); }
public void ChangeSelectPoint(string strIndex, ref CPoint pChange) { if (strIndex != "None" && strIndex != null) { strIndex = strIndex.Substring(1, strIndex.Length - 1); pChange = points[Int32.Parse(strIndex)]; } else { pChange = null; } pn_show.Invalidate(); }
public void SetEndPointChoose() { pointEnd = pointSelected; int index = points.IndexOf(pointEnd); if (index != -1) { CBB_PointEnd.SelectedIndex = CBB_PointEnd.FindStringExact("P" + index.ToString()); } pointSelected = null; pn_show.Invalidate(); }
public static void drawArrow(Graphics graphic, CPoint st, CPoint ed, ArrowType _type, Color color, int lineWidth = 1, DashStyle _typeLine = DashStyle.Solid) { AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 4); Pen p = new Pen(color, lineWidth); p.DashStyle = _typeLine; p.CustomEndCap = bigArrow; if (_type == ArrowType.TwoWay) { p.CustomStartCap = bigArrow; } graphic.DrawLine(p, st.x, st.y, ed.x, ed.y); }
private void pn_show_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { var mouse = e.Location; if (pointSelected == null) { return; } foreach (var point in points) { if (point.checkInSide(mouse.X, mouse.Y) == true && point == pointSelected) { showContextMenu(mouse); break; } } this.Invalidate(); } if (e.Button == MouseButtons.Left) { var mouse = e.Location; foreach (var point in points) { if (point.checkInSide(mouse.X, mouse.Y) == true) { pointSelected = point; pn_show.Invalidate(); return; } } pointSelected = null; pn_show.Invalidate(); } }
public static CPoint add(CPoint st, CPoint ed) { return(new CPoint(st.x + ed.x, st.y + ed.y)); }
public static void showLable(Graphics graphic, CPoint point, string msg, int deltaY = 0) { string lable = msg; graphic.DrawString(lable, font, Brushes.Black, point.x - 30, point.y + deltaY); }
public static void showLable(Graphics graphic, CPoint point) { string lable = "(" + (int)point.x + ":" + (int)point.y + ")"; graphic.DrawString(lable, font, Brushes.Black, point.x - 20, point.y - point.radius - 20); }
public static float mag(CPoint st) { return((float)Math.Sqrt(st.x * st.x + st.y * st.y)); }
public static CPoint mul(CPoint st, float a) { return(new CPoint(st.x * (int)a, st.y * (int)a)); }
public static CPoint sub(CPoint st, CPoint ed) { return(new CPoint(st.x - ed.x, st.y - ed.y)); }
public void Generator() { ResetInfo(); // sinh điểm mẫu nhiên Random random = new Random(); int gridX = 50; int gridY = 50; int uWidth = pn_show.Width / gridX; int uHeight = pn_show.Height / gridY; for (int i = 0; i < pointCount; i++) { CPoint p = new CPoint(uWidth * random.Next(1, gridX - 1), uHeight * random.Next(1, gridY - 1), radius); points.Add(p); } // sinh đường đi và ma trận for (int hang = 0; hang < pointCount; hang++) { for (int cot = 0; cot < pointCount; cot++) { if (hang == cot) { matRouting[hang, cot] = 0; } else { matRouting[hang, cot] = -1; } } } int count = 0; int y = random.Next(0, pointCount - 1); int dty = 1; while (count < rayCount) { int x = random.Next(0, pointCount - 1); if (x != y && matRouting[x, y] == -1 && matRouting[y, x] == -1) { int ty = random.Next(1, 6); // ty le 4/5 if (ty <= 4) { matRouting[x, y] = (int)Utils.mag(points[x], points[y]); matRouting[y, x] = -1; oneDimCount++; } else { int dis = (int)Utils.mag(points[x], points[y]); matRouting[x, y] = dis; matRouting[y, x] = dis; twoDimCount++; } count++; if (count == (pointCount * (pointCount - 1)) / 2) { break; } } dty = random.Next(0, 1); if (dty == 0) { y -= 1; } else { y += 1; } if (y >= pointCount) { y = 0; } else if (y < 0) { y = pointCount - 1; } } rayCount = count; UpdateInfo(); }