void _demoTimer_Tick(object sender, EventArgs e) { if (_t0 != null) { if (_t0 <= 1) { bool isBezier = _animateCurve != null; Point[] arrPoints = isBezier ? new Point[] { _animateCurve.SourceState.Position, _animateCurve.ControlPoints[0], _animateCurve.ControlPoints[1], _animateCurve.DestinationState.Position } : _endPoints; Point[] resultPoints = CalcParamPoint(arrPoints, (float)_t0, isBezier); if (isBezier) { _bPoints = resultPoints; } else { _paramPoint = resultPoints[0]; } _t0 += 0.05f; } else { _t1 = 255; _t0 = null; } } if (_t1 != null) { int r = (int)(_ColorGradients[1].R * (255 - _t1) / 255.0 + _ColorGradients[0].R * _t1 / 255.0); int g = (int)(_ColorGradients[1].G * (255 - _t1) / 255.0 + _ColorGradients[0].G * _t1 / 255.0); int b = (int)(_ColorGradients[1].B * (255 - _t1) / 255.0 + _ColorGradients[0].B * _t1 / 255.0); _paramColor = Color.FromArgb(r, g, b); _t1 -= 12.75f; if (_t1 < 0) { _demoTimer.Stop(); _event.Set(); _hilightState = null; _t0 = _t1 = null; _animateCurve = null; } } Invalidate(); }
public void doBackground(object obj) { var inputString = obj as string; var state = _beginState; DemoFinishedEvent dfe = new DemoFinishedEvent(); for (int i = 0; i < inputString.Length; i++) { if (state.Transitions.ContainsKey(inputString[i])) { _t0 = 0; _t1 = null; var destinedStates = state.Transitions[inputString[i]] as List <State>; _endPoints[0] = state.Position; int random = 0; //đối với NFA, một trạng thái tới ngẫu nhiên sẽ được chọn if (destinedStates.Count > 1) { random = _rnd.Next(0, destinedStates.Count); } var destinedState = destinedStates[random]; _animateCurve = (CurvedStateConnector)state.Find( o => o is CurvedStateConnector && ((CurvedStateConnector)(o)).DestinationState == destinedState); _endPoints[1] = destinedState.Position; _ColorGradients[0] = Color.FromArgb(0xCC, 0x66, 0xFF); _ColorGradients[1] = Color.FromArgb(0x00, 0x66, 0xFF); _hilightState = destinedState; //phải được gọi trong ngữ cảnh của UI thread startTimer(); _event.WaitOne(); state = destinedState; } else { dfe.LanguageIsAccepted = false; DemoFinished(this, dfe); return; } } if (_finalStates.Contains(state)) { dfe.LanguageIsAccepted = true; System.Console.WriteLine("Automat accepts this input string"); } else { dfe.LanguageIsAccepted = false; } DemoFinished(this, dfe); }
public void EnterCurveDrawingMode() { _bCurve = true; CurvedStateConnector curvedConnector; if (!(_selectedObj is CurvedStateConnector)) { if (_selectedObj is StateConnector) { var stateConnector = _selectedObj as StateConnector; int index = Array.FindIndex(_selectables, obj => obj == _selectedObj); if (index != -1) { int labelIndex = Array.FindIndex(_selectables, obj => obj == stateConnector.Label); curvedConnector = new CurvedStateConnector( stateConnector.SourceState, stateConnector.DestinationState); _selectables[index] = curvedConnector; _selectables[labelIndex] = curvedConnector.Label; _selectedObj = curvedConnector; curvedConnector.IsSelected = true; } else { throw new InvalidOperationException(OBJECT_NOT_TRACKED_ERROR); } } else { throw new InvalidOperationException(CONVERSION_ERROR); } } curvedConnector = _selectedObj as CurvedStateConnector; _curveControlPoints[0] = curvedConnector.ControlPoints[0]; _curveControlPoints[1] = curvedConnector.ControlPoints[1]; }
protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); if (!_bShow) { return; } Font stateFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point); StringFormat strFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; Pen borderPen = new Pen(Color.ForestGreen, 1); Pen redPen = new Pen(Color.Red, 2); Pen connectorPen = new Pen(Color.FromArgb(0x33, 0x00, 0x33), 1); Brush arrowBrush = new SolidBrush(Color.FromArgb(0x33, 0x00, 0x33)); foreach (Selectable _selectable in _selectables) { CurvedStateConnector curvedConnector = null; if (_selectable is StateConnector) { var connector = _selectable as StateConnector; if (connector.Label.IsSelected) { Pen labelBorderPen = new Pen(Color.DarkRed, 1.5f) { DashStyle = DashStyle.Dash }; var labelRect = connector.Label.GetRect(_automatGraphics); _automatGraphics.DrawRectangle(labelBorderPen, labelRect.Left, labelRect.Top, labelRect.Width, labelRect.Height); labelBorderPen.Dispose(); } Pen pen = connector.IsSelected ? redPen : connectorPen; if (_selectable is CurvedStateConnector) { curvedConnector = _selectable as CurvedStateConnector; } if (curvedConnector != null && curvedConnector.ControlPoints[0] != _InvalidPoint && curvedConnector.ControlPoints[1] != _InvalidPoint) { _automatGraphics.DrawBezier(pen, curvedConnector.SourceState.Position, curvedConnector.ControlPoints[0], curvedConnector.ControlPoints[1], curvedConnector.DestinationState.Position); _automatGraphics.DrawString(curvedConnector.Label.Text, ConnectorLabel.LabelFont, Brushes.Brown, connector.Label.Position); } else { _automatGraphics.DrawLine(pen, connector.SourceState.Position, connector.DestinationState.Position); _automatGraphics.DrawString(connector.Label.Text, ConnectorLabel.LabelFont, Brushes.Brown, connector.Label.Position); } if (connector.ArrowPoints != null) { _automatGraphics.DrawLine(pen, connector.ArrowPoints[0], connector.ArrowPoints[1]); _automatGraphics.DrawLine(pen, connector.ArrowPoints[0], connector.ArrowPoints[2]); } } else if (_selectable is State) { var state = _selectable as State; GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(state.BoundingRect); var boundColor = (_selectable == _hilightState && _t1 >= 0 && _t1 <= 255) ? _paramColor : Color.FromArgb(0xCC, 0x66, 0xFF); Color[] colors = { boundColor, // dark green Color.FromArgb(0xCC, 0xCC, 0xFF), // aqua Color.FromArgb(0xCC, 0xFF, 0xFF) }; // blue float[] relativePositions = { 0f, // Dark green is at the boundary of the triangle. 0.58f, // Aqua is 40 percent of the way from the boundary // to the center point. 1.0f }; // Blue is at the center point. ColorBlend colorBlend = new ColorBlend(); colorBlend.Colors = colors; colorBlend.Positions = relativePositions; PathGradientBrush grd = new PathGradientBrush(gp); //{ // SurroundColors = new Color[] { Color.FromArgb(0xCC, 0x33, 0xCC) }, // CenterColor = Color.FloralWhite //}; grd.InterpolationColors = colorBlend; _automatGraphics.FillEllipse(grd, state.BoundingRect); _automatGraphics.DrawString(state.Label, stateFont, Brushes.DarkSlateBlue, state.BoundingRect, strFormat); Pen stateBorderPen = state.IsSelected ? redPen : borderPen; _automatGraphics.DrawEllipse(stateBorderPen, state.BoundingRect); if (IsFinalState(state)) { Pen markPen = new Pen(Color.Maroon, 4); var newRect = state.BoundingRect; newRect.Inflate(3, 3); _automatGraphics.DrawEllipse(markPen, newRect); markPen.Dispose(); } if (_beginState == state) { Pen pen_draw = new Pen(Color.Black); Point[] pnt = new Point[3]; if (state.X < 305) { pnt[0].X = state.X - 60; pnt[0].Y = state.Y - 15; pnt[1].X = state.X - 60; pnt[1].Y = state.Y + 15; pnt[2].X = state.X - 30; pnt[2].Y = state.Y; } else if (state.X > 305) { pnt[0].X = state.X + 60; pnt[0].Y = state.Y - 15; pnt[1].X = state.X + 60; pnt[1].Y = state.Y + 15; pnt[2].X = state.X + 30; pnt[2].Y = state.Y; } _automatGraphics.DrawPolygon(pen_draw, pnt); } grd.Dispose(); gp.Dispose(); } } if (_bCurve) { if (_curveControlPoints[0] != InvalidPoint) { OnPaintHandle(_curveControlPoints[0]); } if (_curveControlPoints[1] != InvalidPoint) { OnPaintHandle(_curveControlPoints[1]); } } OnAnimatePaint(_automatGraphics); pe.Graphics.DrawImage(_automatBitmap, DisplayRectangle, DisplayRectangle, GraphicsUnit.Pixel); stateFont.Dispose(); strFormat.Dispose(); borderPen.Dispose(); redPen.Dispose(); connectorPen.Dispose(); arrowBrush.Dispose(); }
public void BuildAutomata() { _bShow = true; _SelCtrlPointIndex = -1; _angleStep = 2 * Math.PI / _drawnStateList.Count; int halfWidth = DisplayRectangle.Width / 2; Point ptCenter = new Point(halfWidth, DisplayRectangle.Height / 2); //int r = halfWidth * 2 / 3; int r = -200; double a = 0; List <Selectable> selectableList = new List <Selectable>(); //pre calculate States's position foreach (State state in _drawnStateList) { if (state.Label == "q0") { _beginState = state; } state.X = (int)(r * Math.Cos(a)) + ptCenter.X; state.Y = (int)(r * Math.Sin(a)) + ptCenter.Y; selectableList.Add(state); foreach (DictionaryEntry de in state.Transitions) //tạo đường nối { var deValue = de.Value as List <State>; foreach (var destinedState in deValue) { //try to compact State Connectors StateConnector dupConnector = null; if (state == destinedState) { //phải tìm đường nối cong sẵn có dupConnector = (CurvedStateConnector)selectableList.Find (obj => obj is CurvedStateConnector && ((StateConnector)obj).DestinationState == destinedState && ((StateConnector)obj).SourceState == state); } else { dupConnector = (StateConnector)selectableList.Find (obj => obj is StateConnector && ((StateConnector)obj).DestinationState == destinedState && ((StateConnector)obj).SourceState == state); } if (dupConnector != null) { continue; } else { StateConnector connector; if (state == destinedState) { //nếu trạng thái đi và đến là cùng một trạng thái //thì ta sẽ tạo đường cong CurvedStateConnector newCurvedConnector = new CurvedStateConnector (state, destinedState); var p = new Point(); if (state.Y < 305) { p = new Point(state.Position.X, state.Position.Y - 100); } else { p = new Point(state.Position.X, state.Position.Y + 100); } var controlPoints = newCurvedConnector.ControlPoints; controlPoints[0].X = p.X - 30; controlPoints[0].Y = controlPoints[1].Y = p.Y; controlPoints[1].X = p.X + 30; connector = newCurvedConnector; } else { connector = new StateConnector(state, destinedState); } selectableList.Add(connector); } } } a += _angleStep; } List <Selectable> labelList = new List <Selectable>(); foreach (Selectable _selectable in selectableList) { if (_selectable is StateConnector) { var connector = _selectable as StateConnector; connector.CalcArrow(); connector.CalcLabelPosition(); labelList.Add(connector.Label); } } selectableList.AddRange(labelList); selectableList.Sort(CompareSelectables); _selectables = selectableList.ToArray(); }