public double CalcDistance(PointDouble other) { double dX = other.X - this.X; double dY = other.Y - this.Y; return Math.Sqrt(dX * dX + dY * dY); }
public Intersection() { this.ID = -1; this.Name = ""; this.Location = new PointDouble(); this.Tracks = new List<Track>(); }
public PointDouble Add(PointDouble other) { double newX = this.X + other.X; double newY = this.Y + other.Y; return new PointDouble(newX, newY); }
public Track(int _ID, string _Name, int _Start, int _End, int _waypointsCount = 0, PointDouble[] _Waypoints = null) { this.ID = _ID; this.Name = _Name; this.Start = _Start; this.End = _End; this.Waypoints = new PointDouble[_waypointsCount]; if (_Waypoints != null) this.Waypoints = _Waypoints; }
public Intersection(int _ID, string _Name, PointDouble _Location, List<Track> _Tracks = null) { this.ID = _ID; this.Name = _Name; this.Location = _Location; if (_Tracks == null) this.Tracks = new List<Track>(); else this.Tracks = _Tracks; }
public Point getDrawingPoint(Point drawingOffset, PointDouble offset, double Zoom = 1.0) { double tX = this.X * Zoom; double tY = this.Y * Zoom; if (offset == null) { return new Point((int)Math.Round(tX), (int)Math.Round(tY)); } else { offset = offset.Mul(Zoom); Point result = new Point((int)Math.Round(tX), (int)Math.Round(tY)); result.X += drawingOffset.X + (int)Math.Round(offset.X); result.Y += drawingOffset.Y + (int)Math.Round(offset.Y); return result; } }
public static bool ParseString(string input, out PointDouble output) { output = null; try { string[] split = input.Split((";").ToArray()); if (split.Length == 2) { PointDouble temp = new PointDouble(); temp.X = XmlConvert.ToDouble(split[0]); temp.Y = XmlConvert.ToDouble(split[1]); output = temp; return true; } else return false; } catch { return false; } }
/// <summary> /// Ustawia wartość pozycji kursora i przelicza ją na współrzędne w jednostkach mapy /// </summary> /// <param name="input"></param> public void SetCursorPos(Point input) { CursorPos = input; RealCursorPos = new PointDouble(CursorPos.X / Zoom - Offset.X, CursorPos.Y / Zoom - Offset.Y); }
public void RecalcBounds(Size canvasSize) { double sumX = 0; int numX = 0; double sumY = 0; int numY = 0; double top = 0; double bottom = 0; double left = 0; double right = 0; foreach (Intersection one in this.Intersections) { sumX += one.Location.X; sumY += one.Location.Y; numX++; numY++; if (one.Location.X < left) left = one.Location.X; if (one.Location.X > right) right = one.Location.X; if (one.Location.Y < top) top = one.Location.Y; if (one.Location.Y > bottom) bottom = one.Location.Y; } this.Offset = new PointDouble(-sumX / numX, -sumY / numY); double width = right - left + 10; double height = bottom - top + 10; if(width>=height) { this.Zoom = Math.Floor((double)canvasSize.Width / width); } else { this.Zoom = Math.Floor((double)canvasSize.Height / height); } }
/// <summary> /// W tej procedurze dokonuje się rysowanie mapy /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void panelCanvas_Paint(object sender, PaintEventArgs e) { if (errorCount > 5) return; try { redrawing = true; drawWatch = Stopwatch.StartNew(); Graphics graphicsObj; Bitmap one_frame = new Bitmap(panelCanvas.Width, panelCanvas.Height); graphicsObj = Graphics.FromImage(one_frame); graphicsObj.Clear(Color.White); graphicsObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; drawMiddle = new Point((int)(Math.Round(panelCanvas.Width / 2.0)), (int)(Math.Round(panelCanvas.Height / 2.0))); Pen myPen = new Pen(Color.Black, 1); Font myFont = new System.Drawing.Font("Consolas", 11.0f, FontStyle.Regular, GraphicsUnit.Pixel); Brush myBrush = new SolidBrush(Color.Black); if(frameTime>0) { graphicsObj.DrawString("FPS: " + String.Format("{0:0.00}", 1000.0 / (double)frameTime) + " / drawTime: " + drawTime + " / Zoom: "+PKMap.Zoom, myFont, myBrush, 0, 0); } if (PKMap.isReady) { // Punkt początku mapy. Koordynaty 0,0 int mapZeroSize = 3; PointDouble mapCenter = new PointDouble(0, 0); graphicsObj.DrawRectangle(myPen, mapCenter.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom).X - mapZeroSize, mapCenter.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom).Y - mapZeroSize, 2 * mapZeroSize, 2 * mapZeroSize); Pen interPen = new Pen(Color.FromArgb(0, 90, 255), 2); Font interFont = new System.Drawing.Font("Consolas", 10.0f, FontStyle.Regular, GraphicsUnit.Pixel); foreach (Intersection one in PKMap.Intersections) { Point spot = one.Location.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom); graphicsObj.DrawEllipse(interPen, new Rectangle(spot.X - 2, spot.Y - 2, 4, 4)); graphicsObj.DrawString(one.Name, interFont, myBrush, spot.X + 2, spot.Y + 2); } interFont.Dispose(); interPen.Dispose(); Pen trackPen = new Pen(Color.FromArgb(30, 30, 30), 1); // wyświetlanie istniejących torów foreach (Track one in PKMap.Tracks) { List<Point> points = new List<Point>(); points.Add(PKMap.getIntersectionByID(one.Start).Location.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom)); foreach (PointDouble waypoint in one.Waypoints) { points.Add(waypoint.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom)); } points.Add(PKMap.getIntersectionByID(one.End).Location.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom)); graphicsObj.DrawLines(trackPen, points.ToArray()); } // Wyświetlanie toru w trakcie edycji if (PKMap.TrackStartID >= 0) { List<Point> points = new List<Point>(); points.Add(PKMap.getIntersectionByID(PKMap.TrackStartID).Location.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom)); foreach (PointDouble waypoint in PKMap.TrackPoints) { points.Add(waypoint.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom)); } points.Add(PKMap.RealCursorPos.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom)); graphicsObj.DrawLines(trackPen, points.ToArray()); } trackPen.Dispose(); // Middle Point int crossSize = 10; graphicsObj.DrawLine(myPen, drawMiddle.X, drawMiddle.Y - crossSize, drawMiddle.X, drawMiddle.Y + crossSize); graphicsObj.DrawLine(myPen, drawMiddle.X - crossSize, drawMiddle.Y, drawMiddle.X + crossSize, drawMiddle.Y); // Cursor position display graphicsObj.DrawEllipse(myPen, drawMiddle.X + PKMap.CursorPos.X - 2, drawMiddle.Y + PKMap.CursorPos.Y - 2, 4, 4); graphicsObj.DrawString(String.Format("{0:0.00}", PKMap.RealCursorPos.X) + ";" + String.Format("{0:0.00}", PKMap.RealCursorPos.Y), myFont, myBrush, drawMiddle.X + PKMap.CursorPos.X, drawMiddle.Y + PKMap.CursorPos.Y - 12); // Keyboard input coordinates if (pointCoords != String.Empty) { SizeF textSize = graphicsObj.MeasureString(pointCoords, myFont); graphicsObj.DrawString(pointCoords, myFont, myBrush, panelCanvas.Width - textSize.Width - 5, panelCanvas.Height - textSize.Height - 5); graphicsObj.DrawRectangle(myPen, panelCanvas.Width - textSize.Width - 7, panelCanvas.Height - textSize.Height - 7, textSize.Width + 4, textSize.Height + 4); } // Last pos on map Pen dataPen = new Pen(Color.FromArgb(160, 30, 0), 2); Font dataFont = new System.Drawing.Font("Consolas", 10.0f, FontStyle.Regular, GraphicsUnit.Pixel); if(server.DataPackList.Count > 0) { DataPack one = server.GetLastDataPack(); PointDouble p = new PointDouble((double)one.x / 10.0, (double)one.y / 10.0); int circleRadius = 5; graphicsObj.DrawEllipse(dataPen, p.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom).X - circleRadius,p.getDrawingPoint(drawMiddle, PKMap.Offset, PKMap.Zoom).Y - circleRadius, 2* circleRadius, 2* circleRadius); } dataFont.Dispose(); dataPen.Dispose(); } e.Graphics.DrawImage(one_frame, 0, 0); myPen.Dispose(); myBrush.Dispose(); myFont.Dispose(); graphicsObj.Dispose(); one_frame.Dispose(); drawWatch.Stop(); drawTime = drawWatch.ElapsedMilliseconds; frameWatch.Stop(); frameTime = frameWatch.ElapsedMilliseconds; framesDrawn++; frameWatch = Stopwatch.StartNew(); errorCount = 0; } catch (Exception ex) { Log.AddToConsole("Błąd rysowania mapy (line " + ex.LineNumber().ToString() + "): " + ex.Message, "error"); errorCount++; if (errorCount > 5) { mainTimer.Enabled = false; Log.AddToConsole("Wstrzymano rysowanie mapy z powodu wielokrotnego błędu", "info"); } } redrawing = false; }
/// <summary> /// Wywoływana podczas każdego przemieszczenia myszy nad panelem przelicza przesunięcie /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void panelCanvas_MouseMove(object sender, MouseEventArgs e) { PKMap.SetCursorPos(new Point(e.Location.X - drawMiddle.X, e.Location.Y - drawMiddle.Y)); if(e.Button == MouseButtons.Right) { PointDouble change = new PointDouble(e.Location.X - mapPanStart.X, e.Location.Y - mapPanStart.Y); change = change.Mul(1.0/PKMap.Zoom); PKMap.Offset = PKMap.TempOffset.Add(change); } }