All pastes #641961 Raw Edit

failed draw line

public text v1 · immutable
#641961 ·published 2007-08-01 16:54 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenStreetMap;
using BdgMobile.BdgServ;

namespace BdgMobile
{
    public partial class MapForm : Form
    {
        string applicationPath;
        string mapPath;
        //public MapForm(int x, int y)
        int mainRow, mainCol;
        double _zoom;
        Point relativePosition, position;
        double relativeZoom;
        int relativeWidth, relativeHeight;
        int relativeSliceCount;
        Bitmap backBuffer;
        Graphics graphics;
        int prevRow, prevCol, prevColCount, prevRowCount, prevRelativeSliceCount;
        Image[][] imageArray;
        IList<Segment> solution;
        int maxSliceCount, sliceWidth, sliceHeight;
        double bottomLeftLatitude, bottomLeftLongitude, topRightLatitude, topRightLongitude, totalZoom, projection, mapWidth, mapZoom, mapHeight;
        Point mousePosition, prevMousePosition;
        BdgServ.Service WS = new Service();

        public MapForm(long[] solution)
        {
            InitializeComponent();
            applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            StreamReader uhuy = File.OpenText(applicationPath + @"\server.ini");
            WS.Url = uhuy.ReadLine();
            if (solution != null)
            {
                this.solution = new List<Segment>();
                foreach (long solId in solution)
                {
                    if (solId != 0)
                        this.solution.Add(new Segment(solId, new OsmNode(0, WS.getLat1FromSegment(solId), WS.getLon1FromSegment(solId)), new OsmNode(0, WS.getLat2FromSegment(solId), WS.getLon2FromSegment(solId))));
                }
            }

            #region Map's properties definition

            mainRow = 0;
            mainCol = 0;

            _zoom = 1;
            maxSliceCount = 10;
            sliceHeight = 723;
            sliceWidth = 691;

            bottomLeftLatitude = -6.96560302144619; //-6.96560302144619

            bottomLeftLongitude = 107.558748921228; //107.558748921228


            topRightLatitude = -6.86352574146708; //-6.86352574146708


            topRightLongitude = 107.65705080874; //107.65705080874

            double middleLatitude = (topRightLatitude + bottomLeftLatitude) / 2.0;
            double latr = (middleLatitude * 3.1415926) / 180;
            double cosLat = 1 - (Math.Pow(latr, 2) / 2.0) + (Math.Pow(latr, 4) / 24.0);
            projection = 1 / cosLat;
            double dataZoom = 0.7;
            double dataWidth = (topRightLongitude - bottomLeftLongitude) * dataZoom * 10000;
            double dataHeight = (topRightLatitude - bottomLeftLatitude) * 10000 * dataZoom * projection;

            mapWidth = maxSliceCount * sliceWidth;
            mapHeight = maxSliceCount * sliceHeight;

            mapZoom = (maxSliceCount * sliceWidth) / (dataWidth + 3);

            totalZoom = dataZoom * mapZoom;

            //Center(CenterPoint);
            //mapInfo = (MapInfo)_data.Query(typeof(MapInfo))[0];

            //Redraw();
            #endregion

            #region Paths

            applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            mapPath = applicationPath + @"\Map\";

            #endregion
        }

        private Point CenterPoint
        {
            get
            {
                Point center = new Point();
                center.X = (int)Math.Round((relativePosition.X + Math.Round(pictureBox1.Width / 2.0)) / _zoom);
                center.Y = (int)Math.Round((relativePosition.Y + Math.Round(pictureBox1.Height / 2.0)) / _zoom);
                return center;
            }
        }
       
        private Point OsmNodeToPoint(OsmNode node)
        {
            Point res = new Point();
            double x = ((topRightLongitude - node.Longitude) * 10000 * totalZoom);
            double y = ((bottomLeftLatitude - node.Latitude) * 10000 * totalZoom * projection);
            res.X = (int)Math.Round((mapWidth - x) - ((3 / 2.0) * mapZoom));
            res.Y = (int)Math.Round((mapHeight + y) - ((3 / 2.0) * mapZoom));
            return res;
        }

        public void Redraw()
        {
            relativePosition.X = (int)Math.Round(position.X / relativeZoom);
            relativePosition.Y = (int)Math.Round(position.Y / relativeZoom);

            if (backBuffer != null)
                backBuffer.Dispose();
            backBuffer = null;

            backBuffer = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            graphics = Graphics.FromImage(backBuffer);

            int colCount, rowCount;

            mainCol = relativePosition.X / relativeWidth;
            if (mainCol >= relativeSliceCount)
                mainCol = relativeSliceCount - 1;
            mainRow = relativePosition.Y / relativeHeight;
            if (mainRow >= relativeSliceCount)
                mainRow = relativeSliceCount - 1;

            colCount = (int)Math.Ceiling((pictureBox1.Width - (relativeWidth - (relativePosition.X % relativeWidth))) / (double)relativeWidth) + 1;
            if (mainCol + colCount > relativeSliceCount)
                colCount = relativeSliceCount - mainCol;
            rowCount = (int)Math.Ceiling((pictureBox1.Height - (relativeHeight - (relativePosition.Y % relativeHeight))) / (double)relativeHeight) + 1;
            if (mainRow + rowCount > relativeSliceCount)
                rowCount = relativeSliceCount - mainRow;

            if ((mainCol != prevCol || mainRow != prevRow) || prevColCount != colCount || prevRowCount != rowCount || prevRelativeSliceCount != relativeSliceCount)
            {
                Cursor.Current = Cursors.WaitCursor;
                if (imageArray != null)
                    for (int i = 0; i < imageArray.Length; i++)
                    {
                        for (int y = 0; y < imageArray[i].Length; y++)
                        {
                            imageArray[i][y].Dispose();
                            imageArray[i][y] = null;
                        }
                    }
                imageArray = new Image[rowCount][];
                for (int i = 0; i < rowCount && mainRow + i < relativeSliceCount; i++)
                {
                    imageArray[i] = new Image[colCount];
                    for (int y = 0; y < colCount && mainCol + y < relativeSliceCount; y++)
                    {
                        int row = mainRow + i;
                        int col = mainCol + y;
                        int zoom = (int)(((double)relativeSliceCount / maxSliceCount) * 100);

                        imageArray[i][y] = new Bitmap(mapPath + zoom.ToString() + @"\map_" + zoom.ToString() + "-" + row.ToString() + "-" + col.ToString() + ".png");
                    }
                }

                prevCol = mainCol;
                prevRow = mainRow;
                prevColCount = colCount;
                prevRowCount = rowCount;
                prevRelativeSliceCount = relativeSliceCount;
                Cursor.Current = Cursors.Default;
            }

            graphics.Clear(Color.White);

            for (int i = 0; i < imageArray.Length; i++)
            {
                for (int y = 0; y < imageArray[i].Length; y++)
                {
                    graphics.DrawImage(imageArray[i][y], new Rectangle(0 + (y * relativeWidth) - (relativePosition.X % relativeWidth), 0 + (i * relativeHeight - (relativePosition.Y % relativeHeight)), relativeWidth, relativeHeight), new Rectangle(0, 0, sliceWidth, sliceHeight), GraphicsUnit.Pixel);
                }
            }
            //if (pointer != null)
            //{
            //    if (((pointer.X * _zoom) - relativePosition.X > 0) && ((pointer.Y * _zoom) - relativePosition.Y > 0))
            //    {
            //        Rectangle destRect = new Rectangle((int)Math.Round((pointer.X * _zoom) - relativePosition.X - (pointerImage.Width / 2.0)), (int)Math.Round((pointer.Y * _zoom) - relativePosition.Y - pointerImage.Height), pointerImage.Width, pointerImage.Height);
            //        ImageAttributes attrib = new ImageAttributes();
            //        attrib.SetColorKey(pointerImage.GetPixel(0, 0), pointerImage.GetPixel(pointerImage.Width - 1, pointerImage.Height - 1));
            //        graphics.DrawImage(pointerImage, destRect, 0, 0, pointerImage.Width, pointerImage.Height, GraphicsUnit.Pixel, attrib);
            //    }
            //}

            //for (int i = 0; i < 5; i++)
            //{
            //    OpenStreetMap.OsmNode startingPoint = LoadStartingPoint(i);
            //    if (startingPoint != null)
            //    {
            //        Point location = OsmNodeToPoint(startingPoint);
            //        if (((location.X * _zoom) - relativePosition.X > 0) && ((location.Y * _zoom) - relativePosition.Y > 0))
            //        {
            //            Rectangle destRect = new Rectangle((int)Math.Round((location.X * _zoom) - relativePosition.X - (startingPointImage[i].Width / 2.0)), (int)Math.Round((location.Y * _zoom) - relativePosition.Y - (startingPointImage[i].Height / 2.0)), startingPointImage[i].Width, startingPointImage[i].Height);
            //            ImageAttributes attrib = new ImageAttributes();
            //            attrib.SetColorKey(startingPointImage[i].GetPixel(0, 0), startingPointImage[i].GetPixel(0, 0));
            //            graphics.DrawImage(startingPointImage[i], destRect, 0, 0, startingPointImage[i].Width, startingPointImage[i].Height, GraphicsUnit.Pixel, attrib);
            //        }
            //    }
            //}

            //if (destination != null)
            //{
            //    Point dest = OsmNodeToPoint(destination);
            //    if (((dest.X * _zoom) - relativePosition.X > 0) && ((dest.Y * _zoom) - relativePosition.Y > 0))
            //    {
            //        Rectangle destRect = new Rectangle((int)Math.Round((dest.X * _zoom) - relativePosition.X - (destinationImage.Width / 2.0)), (int)Math.Round((dest.Y * _zoom) - relativePosition.Y - destinationImage.Height), destinationImage.Width, destinationImage.Height);
            //        ImageAttributes attrib = new ImageAttributes();
            //        attrib.SetColorKey(destinationImage.GetPixel(0, 0), destinationImage.GetPixel(0, 0));
            //        graphics.DrawImage(destinationImage, destRect, 0, 0, destinationImage.Width, destinationImage.Height, GraphicsUnit.Pixel, attrib);
            //    }
            //}

            //if (origin != null)
            //{
            //    Point org = OsmNodeToPoint(origin);
            //    if (((org.X * _zoom) - relativePosition.X > 0) && ((org.Y * _zoom) - relativePosition.Y > 0))
            //    {
            //        Rectangle destRect = new Rectangle((int)Math.Round((org.X * _zoom) - relativePosition.X - (originImage.Width / 2.0)), (int)Math.Round((org.Y * _zoom) - relativePosition.Y - originImage.Height), originImage.Width, originImage.Height);
            //        ImageAttributes attrib = new ImageAttributes();
            //        attrib.SetColorKey(originImage.GetPixel(0, 0), originImage.GetPixel(0, 0));
            //        graphics.DrawImage(originImage, destRect, 0, 0, originImage.Width, originImage.Height, GraphicsUnit.Pixel, attrib);
            //    }
            //}
           
            //MessageBox.Show(solution.Count.ToString());
            if (solution != null)
            {
                if (solution.Count > 0)
                {
                    Point[][] map;
                    for (int i = 0; i < solution.Count; i++)
                    {
                        if (solution[i] != null)
                        {

                            Point fromNodePosition = OsmNodeToPoint(solution[i].From);
                            Point toNodePosition = OsmNodeToPoint(solution[i].To);

                            Point relativeFromNodePosition = new Point();
                            relativeFromNodePosition.X = (int)Math.Round((fromNodePosition.X * _zoom) - relativePosition.X);
                            relativeFromNodePosition.Y = (int)Math.Round((fromNodePosition.Y * _zoom) - relativePosition.Y);

                            Point relativeToNodePosition = new Point();
                            relativeToNodePosition.X = (int)Math.Round((toNodePosition.X * _zoom) - relativePosition.X);
                            relativeToNodePosition.Y = (int)Math.Round((toNodePosition.Y * _zoom) - relativePosition.Y);
                            
                            if ((relativeFromNodePosition.X > 0 && relativeFromNodePosition.Y > 0) || (relativeToNodePosition.X > 0 && relativeToNodePosition.Y > 0))
                            {
                                
                                //graphics.DrawLine(new Pen(Color.Red), relativeFromNodePosition.X, relativeFromNodePosition.Y, relativeToNodePosition.X, relativeToNodePosition.Y);
                            }
                        }
                    }

                }
                graphics = Graphics.FromImage(backBuffer);
                if (pictureBox1.Image != null)
                    pictureBox1.Image.Dispose();
                pictureBox1.Image = backBuffer;

                if (graphics != null)
                    graphics.Dispose();
                graphics = null;
            }
          
        //graphics = Graphics.FromImage(backBuffer);
        //        if (pictureBox1.Image != null)
        //            pictureBox1.Image.Dispose();
        //        pictureBox1.Image = backBuffer;

        //        if (graphics != null)
        //            graphics.Dispose();
        //        graphics = null;
        }

        private void Zoom(double zoom, Point center)
        {
            _zoom = Math.Round(zoom, 2);
            relativeSliceCount = (int)Math.Ceiling(maxSliceCount * _zoom);
            relativeZoom = Math.Round(_zoom / (relativeSliceCount / (double)maxSliceCount), 2);
            relativeWidth = (int)Math.Round(sliceWidth * relativeZoom);
            relativeHeight = (int)Math.Round(sliceHeight * relativeZoom);
            string tes = _zoom.ToString();
            switch ((_zoom * 10).ToString())
            {
                case "1":
                    //zoomInMenuItem.Enabled = true;
                    //zoomOutMenuItem.Enabled = false;
                    //zoomInContextMenuItem.Enabled = true;
                    //zoomOutContextMenuItem.Enabled = false;
                    zoom10MenuItem.Checked = true;
                    zoom30MenuItem.Checked = false;
                    zoom50MenuItem.Checked = false;
                    zoom70MenuItem.Checked = false;
                    zoom100MenuItem.Checked = false;
                    break;
                case "3":
                    //zoomInMenuItem.Enabled = true;
                    //zoomOutMenuItem.Enabled = true;
                    //zoomInContextMenuItem.Enabled = true;
                    //zoomOutContextMenuItem.Enabled = true;
                    zoom10MenuItem.Checked = false;
                    zoom30MenuItem.Checked = true;
                    zoom50MenuItem.Checked = false;
                    zoom70MenuItem.Checked = false;
                    zoom100MenuItem.Checked = false;
                    break;
                case "5":
                    //zoomInMenuItem.Enabled = true;
                    //zoomOutMenuItem.Enabled = true;
                    //zoomInContextMenuItem.Enabled = true;
                    //zoomOutContextMenuItem.Enabled = true;
                    zoom10MenuItem.Checked = false;
                    zoom30MenuItem.Checked = false;
                    zoom50MenuItem.Checked = true;
                    zoom70MenuItem.Checked = false;
                    zoom100MenuItem.Checked = false;
                    break;
                case "7":
                    //zoomInMenuItem.Enabled = true;
                    //zoomOutMenuItem.Enabled = true;
                    //zoomInContextMenuItem.Enabled = true;
                    //zoomOutContextMenuItem.Enabled = true;
                    zoom10MenuItem.Checked = false;
                    zoom30MenuItem.Checked = false;
                    zoom50MenuItem.Checked = false;
                    zoom70MenuItem.Checked = true;
                    zoom100MenuItem.Checked = false;
                    break;
                case "10":
                    //zoomInMenuItem.Enabled = false;
                    //zoomOutMenuItem.Enabled = true;
                    //zoomInContextMenuItem.Enabled = false;
                    //zoomOutContextMenuItem.Enabled = true;
                    zoom10MenuItem.Checked = false;
                    zoom30MenuItem.Checked = false;
                    zoom50MenuItem.Checked = false;
                    zoom70MenuItem.Checked = false;
                    zoom100MenuItem.Checked = true;
                    break;
            }
            Center(center);
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mousePosition.X = e.X;
            mousePosition.Y = e.Y;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            prevMousePosition.X = 0;
            prevMousePosition.Y = 0;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            double relativeZoom = Math.Round(_zoom / (relativeSliceCount / (double)maxSliceCount), 2);
            int relativeWidth = (int)Math.Round(sliceWidth * relativeZoom);
            int relativeHeight = (int)Math.Round(sliceHeight * relativeZoom);
            if (prevMousePosition.X != 0 && prevMousePosition.Y != 0)
            {
               
                position.X = position.X + (prevMousePosition.X - e.X);
                if (position.X < 0)
                    position.X = 0;
                int xPosZoom = (int)Math.Ceiling(position.X / relativeZoom);
                if ((xPosZoom + pictureBox1.Width) > relativeWidth * relativeSliceCount)
                    position.X = (int)(((relativeWidth * relativeSliceCount) - pictureBox1.Width) * relativeZoom);
                position.Y = position.Y + (prevMousePosition.Y - e.Y);
                if (position.Y < 0)
                    position.Y = 0;
                int yPosZoom = (int)Math.Ceiling(position.Y / relativeZoom);
                if ((yPosZoom + pictureBox1.Height) > relativeHeight * relativeSliceCount)
                    position.Y = (int)(((relativeHeight * relativeSliceCount) - pictureBox1.Height) * relativeZoom);
                Redraw();
            }
            prevMousePosition.X = e.X;
            prevMousePosition.Y = e.Y;
        }

        private void Center(Point center)
        {
            int x = (int)(Math.Round(center.X * _zoom) - Math.Round(pictureBox1.Width / 2.0));
            int y = (int)(Math.Round(center.Y * _zoom) - Math.Round(pictureBox1.Height / 2.0));

            position.X = (int)Math.Round(x * relativeZoom);
            if (position.X < 0)
                position.X = 0;
            if (position.X / relativeZoom > (relativeWidth * relativeSliceCount) - pictureBox1.Width)
                position.X = (int)Math.Round(((relativeWidth * relativeSliceCount) - pictureBox1.Width) * relativeZoom);
            position.Y = (int)Math.Round(y * relativeZoom);
            if (position.Y < 0)
                position.Y = 0;
            if (position.Y / relativeZoom > (relativeHeight * relativeSliceCount) - pictureBox1.Height)
                position.Y = (int)Math.Round(((relativeHeight * relativeSliceCount) - pictureBox1.Height) * relativeZoom);
            Redraw();
        }


        private void MapForm_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void MapForm_Load(object sender, EventArgs e)
        {
            Point center = new Point();
            center.X = (int)Math.Round(sliceWidth * maxSliceCount / 2.0);
            center.Y = (int)Math.Round(sliceHeight * maxSliceCount / 2.0);
            Zoom(1, center);
            this.Focus();
        }

        private void zoomToMenuItem_Click(object sender, EventArgs e)
        {
            MenuItem menuItem = (MenuItem)sender;
            switch (menuItem.Text)
            {
                case "10%":
                    Zoom(0.1, CenterPoint);
                    break;
                case "30%":
                    Zoom(0.3, CenterPoint);
                    break;
                case "50%":
                    Zoom(0.5, CenterPoint);
                    break;
                case "70%":
                    Zoom(0.7, CenterPoint);
                    break;
                case "100%":
                    Zoom(1.0, CenterPoint);
                    break;
            }
        }
    }
}