Naveen

Naveen

  • 1.5k
  • 147
  • 8.7k

i want to use graphics at some other place

Jul 12 2024 5:59 AM
private void S57Control_PaintOverlaysEvent(object sender, Graphics gr)
{

    // Initialize usvTrail if not already done
    if (usvTrail == null)
    {
        usvTrail = new List<Tuple<GeoCoordinate,double>>();
    }

    // USV Position
    if (DataRead._USVGeoCoordinate != null)
    {
        usvPixelPoint = m_draw.GeoPix.Point(new GeoPoint
        {
            Lat = DataRead._USVGeoCoordinate.Latitude,
            Lon = DataRead._USVGeoCoordinate.Longitude
        });

        //var usvGeoPoint = m_draw.GeoPix.GeoPointFromPixelPoint(usvPixelPoint);

        if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView)
        {
            var currentusvGeocordinate = new GeoCoordinate(DataRead._USVGeoCoordinate.Latitude, DataRead._USVGeoCoordinate.Longitude);

            if (usvTrail.Count > 0)
            {
                lastPosition = usvTrail[usvTrail.Count - 1].Item1;
                lastPositionAngle = usvTrail[usvTrail.Count - 1].Item2;
                float distance = DataRead.DistanceBetweenPoints(lastPosition, currentusvGeocordinate);
                if (distance >= 10)
                {
                    usvTrail.Add(new Tuple<GeoCoordinate, double>(currentusvGeocordinate, DataRead.USV_Angle));
                }
            }
            else
            {
                usvTrail.Add(new Tuple<GeoCoordinate, double>(currentusvGeocordinate, DataRead.USV_Angle));
            }

            if (DataRead._USVGeoCoordinate != null)
            {
                //Draw the USV at the current position
                usvRectangle = new Rectangle(Convert.ToInt32(usvPixelPoint.X - 14),
                    Convert.ToInt32(usvPixelPoint.Y - 14), 30, 30);
            }

            //Draw Circle around USV
            Color transparentOrange = Color.FromArgb(50, Color.DarkOrange);
            var usvPixelPerMeters = m_draw.PixelSizeMetres * m_draw.GeoPix.DisplayScale;
            int radius = (int)(600 / (2 * Math.PI) / usvPixelPerMeters);

            using (Brush brush = new SolidBrush(transparentOrange))
            {
                gr.FillEllipse(brush, usvPixelPoint.X - radius, usvPixelPoint.Y - radius, radius * 2, radius * 2);
            }

            Font distFont = new Font("Calibri", 11,FontStyle.Bold);
            Brush textBrush = Brushes.Black;

            PointF topPoint = new PointF(usvPixelPoint.X - 30, usvPixelPoint.Y - radius - 15);
            gr.DrawString("Safe Zone", distFont, textBrush, topPoint);
            
            if (_rotatechartbuttonChecked)
            {
                m_draw.SetDirectionOfUp(DataRead.USV_Angle, _defaultPixelPoint);

                //Draw USVImage
                Bitmap rotatedImage = RotateImage(_usvImage, 0);
                gr.DrawImage(rotatedImage, usvRectangle);

                gr.SmoothingMode = SmoothingMode.AntiAlias;

                //Draw trail
                foreach (var point in usvTrail)
                {
                    var trailPixelPoint = m_draw.GeoPix.Point(new GeoPoint
                    {
                        Lat = point.Item1.Latitude,
                        Lon = point.Item1.Longitude
                    });

                    trailRectangle = new Rectangle(Convert.ToInt32(trailPixelPoint.X), Convert.ToInt32(trailPixelPoint.Y), 10, 10);
                    RotateRectangle(gr, trailRectangle, point.Item2 - DataRead.USV_Angle);
                    if (point.Item2 >= 0 && point.Item2 < 90)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X - 3, trailRectangle.Y, 3, 10);
                    }
                    else if (point.Item2 >= 90 && point.Item2 < 180)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y + 7, 3, 10);
                    }
                    else if (point.Item2 >= 180 && point.Item2 <= 270)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X + 7, trailRectangle.Y, 3, 10);
                    }
                    else if (point.Item2 > 270 && point.Item2 < 360)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y, 3, 10);
                    }
                }
                gr.SmoothingMode = SmoothingMode.Default;
            }
            else if (!_rotatechartbuttonChecked)
            {
                m_draw.SetDirectionOfUp(0, _defaultPixelPoint);

                //Draw USVImage
                Bitmap rotatedImage = RotateImage(_usvImage, DataRead.USV_Angle);
                gr.DrawImage(rotatedImage, usvRectangle);

                gr.SmoothingMode = SmoothingMode.AntiAlias;

                // Draw trail
                foreach (var point in usvTrail)
                {
                    var trailPixelPoint = m_draw.GeoPix.Point(new GeoPoint
                    {
                        Lat = point.Item1.Latitude,
                        Lon = point.Item1.Longitude
                    });

                    trailRectangle = new Rectangle(Convert.ToInt32(trailPixelPoint.X), Convert.ToInt32(trailPixelPoint.Y), 10, 10);
                    RotateRectangle(gr, trailRectangle, point.Item2);
                    if (point.Item2 >= 0 && point.Item2 < 90)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X - 3, trailRectangle.Y, 3, 10);
                    }
                    else if (point.Item2 >= 90 && point.Item2 < 180)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y + 7, 3, 10);
                    }
                    else if (point.Item2 >= 180 && point.Item2 <= 270)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X + 7, trailRectangle.Y, 3, 10);
                    }
                    else if (point.Item2 > 270 && point.Item2 < 360)
                    {
                        gr.DrawRectangle(Pens.Black, trailRectangle.X, trailRectangle.Y, 3, 10);
                    }
                }
                gr.SmoothingMode = SmoothingMode.Default;
            }

            //Draw AIS
            if (windowsUIButtonPanel4.Buttons["AIS"].Properties.Checked)
            {
                DrawAis(gr);
            }

            // Draw RangeBar
            //var futureDistance = DataRead.USV_SOG * mpsToKmPerHour * 1 * MinToHour; // distance in nautical miles for 1 minute
            //var futurePoint = DataRead.FindPointAtDistanceFrom(
            //    currentusvGeocordinate,
            //    DataRead.ToRad(DataRead.USV_Angle),
            //    futureDistance);

            //var futureDistancePixelPoint = m_draw.GeoPix.Point(new GeoPoint { Lat = futurePoint.Latitude, Lon = futurePoint.Longitude });
            //var futureGeopoint = m_draw.GeoPix.GeoPointFromPixelPoint(futureDistancePixelPoint);

            //gr.SmoothingMode = SmoothingMode.AntiAlias;
            //using (var rangeBarPen = new Pen(Color.Black, 3))
            //{
            //    gr.DrawLine(rangeBarPen, (int)usvGeoPoint.Lat, (int)usvGeoPoint.Lon, (int)futureGeopoint.Lat, (int)futureGeopoint.Lon);
            //}
            //gr.SmoothingMode = SmoothingMode.Default;
        }
    }
    S57Control1.Invalidate();
}

private void DrawAis(Graphics gr)
{
    //AIS Position
    if (aisTrail == null)  //Initialize usvTrail if not already done
    {
        aisTrail = new List<Tuple<double,GeoCoordinate>>();
    }

    if (DataRead._AISGeoCoordinate != null)
    {
        aisPixelPoint = m_draw.GeoPix.Point(new GeoPoint
        {
            Lat = DataRead._AISGeoCoordinate.Latitude,
            Lon = DataRead._AISGeoCoordinate.Longitude
        });

        //var usvGeoPoint = m_draw.GeoPix.GeoPointFromPixelPoint(usvPixelPoint);

        if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView)
        {
            var currentaisGeocordinate = new GeoCoordinate(DataRead._AISGeoCoordinate.Latitude,
                DataRead._AISGeoCoordinate.Longitude);

            if (aisTrail.Count > 0)
            {
                lastaisPosition = aisTrail[aisTrail.Count - 1].Item2;
                lastaisPositionAngle = aisTrail[aisTrail.Count - 1].Item1;
                float aisdistance = DataRead.DistanceBetweenPoints(lastaisPosition, currentaisGeocordinate);
                if (aisdistance >= 10)
                {
                    aisTrail.Add(new Tuple<double, GeoCoordinate>(DataRead.AIS_Angle,currentaisGeocordinate));
                }
            }
            else
            {
                aisTrail.Add(new Tuple<double, GeoCoordinate>(DataRead.AIS_Angle, currentaisGeocordinate));
            }

            if (DataRead._AISGeoCoordinate != null)
            {
                //Draw the USV at the current position
                aisRectangle = new Rectangle(Convert.ToInt32(aisPixelPoint.X - 14),
                    Convert.ToInt32(aisPixelPoint.Y - 14), 30, 30);
            }

            //Draw AIS Image
            Bitmap rotatedAISImage = RotateImage(_aisImage, DataRead.AIS_Angle);
            gr.DrawImage(rotatedAISImage, aisRectangle);

            gr.SmoothingMode = SmoothingMode.AntiAlias;

            // Draw trail
            foreach (var aispoint in aisTrail)
            {
                var trailaisPixelPoint = m_draw.GeoPix.Point(new GeoPoint
                {
                    Lat = aispoint.Item2.Latitude,
                    Lon = aispoint.Item2.Longitude
                });

                trailaisRectangle = new Rectangle(Convert.ToInt32(trailaisPixelPoint.X),
                    Convert.ToInt32(trailaisPixelPoint.Y), 10, 10);
                RotateRectangle(gr, trailaisRectangle, aispoint.Item1);
                if (aispoint.Item1 >= 0 && aispoint.Item1 < 90)
                {
                    gr.DrawRectangle(Pens.Black, trailaisRectangle.X - 3, trailaisRectangle.Y, 3, 10);
                }
                else if (aispoint.Item1 >= 90 && aispoint.Item1 < 180)
                {
                    gr.DrawRectangle(Pens.Black, trailaisRectangle.X, trailaisRectangle.Y + 7, 3, 10);
                }
                else if (aispoint.Item1 >= 180 && aispoint.Item1 <= 270)
                {
                    gr.DrawRectangle(Pens.Black, trailaisRectangle.X + 7, trailaisRectangle.Y, 3, 10);
                }
                else if (aispoint.Item1 > 270 && aispoint.Item1 < 360)
                {
                    gr.DrawRectangle(Pens.Black, trailaisRectangle.X, trailaisRectangle.Y, 3, 10);
                }
            }
            gr.SmoothingMode = SmoothingMode.Default;
        }
    }
} 

here in this code i am drawing two images usv and ais. but when i am changing the angle of usv and speed of usv its effecting the drawing of ais image so i dont want call drawais(gr) inside that paintoverlay event any other way i can call that method somewhere?


Answers (2)